فهرست منبع

add http based session support in frontend

wangweimin 5 سال پیش
والد
کامیت
84496e3185
2فایلهای تغییر یافته به همراه56 افزوده شده و 0 حذف شده
  1. 1 0
      pywebio/html/index.html
  2. 55 0
      pywebio/html/js/form.js

+ 1 - 0
pywebio/html/index.html

@@ -53,6 +53,7 @@
         integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6"
         crossorigin="anonymous"></script>
 <script src="https://cdn.jsdelivr.net/npm/bs-custom-file-input/dist/bs-custom-file-input.js"></script>
+<script src="https://cdn.jsdelivr.net/npm/js-cookie@rc/dist/js.cookie.min.js"></script>
 
 <script>
     $(document).ready(function () {

+ 55 - 0
pywebio/html/js/form.js

@@ -816,6 +816,11 @@
     };
 
 
+    /*
+    * 会话
+    * 向外暴露的事件:on_session_create、on_session_close、on_server_message
+    * 提供的函数:start_session、send_message、close_session
+    * */
     function WebIOSession() {
         this.on_session_create = () => {
         };
@@ -863,6 +868,53 @@
     }
 
 
+    function HttpWebIOSession(api_url, pull_interval_ms = 1000) {
+        WebIOSession.apply(this);
+        this.api_url = api_url;
+        this.interval_pull_id = null;
+
+        var this_ = this;
+        this.start_session = function () {
+            this.interval_pull_id = setInterval(function () {
+                $.ajax({
+                    type: "GET",
+                    url: this_.api_url,
+                    contentType: "application/json; charset=utf-8",
+                    dataType: "json",
+                    success: function (data) {
+                        for (var idx in data)
+                            this_.on_server_message(data[idx]);
+                        this_.on_session_create();
+                    },
+                    error: function () {
+                        console.error('Http pulling failed');
+                    }
+                })
+            }, pull_interval_ms);
+        };
+        this.send_message = function (msg) {
+            $.ajax({
+                type: "POST",
+                url: this.api_url,
+                data: JSON.stringify(msg),
+                contentType: "application/json; charset=utf-8",
+                dataType: "json",
+                success: function (data) {
+                    for (var idx in data)
+                        this_.on_server_message(data[idx]);
+                },
+                error: function () {  // todo
+                    console.error('Http push event failed, event data: %s', msg);
+                }
+            })
+        };
+        this.close_session = function () {
+            Cookies.remove('webio_session_id');
+            this.on_session_close();
+            clearInterval(this.interval_pull_id);
+        };
+    }
+
     var WebIOSession_;
 
     function WebIOController(webio_session, output_container_elem, input_container_elem) {
@@ -884,12 +936,15 @@
                 this_.input_ctrl.handle_message(msg);
             else if (msg.command in this_.output_cmds)
                 this_.output_ctrl.handle_message(msg);
+            else if (msg.command === 'close_session')
+                webio_session.close_session();
             else
                 console.error('Unknown command:%s', msg.command);
         };
     }
 
     return {
+        'HttpWebIOSession': HttpWebIOSession,
         'WebSocketWebIOSession': WebSocketWebIOSession,
         'WebIOController': WebIOController,
         'DisplayAreaButtonOnClick': DisplayAreaButtonOnClick,