Merge branch 'post-bold2' into avoid-domino
[brisk.git] / web / xynt-streaming.js
index 3db25a1..46bdb42 100644 (file)
@@ -25,6 +25,8 @@ function transport_ws(doc, xynt_streaming, page)
         this.xynt_streaming.log("PAGE: "+page);
         this.ws = new WebSocket(page);
         this.ws.onopen = function () {
+            console.log('WS On open');
+
             self.xynt_streaming.log("onopen");
             if (this.readyState == 1) {
                 // connected
@@ -33,14 +35,16 @@ function transport_ws(doc, xynt_streaming, page)
             }
         };
         this.ws.onmessage = function (msg) {
+            console.log('WS On message');
             self.xynt_streaming.log("onmessage");
             // new data in msg.data
             self.ctx_new += msg.data;
         };
         this.ws.onclose = function (msg) {
-            this.onopen  = null;
-            this.onclose = null;
-            this.onerror = null;
+            console.log('WS On close');
+            self.onopen  = null;
+            self.onclose = null;
+            self.onerror = null;
             self.xynt_streaming.log("onclose"+self.init_steps);
             if (self.init_steps == 0)
                 self.ws_cb("error");
@@ -99,11 +103,58 @@ this.xynt_streaming.log("DEC: "+this.xynt_streaming.transp_fback);
                 }
             }
         }
+        else if (from == "open") {
+            this.flush_out_queue();
+        }
+
         if (this.ws != null && this.ws.readyState > 1) {
            this.stopped = true;
         }
     },
 
+    flush_out_queue: function() {
+        var l_out = this.xynt_streaming.out_queue.length;
+
+        if (l_out == 0)
+            return;
+
+        console.log('flush_out_queue: ' + l_out);
+        for (var i = 0 ; i < l_out ; i++) {
+            if (this.ws.readyState != 1) {
+                break;
+            }
+            var item = this.xynt_streaming.out_queue.shift();
+            var sent = true;
+            try {
+                this.ws.send(item);
+            }
+            catch (ex) {
+                this.xynt_streaming.out_queue.unshift(item);
+                break;
+            }
+        }
+    },
+
+    send: function(msg) {
+        console.log('new send');
+        if (this.ws && this.ws.readyState == 1) {
+            try {
+                console.log('Try send ... ');
+                this.flush_out_queue();
+                this.ws.send(msg);
+                console.log(' ... done');
+            }
+            catch (ex) {
+                console.log(' ... catched exception');
+                this.xynt_streaming.out_queue.push(msg);
+            }
+        }
+        else {
+            console.log('ws not ready: push into out_queue');
+            this.xynt_streaming.out_queue.push(msg);
+        }
+    },
+
     ws_abort: function() {
         if (this.ws != null) {
 this.xynt_streaming.log("WSCLOSE");
@@ -515,7 +566,7 @@ transport_iframe.prototype = {
     }
 }
 
-function xynt_streaming(win, transp_type, transp_port, transp_fback, console, gst, from, cookiename, sess, sandbox, page, cmdproc)
+function xynt_streaming(win, transp_type, transp_port, transp_fback, console, gst, from, cookiename, sess, sandbox, page, page_id, cmdproc)
 {
     this.win = win;
     this.transp_type = transp_type;
@@ -528,11 +579,13 @@ function xynt_streaming(win, transp_type, transp_port, transp_fback, console, gs
     this.sess = sess;
     this.sandbox = sandbox;
     this.page = page;
+    this.page_id = page_id;
     this.cmdproc = cmdproc;
     // this.cmdproc = function(com){/* console.log("COM: "+com); */ eval(com);}
     this.doc = win.document;
     this.keepalive_old = -1;
     this.keepalive_new = -1;
+    this.out_queue = [];
 
     this.mon_errtime = this.keepalives_eq_max * this.watchdog_checktm * this.watchdog_timeout;
     this.mon_wrntime = this.mon_errtime / 2;
@@ -544,6 +597,8 @@ xynt_streaming.prototype = {
     win:               null,
     transp_type:       null,
     transp_port:         80,
+    transp_type_cur:   null,
+    transp_port_cur:     80,
     transp_fback:         0,
     transp:            null,
     console:           null,
@@ -553,6 +608,7 @@ xynt_streaming.prototype = {
     sess:              null,
     sandbox:           null,
     page:              null,
+    page_id:           null,
     cmdproc:           null,
 
     start_time:        0,
@@ -576,6 +632,7 @@ xynt_streaming.prototype = {
     comm_match:        /_*@BEGIN@(.*?)@END@/g,
     comm_clean:        /_*@BEGIN@(.*?)@END@/,
     stream:            "",
+    out_queue:         null,
     the_end:           false,
 
     mon_time:         -1,
@@ -633,14 +690,24 @@ xynt_streaming.prototype = {
 
         // DEFAULT TRANSPORT PROTOCOL HERE websocketsec, websocket
         if (this.transp_fback > 0) {
-            transp_type = "websocketsec";
-            transp_port = (this.transp_fback == 2 ? 80 : 8080);
+            if (location.protocol == 'https:') {
+                transp_type = "websocketsec";
+                transp_port = 443;
+            }
+            else {
+                transp_type = "websocket";
+                transp_port = (this.transp_fback == 2 ? 80 : 8080);
+            }
+
         }
         else {
             transp_type = this.transp_type;
             transp_port = this.transp_port;
         }
 
+        this.transp_type_cur = transp_type;
+        this.transp_port_cur = transp_port;
+
         if (transp_type == "websocket" || transp_type == "websocketsec") {
             var end_proto, first_slash, newpage;
 
@@ -665,7 +732,7 @@ xynt_streaming.prototype = {
         }
         // stat, subst, this.gst.st
 
-        page = url_append_args(page, "sess", this.sess, "stat", stat, "subst", subst, "step", this.gst.st, "from", this.from);
+        page = url_append_args(page, "sess", this.sess, "page_id", this.page_id, "stat", stat, "subst", subst, "step", this.gst.st, "from", this.from);
         // this.log("the page:");
         // this.log(page);
 
@@ -901,6 +968,10 @@ xynt_streaming.prototype = {
                 // alert("SINGLE: ["+singlecomm+"]");
                 // window.console.log("["+singlecomm+"]");
                this.cmdproc(singlecomm);
+                if (this.transp_type_cur) {
+                    this.transp_type = this.transp_type_cur;
+                    this.transp_port = this.transp_port_cur;
+                }
                again = 1;
            }
         } while (again);
@@ -918,6 +989,15 @@ xynt_streaming.prototype = {
         return;
     },
 
+    send: function(msg) {
+        if (typeof(this.transp.send) == 'undefined') {
+            this.log('send not implemented for ' + this.transp_type);
+            return;
+        }
+
+        return this.transp.send(msg);
+    },
+
     //
     // moved to xynt-streaming-ifra as push()
     //