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";
23 this.xynt_streaming = xynt_streaming;
25 this.xynt_streaming.log("PAGE: "+page);
26 this.ws = new WebSocket(page);
27 this.ws.onopen = function () {
28 console.log('WS On open');
30 self.xynt_streaming.log("onopen");
31 if (this.readyState == 1) {
37 this.ws.onmessage = function (msg) {
38 console.log('WS On message');
39 self.xynt_streaming.log("onmessage");
40 // new data in msg.data
41 self.ctx_new += msg.data;
43 this.ws.onclose = function (msg) {
44 console.log('WS On close');
48 self.xynt_streaming.log("onclose"+self.init_steps);
49 if (self.init_steps == 0)
54 this.ws.onerror = function () {
59 self.xynt_streaming.log("onerror");
64 throw "websocket creation failed";
70 transport_ws.prototype = {
73 xynt_streaming: "ready",
86 destroy: function () { /* public */
87 if (this.ws != null) {
93 ws_cb: function (from) {
96 if (from == "error") {
97 if (this.xynt_streaming != "ready") {
98 if (this.xynt_streaming.transp_fback > 0) {
99 this.xynt_streaming.log("DEC: "+this.xynt_streaming.transp_fback);
100 this.xynt_streaming.transp_fback--;
102 this.xynt_streaming.reload();
106 else if (from == "open") {
107 this.flush_out_queue();
110 if (this.ws != null && this.ws.readyState > 1) {
115 flush_out_queue: function() {
116 var l_out = this.xynt_streaming.out_queue.length;
121 console.log('flush_out_queue: ' + l_out);
122 for (var i = 0 ; i < l_out ; i++) {
123 if (this.ws.readyState != 1) {
126 var item = this.xynt_streaming.out_queue.shift();
132 this.xynt_streaming.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.xynt_streaming.out_queue.push(msg);
153 console.log('ws not ready: push into out_queue');
154 this.xynt_streaming.out_queue.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;
589 this.mon_errtime = this.keepalives_eq_max * this.watchdog_checktm * this.watchdog_timeout;
590 this.mon_wrntime = this.mon_errtime / 2;
595 xynt_streaming.prototype = {
599 transp_type_cur: null,
613 restart_wait: 5000, // wait restart_wait millisec before begin to check if restart is needed
616 cookiepath: "/brisk/",
618 hbit: function () {},
622 /* NOTE: right watch_timeout value to 100, for devel reasons use 1000 or more */
623 /* restart after 4 * 40 * 100 millisec if server ping is missing => 16secs */
624 keepalives_eq_max: 4,
625 watchdog_checktm: 40,
626 watchdog_timeout: 100,
630 comm_match: /_*@BEGIN@(.*?)@END@/g,
631 comm_clean: /_*@BEGIN@(.*?)@END@/,
642 mon_update: function()
644 var date = new Date();
646 this.mon_time = date.getTime();
650 ping arrives at least every RD_KEEPALIVE_TOUT secs (currently 4 secs)
652 return values: 0 unknown
657 mon_status: function()
661 var date = new Date();
663 delta = date.getTime() - this.mon_time;
665 if (delta < this.mon_wrntime)
667 else if (delta < this.mon_errtime)
673 start: function() { /* public */
677 // this.log("start "+this.transp_type+" "+this.transp_fback);
681 createCookie(this.cookiename, sess, 24*365, this.cookiepath);
683 this.log("xynt_streaming:start restart: "+this.restart_n);
684 this.keepalives_equal = 0;
687 this.page = url_complete(this.win.location.href, this.page);
689 // DEFAULT TRANSPORT PROTOCOL HERE websocketsec, websocket
690 if (this.transp_fback > 0) {
691 if (location.protocol == 'https:') {
692 transp_type = "websocketsec";
696 transp_type = "websocket";
697 transp_port = (this.transp_fback == 2 ? 80 : 8080);
702 transp_type = this.transp_type;
703 transp_port = this.transp_port;
706 this.transp_type_cur = transp_type;
707 this.transp_port_cur = transp_port;
709 if (transp_type == "websocket" || transp_type == "websocketsec") {
710 var end_proto, first_slash, newpage;
713 this.log("precha ["+this.page+"]");
714 if (transp_type == "websocketsec") {
715 newpage = this.page.replace(/\.php$/g, "_wss.php").replace(/\.php\?/g, "_wss.php?");
720 end_proto = newpage.indexOf("://");
721 first_slash = newpage.substring(end_proto+3).indexOf("/");
723 page = (transp_type == "websocketsec" ? "wss://" : "ws://")
724 + newpage.substring(end_proto+3, end_proto+3 + first_slash) + ":"
725 + transp_port + newpage.substring(end_proto+3 + first_slash);
726 // this.log("MOP WS: " + page);
731 // stat, subst, this.gst.st
733 page = url_append_args(page, "sess", this.sess, "stat", stat, "subst", subst, "step", this.gst.st, "from", this.from);
734 // this.log("the page:");
738 // transport instantiation
739 if (transp_type == "websocketsec") {
740 page = url_append_args(page, "transp", "websocketsec");
741 this.transp = new transport_ws(this.doc, this, page, true);
743 else if (transp_type == "websocket") {
744 page = url_append_args(page, "transp", "websocket");
745 this.transp = new transport_ws(this.doc, this, page);
747 else if (transp_type == "xhr") {
748 page = url_append_args(page, "transp", "xhr");
749 this.transp = new transport_xhr(this.doc, this, page);
751 else if (transp_type == "iframe") {
752 page = url_append_args(page, "transp", "iframe");
753 this.transp = new transport_iframe(this.doc, this, page);
755 else if (transp_type == "htmlfile") {
756 page = url_append_args(page, "transp", "htmlfile");
757 this.transp = new transport_htmlfile(this.doc, this, page);
763 if (this.transp_fback > 0) {
771 this.watchdog_ct = 0;
773 this.watchdog_hdl = setTimeout(function(obj) { obj.log("tout1"); obj.watchdog(); }, this.watchdog_timeout, this);
776 var date = new Date();
777 this.start_time = date.getTime();
785 hbit_set: function (hbit) {
790 hbit_status: function () {
793 ret = this.mon_status();
794 // console.log("mon_status: "+ret+" 0: "+this.mon_time);
810 if (this.mon_stat_old != mon_stat) {
812 this.mon_stat_old = mon_stat;
817 watchdog: function () {
818 // alert("watchdog");
820 var comm_newpart, comm_len, comm_arr;
823 if (this.sandbox != null) {
824 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;
825 if (zug != this.sandbox.innerHTML)
826 this.sandbox.innerHTML = zug;
829 // WATCHDOGING THE CONNECTION
830 this.log("hs::watchdog: start, cur equal times: "+this.keepalives_equal);
831 if (!this.watchable) {
834 // if (typeof(this.ifra.contentWindow.xynt_streaming) == 'undefined')
835 if (!this.transp.xstr_is_init()) {
836 this.log("hs::watchdog: xstr_is_init = false");
841 this.log("hs::watchdog: exception");
846 on IE7 the the window frame scope is cleaned after the href is set, so we wait
847 for a well know variable value before assign this object value to it (OO is a passion)
849 // if (this.ifra.contentWindow.xynt_streaming == "ready") {
850 if (this.transp.xstr_set()) {
851 // this.ifra.contentWindow.xynt_streaming = this;
852 this.watchable = true;
853 this.watchdog_ct = 0;
854 this.log("hs::watchdog: watchable = yes");
858 if ( (this.watchdog_ct % this.watchdog_checktm) == 0) {
859 this.log("hs::watchdog: this.keepalive_old: "+this.keepalive_old+" this.keepalive_new: "+this.keepalive_new);
860 if (this.keepalive_old == this.keepalive_new) {
861 this.keepalives_equal++;
864 this.keepalive_old = this.keepalive_new;
865 this.keepalives_equal = 0;
868 if (this.keepalives_equal >= this.keepalives_eq_max) {
869 this.log("hs::watchdog: MAX ACHIEVED "+this.keepalives_equal);
871 // alert("watchdog return reload");
877 // PICK COMMANDS FROM STREAM
879 // alert("do--while begin ["+again+"]");
880 // CHECK: maybe again here isn't needed
883 /* if (typeof(this.ifra.contentWindow.ctx_new) == 'undefined' ||
884 typeof(this.ifra.contentWindow.ctx_old_len) == 'undefined') */
885 if (!this.transp.ctx_new_is_set() || !this.transp.ctx_old_len_is_set())
892 // ctx_new_len = this.ifra.contentWindow.ctx_new.length;
893 ctx_new_len = this.transp.ctx_new_curlen_get();
894 // if (ctx_new_len <= this.ifra.contentWindow.ctx_old_len) {
895 if (ctx_new_len <= this.transp.ctx_old_len_get()) {
898 this.log("new: "+ ctx_new_len + " old: "+this.transp.ctx_old_len_get());
899 this.keepalive_new++;
900 // alert("pre-loop 1");
901 for (i = this.transp.ctx_old_len_get() ; i < ctx_new_len ; i++) {
902 // if (this.ifra.contentWindow.ctx_new.charAt(i) != '_') {
903 if (this.transp.ctx_new_getchar(i) != '_') {
904 // this.log("ctx_new.char(i) != '_' ["+this.ifra.contentWindow.ctx_new.charAt(i)+"]");
911 // this.log("ctx_new.charAt(i) == '_'");
914 // this.ifra.contentWindow.ctx_old_len = i;
915 this.transp.ctx_old_len_set(i);
916 if (i == ctx_new_len) {
917 this.log("old_len == i");
921 this.log("old_len != i: "+i);
923 // alert("do--while middle ["+this.ifra.contentWindow.ctx_old_len+"]");
925 comm_newpart = this.transp.new_part();
926 this.log("COM_NEWPART: ["+comm_newpart+"]");
928 comm_arr = comm_newpart.match(this.comm_match);
930 // alert("do--while middle2 ["+again+"]");
932 var comm_arr_len = comm_arr.length;
933 for (i = 0 ; i < comm_arr_len ; i++) {
934 var temp = comm_arr[i].replace(this.comm_clean,"$1").split("|");
935 this.gst.comms = this.gst.comms.concat(temp);
936 comm_len += comm_arr[i].length;
942 // this.ifra.contentWindow.ctx_old_len += comm_len;
943 this.transp.ctx_old_len_add(comm_len);
944 // this.ifra.contentWindow.script_clean = this.gst.st;
945 this.transp.scrcls_set(this.gst.st);
946 // alert("do--while end ["+again+"]");
949 // alert("post while");
950 // EXECUTION OF STREAM COMMANDS
953 //MOP ?? xhrrestart = 0;
954 if (this.gst.st_loc < this.gst.st_loc_new) {
955 // there is some slow actions running
958 else if (this.gst.comms.length > 0) {
961 singlecomm = this.gst.comms.shift();
962 // alert("EXE"+gugu);
963 // $("xhrdeltalog").innerHTML = "EVALL: "+singlecomm.replace("<", "<", "g"); +"<br>";
966 // alert("SINGLE: ["+singlecomm+"]");
967 // window.console.log("["+singlecomm+"]");
968 this.cmdproc(singlecomm);
969 if (this.transp_type_cur) {
970 this.transp_type = this.transp_type_cur;
971 this.transp_port = this.transp_port_cur;
978 var date = new Date();
979 if (date.getTime() > (this.start_time + this.restart_wait)) {
980 this.transp.postproc();
982 this.watchdog_hdl = setTimeout(function(obj) { /* obj.log("tout2"); */ obj.watchdog(); }, this.watchdog_timeout, this);
985 // alert("watchdog return normal");
990 send: function(msg) {
991 if (typeof(this.transp.send) == 'undefined') {
992 this.log('send not implemented for ' + this.transp_type);
996 return this.transp.send(msg);
1000 // moved to xynt-streaming-ifra as push()
1002 // keepalive: function (s) {
1003 // this.log("hs::keepalive");
1006 // this.ifra.contentWindow.ctx_new += "@BEGIN@"+s+"@END@";
1009 // this.ifra.contentWindow.ctx_new += "_";
1011 // // this.keepalive_new++;
1014 abort: function () { /* public */
1015 // this.log("PATH: "+this.ifra.contentWindow.location.protocol + "://" + this.ifra.contentWindow.location.host + "/" + this.ifra.contentWindow.location.pathname);
1018 if (this.watchdog_hdl != null) {
1019 clearTimeout(this.watchdog_hdl);
1020 this.watchdog_hdl = null;
1024 this.log("hs::reload");
1025 this.watchable = false;
1026 if (this.transp != null) {
1027 this.transp.destroy();
1033 reload: function () {
1039 if (this.console != null) {
1040 return (this.console.log(s));