1 // old targetpage == page and moved into start method
6 function transport_ws(doc, xynt_streaming, page)
8 // if four arguments manage if WS or WSS connection
9 if (arguments.length > 3)
10 this.is_secure = arguments[3];
12 this.is_secure = false;
15 this.name = "WebSocketSecure";
17 this.name = "WebSocket";
24 this.xynt_streaming = xynt_streaming;
26 this.xynt_streaming.log("PAGE: "+page);
27 this.ws = new WebSocket(page);
28 this.ws.onopen = function () {
29 console.log('WS On open');
31 self.xynt_streaming.log("onopen");
32 if (this.readyState == 1) {
38 this.ws.onmessage = function (msg) {
39 console.log('WS On message');
40 self.xynt_streaming.log("onmessage");
41 // new data in msg.data
42 self.ctx_new += msg.data;
44 this.ws.onclose = function (msg) {
45 console.log('WS On close');
49 self.xynt_streaming.log("onclose"+self.init_steps);
50 if (self.init_steps == 0)
55 this.ws.onerror = function () {
60 self.xynt_streaming.log("onerror");
65 throw "websocket creation failed";
71 transport_ws.prototype = {
74 xynt_streaming: "ready",
88 destroy: function () { /* public */
89 if (this.ws != null) {
95 ws_cb: function (from) {
98 if (from == "error") {
99 if (this.xynt_streaming != "ready") {
100 if (this.xynt_streaming.transp_fback > 0) {
101 this.xynt_streaming.log("DEC: "+this.xynt_streaming.transp_fback);
102 this.xynt_streaming.transp_fback--;
104 this.xynt_streaming.reload();
108 else if (from == "open") {
109 this.flush_out_queue();
112 if (this.ws != null && this.ws.readyState > 1) {
117 flush_out_queue: function() {
118 var l_out = this.out_queue.length;
122 for (var i = 0 ; i < l_out ; i++) {
123 if (this.ws.readyState != 1) {
126 var item = this.out_queue.shift();
132 this.out_queue.unshift(item);
138 send: function(msg) {
139 console.log('new send');
140 if (this.ws && this.ws.readyState == 1) {
142 console.log('Try send ... ');
143 this.flush_out_queue();
145 console.log(' ... done');
148 console.log(' ... catched exception');
149 this.flush_out.push(msg);
153 console.log('ws not ready: push into flush_out');
154 this.flush_out.push(msg);
158 ws_abort: function() {
159 if (this.ws != null) {
160 this.xynt_streaming.log("WSCLOSE");
165 xstr_is_init: function () { /* public */
169 /* only after a successfull is_initialized call */
170 xstr_is_ready: function () { /* public */
171 return (this.ws.readyState == 1);
174 xstr_set: function () { /* public */
178 ctx_new_is_set: function () { /* public */
179 return (this.ctx_new != null);
182 ctx_new_curlen_get: function () { /* public */
183 return (this.ctx_new.length);
186 ctx_new_getchar: function(idx) { /* public */
187 return (this.ctx_new[idx]);
190 ctx_old_len_is_set: function () { /* public */
194 ctx_old_len_get: function () { /* public */
195 return (this.ctx_old_len);
198 ctx_old_len_set: function (len) { /* public */
199 this.ctx_old_len = len;
202 ctx_old_len_add: function (len) { /* public */
203 this.ctx_old_len += len;
206 new_part: function () { /* public */
207 return (this.ctx_new.substr(this.ctx_old_len));
210 scrcls_set: function (step) { /* public */
211 // this.script_clean = step;
214 postproc: function () {
215 if (this.stopped && !this.xstr_is_ready()) {
216 this.xynt_streaming.reload();
222 // CLASS transport_xhr
224 function transport_xhr(doc, xynt_streaming, page)
228 this.xynt_streaming = xynt_streaming;
229 this.xhr = createXMLHttpRequest();
230 this.xhr.open('GET', page);
233 this.xhr.onreadystatechange = function () { self.xhr_cb(); };
236 this.stopped = false;
239 transport_xhr.prototype = {
242 xynt_streaming: "ready",
252 destroy: function () { /* public */
253 if (this.xhr != null) {
259 xhr_cb: function () {
262 if (this.xhr.readyState == 4) {
263 // console.log("SS: "+safestatus(xhr));
265 // NOTE: delay management later
267 // if ((ret = safestatus(this.xhr)) == 200) {
269 // // console.log("del a null "+this.delayed);
270 // } else if (ret != -1) {
271 // this.delay = 5000;
273 // // alert('There was a problem with the request.' + ret);
277 // this.delayed = null;
282 xhr_abort: function() {
283 if (this.xhr != null) {
288 xstr_is_init: function () { /* public */
290 if (this.xhr.responseText != null) {
291 this.ctx_new = this.xhr.responseText;
297 return (this.ctx_new != null);
300 /* only after a successfull is_initialized call */
301 xstr_is_ready: function () { /* public */
302 return (this.xynt_streaming == "ready");
305 xstr_set: function () { /* public */
309 ctx_new_is_set: function () { /* public */
310 return (this.ctx_new != null);
313 ctx_new_curlen_get: function () { /* public */
314 return (this.ctx_new.length);
317 ctx_new_getchar: function(idx) { /* public */
318 return (this.ctx_new[idx]);
321 ctx_old_len_is_set: function () { /* public */
325 ctx_old_len_get: function () { /* public */
326 return (this.ctx_old_len);
329 ctx_old_len_set: function (len) { /* public */
330 this.ctx_old_len = len;
333 ctx_old_len_add: function (len) { /* public */
334 this.ctx_old_len += len;
337 new_part: function () { /* public */
338 return (this.ctx_new.substr(this.ctx_old_len));
341 scrcls_set: function (step) { /* public */
342 // this.script_clean = step;
345 postproc: function () {
346 if (this.stopped && !this.xstr_is_ready()) {
347 this.xynt_streaming.reload();
353 // CLASS transport_htmlfile
355 function transport_htmlfile(doc, xynt_streaming, page)
357 this.name = "HTMLFile";
359 this.xynt_streaming = xynt_streaming;
360 this.transfdoc = new ActiveXObject("htmlfile");
361 this.transfdoc.open();
362 this.transfdoc.write("<html><body><iframe id='iframe'></iframe></body></html>");
363 this.transfdoc.close();
365 this.ifra = this.transfdoc.getElementById("iframe");
366 this.ifra.contentWindow.location.href = page;
367 this.stopped = false;
370 transport_htmlfile.prototype = {
373 xynt_streaming: null,
378 destroy: function () { /* public */
379 if (this.ifra != null) {
380 // this.doc.body.removeChild(this.ifra);
385 if (this.transfdoc) {
386 delete this.transfdoc;
387 this.transfdoc = null;
391 xstr_is_init: function () { /* public */
392 return (typeof(this.ifra.contentWindow.xynt_streaming) != 'undefined');
395 /* only after a successfull is_initialized call */
396 xstr_is_ready: function () { /* public */
397 return (this.ifra.contentWindow.xynt_streaming == "ready");
400 /* only after a successfull is_initialized call */
401 xstr_set: function () { /* public */
402 if (this.ifra.contentWindow.xynt_streaming == "ready") {
403 this.ifra.contentWindow.xynt_streaming = this.xynt_streaming;
406 else if (this.ifra.contentWindow.xynt_streaming == this.xynt_streaming) {
414 ctx_new_is_set: function () { /* public */
415 return (typeof(this.ifra.contentWindow.ctx_new) != 'undefined');
418 ctx_new_curlen_get: function () { /* public */
419 return (this.ifra.contentWindow.ctx_new.length);
422 ctx_new_getchar: function(idx) { /* public */
423 return (this.ifra.contentWindow.ctx_new.charAt(idx));
426 ctx_old_len_is_set: function () { /* public */
427 return (typeof(this.ifra.contentWindow.ctx_old_len) != 'undefined');
430 ctx_old_len_get: function () { /* public */
431 return (this.ifra.contentWindow.ctx_old_len);
434 ctx_old_len_set: function (len) { /* public */
435 this.ifra.contentWindow.ctx_old_len = len;
438 ctx_old_len_add: function (len) { /* public */
439 this.ifra.contentWindow.ctx_old_len += len;
442 new_part: function () { /* public */
443 return (this.ifra.contentWindow.ctx_new.substr(this.ifra.contentWindow.ctx_old_len));
446 scrcls_set: function (step) { /* public */
447 this.ifra.contentWindow.script_clean = step;
450 postproc: function () { /* public */
451 if (this.stopped && !this.xstr_is_ready()) {
452 this.xynt_streaming.reload();
458 // CLASS transport_iframe
460 function transport_iframe(doc, xynt_streaming, page)
462 this.name = "IFRAME";
464 this.xynt_streaming = xynt_streaming;
465 this.ifra = doc.createElement("iframe");
466 this.ifra.style.visibility = "hidden";
467 doc.body.appendChild(this.ifra);
468 this.ifra.contentWindow.location.href = page;
469 this.stopped = false;
472 transport_iframe.prototype = {
475 xynt_streaming: null,
479 destroy: function () { /* public */
481 if (this.ifra != null) {
482 // NOTE: on Opera this remove child crash js if called from
483 // inside of the iframe, on IE on Windows without
484 // it stream abort fails.
485 // the problem is fixed setting into the iframe's onload
486 // function the stopped attribute to true and delegate
487 // postproc() fired by xynt_streaming watchdog()
488 this.doc.body.removeChild(this.ifra);
493 alert("destroy exception catched");
497 xstr_is_init: function () { /* public */
498 return (typeof(this.ifra.contentWindow.xynt_streaming) != 'undefined');
501 /* only after a successfull is_initialized call */
502 xstr_is_ready: function () { /* public */
503 return (this.ifra.contentWindow.xynt_streaming == "ready");
506 /* only after a successfull is_initialized call */
507 xstr_set: function () { /* public */
508 if (this.ifra.contentWindow.xynt_streaming == "ready") {
509 this.ifra.contentWindow.xynt_streaming = this.xynt_streaming;
512 else if (this.ifra.contentWindow.xynt_streaming == this.xynt_streaming) {
521 /* only after a successfull is_ready call to be sure the accessibility of the var */
522 xstr_set_old: function (xynt_streaming) { /* public */
523 this.ifra.contentWindow.xynt_streaming = xynt_streaming;
526 ctx_new_is_set: function () { /* public */
527 return (typeof(this.ifra.contentWindow.ctx_new) != 'undefined');
530 ctx_new_curlen_get: function () { /* public */
531 return (this.ifra.contentWindow.ctx_new.length);
534 ctx_new_getchar: function(idx) { /* public */
535 return (this.ifra.contentWindow.ctx_new.charAt(idx));
538 ctx_old_len_is_set: function () { /* public */
539 return (typeof(this.ifra.contentWindow.ctx_old_len) != 'undefined');
542 ctx_old_len_get: function () { /* public */
543 return (this.ifra.contentWindow.ctx_old_len);
546 ctx_old_len_set: function (len) { /* public */
547 this.ifra.contentWindow.ctx_old_len = len;
550 ctx_old_len_add: function (len) { /* public */
551 this.ifra.contentWindow.ctx_old_len += len;
554 new_part: function () { /* public */
555 return (this.ifra.contentWindow.ctx_new.substr(this.ifra.contentWindow.ctx_old_len));
558 scrcls_set: function (step) { /* public */
559 this.ifra.contentWindow.script_clean = step;
562 postproc: function () { /* public */
563 if (this.stopped && !this.xstr_is_ready()) {
564 this.xynt_streaming.reload();
569 function xynt_streaming(win, transp_type, transp_port, transp_fback, console, gst, from, cookiename, sess, sandbox, page, cmdproc)
572 this.transp_type = transp_type;
573 this.transp_port = transp_port;
574 this.transp_fback = transp_fback;
575 this.console = console;
578 this.cookiename = cookiename;
580 this.sandbox = sandbox;
582 this.cmdproc = cmdproc;
583 // this.cmdproc = function(com){/* console.log("COM: "+com); */ eval(com);}
584 this.doc = win.document;
585 this.keepalive_old = -1;
586 this.keepalive_new = -1;
588 this.mon_errtime = this.keepalives_eq_max * this.watchdog_checktm * this.watchdog_timeout;
589 this.mon_wrntime = this.mon_errtime / 2;
594 xynt_streaming.prototype = {
598 transp_type_cur: null,
612 restart_wait: 5000, // wait restart_wait millisec before begin to check if restart is needed
615 cookiepath: "/brisk/",
617 hbit: function () {},
621 /* NOTE: right watch_timeout value to 100, for devel reasons use 1000 or more */
622 /* restart after 4 * 40 * 100 millisec if server ping is missing => 16secs */
623 keepalives_eq_max: 4,
624 watchdog_checktm: 40,
625 watchdog_timeout: 100,
629 comm_match: /_*@BEGIN@(.*?)@END@/g,
630 comm_clean: /_*@BEGIN@(.*?)@END@/,
640 mon_update: function()
642 var date = new Date();
644 this.mon_time = date.getTime();
648 ping arrives at least every RD_KEEPALIVE_TOUT secs (currently 4 secs)
650 return values: 0 unknown
655 mon_status: function()
659 var date = new Date();
661 delta = date.getTime() - this.mon_time;
663 if (delta < this.mon_wrntime)
665 else if (delta < this.mon_errtime)
671 start: function() { /* public */
675 // this.log("start "+this.transp_type+" "+this.transp_fback);
679 createCookie(this.cookiename, sess, 24*365, this.cookiepath);
681 this.log("xynt_streaming:start restart: "+this.restart_n);
682 this.keepalives_equal = 0;
685 this.page = url_complete(this.win.location.href, this.page);
687 // DEFAULT TRANSPORT PROTOCOL HERE websocketsec, websocket
688 if (this.transp_fback > 0) {
689 if (location.protocol == 'https:') {
690 transp_type = "websocketsec";
694 transp_type = "websocket";
695 transp_port = (this.transp_fback == 2 ? 80 : 8080);
700 transp_type = this.transp_type;
701 transp_port = this.transp_port;
704 this.transp_type_cur = transp_type;
705 this.transp_port_cur = transp_port;
707 if (transp_type == "websocket" || transp_type == "websocketsec") {
708 var end_proto, first_slash, newpage;
711 this.log("precha ["+this.page+"]");
712 if (transp_type == "websocketsec") {
713 newpage = this.page.replace(/\.php$/g, "_wss.php").replace(/\.php\?/g, "_wss.php?");
718 end_proto = newpage.indexOf("://");
719 first_slash = newpage.substring(end_proto+3).indexOf("/");
721 page = (transp_type == "websocketsec" ? "wss://" : "ws://")
722 + newpage.substring(end_proto+3, end_proto+3 + first_slash) + ":"
723 + transp_port + newpage.substring(end_proto+3 + first_slash);
724 // this.log("MOP WS: " + page);
729 // stat, subst, this.gst.st
731 page = url_append_args(page, "sess", this.sess, "stat", stat, "subst", subst, "step", this.gst.st, "from", this.from);
732 // this.log("the page:");
736 // transport instantiation
737 if (transp_type == "websocketsec") {
738 page = url_append_args(page, "transp", "websocketsec");
739 this.transp = new transport_ws(this.doc, this, page, true);
741 else if (transp_type == "websocket") {
742 page = url_append_args(page, "transp", "websocket");
743 this.transp = new transport_ws(this.doc, this, page);
745 else if (transp_type == "xhr") {
746 page = url_append_args(page, "transp", "xhr");
747 this.transp = new transport_xhr(this.doc, this, page);
749 else if (transp_type == "iframe") {
750 page = url_append_args(page, "transp", "iframe");
751 this.transp = new transport_iframe(this.doc, this, page);
753 else if (transp_type == "htmlfile") {
754 page = url_append_args(page, "transp", "htmlfile");
755 this.transp = new transport_htmlfile(this.doc, this, page);
761 if (this.transp_fback > 0) {
769 this.watchdog_ct = 0;
771 this.watchdog_hdl = setTimeout(function(obj) { obj.log("tout1"); obj.watchdog(); }, this.watchdog_timeout, this);
774 var date = new Date();
775 this.start_time = date.getTime();
783 hbit_set: function (hbit) {
788 hbit_status: function () {
791 ret = this.mon_status();
792 // console.log("mon_status: "+ret+" 0: "+this.mon_time);
808 if (this.mon_stat_old != mon_stat) {
810 this.mon_stat_old = mon_stat;
815 watchdog: function () {
816 // alert("watchdog");
818 var comm_newpart, comm_len, comm_arr;
821 if (this.sandbox != null) {
822 var zug = "WATCHDOG sess = ["+this.sess+"] step = "+this.gst.st+" step_loc = "+this.gst.st_loc+" step_loc_new = "+this.gst.st_loc_new+"Transport: "+this.transp.name;
823 if (zug != this.sandbox.innerHTML)
824 this.sandbox.innerHTML = zug;
827 // WATCHDOGING THE CONNECTION
828 this.log("hs::watchdog: start, cur equal times: "+this.keepalives_equal);
829 if (!this.watchable) {
832 // if (typeof(this.ifra.contentWindow.xynt_streaming) == 'undefined')
833 if (!this.transp.xstr_is_init()) {
834 this.log("hs::watchdog: xstr_is_init = false");
839 this.log("hs::watchdog: exception");
844 on IE7 the the window frame scope is cleaned after the href is set, so we wait
845 for a well know variable value before assign this object value to it (OO is a passion)
847 // if (this.ifra.contentWindow.xynt_streaming == "ready") {
848 if (this.transp.xstr_set()) {
849 // this.ifra.contentWindow.xynt_streaming = this;
850 this.watchable = true;
851 this.watchdog_ct = 0;
852 this.log("hs::watchdog: watchable = yes");
856 if ( (this.watchdog_ct % this.watchdog_checktm) == 0) {
857 this.log("hs::watchdog: this.keepalive_old: "+this.keepalive_old+" this.keepalive_new: "+this.keepalive_new);
858 if (this.keepalive_old == this.keepalive_new) {
859 this.keepalives_equal++;
862 this.keepalive_old = this.keepalive_new;
863 this.keepalives_equal = 0;
866 if (this.keepalives_equal >= this.keepalives_eq_max) {
867 this.log("hs::watchdog: MAX ACHIEVED "+this.keepalives_equal);
869 // alert("watchdog return reload");
875 // PICK COMMANDS FROM STREAM
877 // alert("do--while begin ["+again+"]");
878 // CHECK: maybe again here isn't needed
881 /* if (typeof(this.ifra.contentWindow.ctx_new) == 'undefined' ||
882 typeof(this.ifra.contentWindow.ctx_old_len) == 'undefined') */
883 if (!this.transp.ctx_new_is_set() || !this.transp.ctx_old_len_is_set())
890 // ctx_new_len = this.ifra.contentWindow.ctx_new.length;
891 ctx_new_len = this.transp.ctx_new_curlen_get();
892 // if (ctx_new_len <= this.ifra.contentWindow.ctx_old_len) {
893 if (ctx_new_len <= this.transp.ctx_old_len_get()) {
896 this.log("new: "+ ctx_new_len + " old: "+this.transp.ctx_old_len_get());
897 this.keepalive_new++;
898 // alert("pre-loop 1");
899 for (i = this.transp.ctx_old_len_get() ; i < ctx_new_len ; i++) {
900 // if (this.ifra.contentWindow.ctx_new.charAt(i) != '_') {
901 if (this.transp.ctx_new_getchar(i) != '_') {
902 // this.log("ctx_new.char(i) != '_' ["+this.ifra.contentWindow.ctx_new.charAt(i)+"]");
909 // this.log("ctx_new.charAt(i) == '_'");
912 // this.ifra.contentWindow.ctx_old_len = i;
913 this.transp.ctx_old_len_set(i);
914 if (i == ctx_new_len) {
915 this.log("old_len == i");
919 this.log("old_len != i: "+i);
921 // alert("do--while middle ["+this.ifra.contentWindow.ctx_old_len+"]");
923 comm_newpart = this.transp.new_part();
924 this.log("COM_NEWPART: ["+comm_newpart+"]");
926 comm_arr = comm_newpart.match(this.comm_match);
928 // alert("do--while middle2 ["+again+"]");
930 var comm_arr_len = comm_arr.length;
931 for (i = 0 ; i < comm_arr_len ; i++) {
932 var temp = comm_arr[i].replace(this.comm_clean,"$1").split("|");
933 this.gst.comms = this.gst.comms.concat(temp);
934 comm_len += comm_arr[i].length;
940 // this.ifra.contentWindow.ctx_old_len += comm_len;
941 this.transp.ctx_old_len_add(comm_len);
942 // this.ifra.contentWindow.script_clean = this.gst.st;
943 this.transp.scrcls_set(this.gst.st);
944 // alert("do--while end ["+again+"]");
947 // alert("post while");
948 // EXECUTION OF STREAM COMMANDS
951 //MOP ?? xhrrestart = 0;
952 if (this.gst.st_loc < this.gst.st_loc_new) {
953 // there is some slow actions running
956 else if (this.gst.comms.length > 0) {
959 singlecomm = this.gst.comms.shift();
960 // alert("EXE"+gugu);
961 // $("xhrdeltalog").innerHTML = "EVALL: "+singlecomm.replace("<", "<", "g"); +"<br>";
964 // alert("SINGLE: ["+singlecomm+"]");
965 // window.console.log("["+singlecomm+"]");
966 this.cmdproc(singlecomm);
967 if (this.transp_type_cur) {
968 this.transp_type = this.transp_type_cur;
969 this.transp_port = this.transp_port_cur;
976 var date = new Date();
977 if (date.getTime() > (this.start_time + this.restart_wait)) {
978 this.transp.postproc();
980 this.watchdog_hdl = setTimeout(function(obj) { /* obj.log("tout2"); */ obj.watchdog(); }, this.watchdog_timeout, this);
983 // alert("watchdog return normal");
988 send: function(msg) {
989 if (typeof(this.transp.send) == 'undefined') {
990 this.log('send not implemented for ' + this.transp_type);
994 return this.transp.send(msg);
998 // moved to xynt-streaming-ifra as push()
1000 // keepalive: function (s) {
1001 // this.log("hs::keepalive");
1004 // this.ifra.contentWindow.ctx_new += "@BEGIN@"+s+"@END@";
1007 // this.ifra.contentWindow.ctx_new += "_";
1009 // // this.keepalive_new++;
1012 abort: function () { /* public */
1013 // this.log("PATH: "+this.ifra.contentWindow.location.protocol + "://" + this.ifra.contentWindow.location.host + "/" + this.ifra.contentWindow.location.pathname);
1016 if (this.watchdog_hdl != null) {
1017 clearTimeout(this.watchdog_hdl);
1018 this.watchdog_hdl = null;
1022 this.log("hs::reload");
1023 this.watchable = false;
1024 if (this.transp != null) {
1025 this.transp.destroy();
1031 reload: function () {
1037 if (this.console != null) {
1038 return (this.console.log(s));