Browse Source

frontend auto select http/websocket session

wangweimin 5 years ago
parent
commit
b13a08658a
3 changed files with 39 additions and 5 deletions
  1. 35 4
      pywebio/html/index.html
  2. 1 1
      pywebio/ioloop.py
  3. 3 0
      pywebio/platform/flask.py

+ 35 - 4
pywebio/html/index.html

@@ -56,6 +56,29 @@
 <script src="https://cdn.jsdelivr.net/npm/js-cookie@rc/dist/js.cookie.min.js"></script>
 
 <script>
+
+    /*
+    * Check given `backend_addr` is a http backend
+    * Usage:
+    *   is_http_backend('http://localhost:8080/io').then(function(http_backend){
+    *        if(http_backend)
+    *               //
+    *        else
+    *               //
+    *   });
+    * */
+    function is_http_backend(backend_addr) {
+        if (!backend_addr.startsWith('http://') && !backend_addr.startsWith('https://'))
+            backend_addr = 'http://' + backend_addr;
+        return new Promise(function (resolve, reject) {
+            $.get(backend_addr, {test: 1}, 'html').done(function (data) {
+                resolve(data === 'ok');
+            }).fail(function (e) {
+                resolve(false);
+            });
+        });
+    }
+
     $(document).ready(function () {
         // https://www.npmjs.com/package/bs-custom-file-input
         bsCustomFileInput.init()
@@ -65,16 +88,24 @@
         if (window.location.hash.length > 0) {
             return window.location.hash.slice(1);
         } else {
-            return "ws://" + window.location.host + "/ws"
+            return "ws://" + window.location.host + "/io"
         }
     }
 
     CodeMirror.modeURL = "https://cdn.bootcss.com/codemirror/2.36.0/%N.js";
 
     var md_body = $('#markdown-body');
-    var session = new WebIO.WebSocketWebIOSession(get_ws_addr());
-    var ctrl = new WebIO.WebIOController(session, md_body, $('#input-container'));
-    session.start_session();
+
+    is_http_backend('http://localhost:8080/io').then(function (http_backend) {
+        var session;
+        if (http_backend)
+            session = new WebIO.HttpWebIOSession('/io');
+        else
+            session = new WebIO.WebSocketWebIOSession(get_ws_addr());
+        var ctrl = new WebIO.WebIOController(session, md_body, $('#input-container'));
+        session.start_session();
+    });
+
 
 </script>
 

+ 1 - 1
pywebio/ioloop.py

@@ -17,7 +17,7 @@ def start_ioloop(coro_func, port=8080, debug=True, tornado_app_args=None):
        ``websocket_max_message_size``.
 
     """
-    handlers = [(r"/ws", webio_handler(coro_func)),
+    handlers = [(r"/io", webio_handler(coro_func)),
                 (r"/(.*)", StaticFileHandler, {"path": STATIC_PATH,
                                                'default_filename': 'index.html'})]
 

+ 3 - 0
pywebio/platform/flask.py

@@ -55,6 +55,9 @@ def _webio_view(coro_func, session_expire_seconds):
     :param session_expire_seconds:
     :return:
     """
+    if request.args.get('test'):  # 测试接口,当会话使用给予http的backend时,返回 ok
+        return 'ok'
+
     global _last_check_session_expire_ts, _event_loop
     if _event_loop:
         asyncio.set_event_loop(_event_loop)