+//
+// 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();
+ }
+ }
+}
+