X-Git-Url: https://mop.ddnsfree.com/gitweb/?a=blobdiff_plain;f=web%2Fxynt-streaming.js;h=b9ef124eb0bb5772b23b7fda42432d897e0bfe4a;hb=3b60e5210598df261ee49ad7bf11790fb629d460;hp=a8cad92232d237bf70df822d0ad1f3b9f06287d8;hpb=14136ff3d45c9d1212d6b5e0fd789b57ed73b893;p=brisk.git diff --git a/web/xynt-streaming.js b/web/xynt-streaming.js index a8cad92..b9ef124 100644 --- a/web/xynt-streaming.js +++ b/web/xynt-streaming.js @@ -1,10 +1,229 @@ // old targetpage == page and moved into start method +// +// CLASS transport_ws +// +function transport_ws(doc, xynt_streaming, page) +{ + // if four arguments manage if WS or WSS connection + if (arguments.length > 3) + this.is_secure = arguments[3]; + else + this.is_secure = false; + + if (this.is_secure) + this.name = "WebSocketSecure"; + else + this.name = "WebSocket"; + this.ctx_new = ""; + var self = this; + + this.doc = doc; + this.failed = false; + this.xynt_streaming = xynt_streaming; + try { + 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 + self.ws_cb("open"); + self.init_steps = 1; + } + }; + 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) { + 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"); + else + self.ws_cb("close"); + }; + this.ws.onerror = function () { + // on error + this.onopen = null; + this.onclose = null; + this.onerror = null; + self.xynt_streaming.log("onerror"); + self.ws_cb("error"); + }; + } + catch (ex) { + throw "websocket creation failed"; + } + + this.stopped = false; +} + +transport_ws.prototype = { + doc: null, + name: null, + xynt_streaming: "ready", + ws: null, + stopped: true, + failed: false, + + init_steps: 0, + + ctx_old: "", + ctx_old_len: 0, + ctx_new: "", + + // script_clean: 0, + + destroy: function () { /* public */ + if (this.ws != null) { + this.ws_abort(); + } + delete this.ws; + }, + + ws_cb: function (from) { + var ret; + + if (from == "error") { + if (this.xynt_streaming != "ready") { + if (this.xynt_streaming.transp_fback > 0) { +this.xynt_streaming.log("DEC: "+this.xynt_streaming.transp_fback); + this.xynt_streaming.transp_fback--; + this.stopped = true; + this.xynt_streaming.reload(); + } + } + } + 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"); + this.ws.close(); + } + }, + + xstr_is_init: function () { /* public */ + return (true); + }, + + /* only after a successfull is_initialized call */ + xstr_is_ready: function () { /* public */ + return (this.ws.readyState == 1); + }, + + xstr_set: function () { /* public */ + // already set + }, + + ctx_new_is_set: function () { /* public */ + return (this.ctx_new != null); + }, + + ctx_new_curlen_get: function () { /* public */ + return (this.ctx_new.length); + }, + + ctx_new_getchar: function(idx) { /* public */ + return (this.ctx_new[idx]); + }, + + ctx_old_len_is_set: function () { /* public */ + return (true); + }, + + ctx_old_len_get: function () { /* public */ + return (this.ctx_old_len); + }, + + ctx_old_len_set: function (len) { /* public */ + this.ctx_old_len = len; + }, + + ctx_old_len_add: function (len) { /* public */ + this.ctx_old_len += len; + }, + + new_part: function () { /* public */ + return (this.ctx_new.substr(this.ctx_old_len)); + }, + + scrcls_set: function (step) { /* public */ + // this.script_clean = step; + }, + + postproc: function () { + if (this.stopped && !this.xstr_is_ready()) { + this.xynt_streaming.reload(); + } + } +} + // // CLASS transport_xhr // function transport_xhr(doc, xynt_streaming, page) { + this.name = "XHR"; this.doc = doc; this.xynt_streaming = xynt_streaming; this.xhr = createXMLHttpRequest(); @@ -19,6 +238,7 @@ function transport_xhr(doc, xynt_streaming, page) transport_xhr.prototype = { doc: null, + name: null, xynt_streaming: "ready", xhr: null, stopped: true, @@ -95,6 +315,7 @@ transport_xhr.prototype = { }, ctx_new_getchar: function(idx) { /* public */ + return (this.ctx_new[idx]); }, ctx_old_len_is_set: function () { /* public */ @@ -133,23 +354,24 @@ transport_xhr.prototype = { // function transport_htmlfile(doc, xynt_streaming, page) { + this.name = "HTMLFile"; this.doc = doc; this.xynt_streaming = xynt_streaming; this.transfdoc = new ActiveXObject("htmlfile"); this.transfdoc.open(); - /*this.transfdoc.write("