1 // old targetpage == page and moved into start method
6 function transport_ws(doc, xynt_streaming, page)
13 this.xynt_streaming = xynt_streaming;
15 this.xynt_streaming.log("PAGE: "+page);
16 this.ws = new WebSocket(page);
17 this.ws.onopen = function () {
18 self.xynt_streaming.log("onopen");
19 if (this.readyState == 1) {
25 this.ws.onmessage = function (msg) {
26 self.xynt_streaming.log("onmessage");
27 // new data in msg.data
28 self.ctx_new += msg.data;
30 this.ws.onclose = function (msg) {
34 self.xynt_streaming.log("onclose"+self.init_steps);
35 if (self.init_steps == 0)
40 this.ws.onerror = function () {
45 self.xynt_streaming.log("onerror");
50 throw "websocket creation failed";
56 transport_ws.prototype = {
58 xynt_streaming: "ready",
71 destroy: function () { /* public */
72 if (this.ws != null) {
78 ws_cb: function (from) {
81 if (from == "error") {
82 if (this.xynt_streaming != "ready") {
83 if (this.xynt_streaming.transp_fback > 0) {
84 this.xynt_streaming.log("DEC: "+this.xynt_streaming.transp_fback);
85 this.xynt_streaming.transp_fback--;
87 this.xynt_streaming.reload();
91 if (this.ws != null && this.ws.readyState > 1) {
96 ws_abort: function() {
97 if (this.ws != null) {
98 this.xynt_streaming.log("WSCLOSE");
103 xstr_is_init: function () { /* public */
107 /* only after a successfull is_initialized call */
108 xstr_is_ready: function () { /* public */
109 return (this.ws.readyState == 1);
112 xstr_set: function () { /* public */
116 ctx_new_is_set: function () { /* public */
117 return (this.ctx_new != null);
120 ctx_new_curlen_get: function () { /* public */
121 return (this.ctx_new.length);
124 ctx_new_getchar: function(idx) { /* public */
125 return (this.ctx_new[idx]);
128 ctx_old_len_is_set: function () { /* public */
132 ctx_old_len_get: function () { /* public */
133 return (this.ctx_old_len);
136 ctx_old_len_set: function (len) { /* public */
137 this.ctx_old_len = len;
140 ctx_old_len_add: function (len) { /* public */
141 this.ctx_old_len += len;
144 new_part: function () { /* public */
145 return (this.ctx_new.substr(this.ctx_old_len));
148 scrcls_set: function (step) { /* public */
149 // this.script_clean = step;
152 postproc: function () {
153 if (this.stopped && !this.xstr_is_ready()) {
154 this.xynt_streaming.reload();
160 // CLASS transport_xhr
162 function transport_xhr(doc, xynt_streaming, page)
165 this.xynt_streaming = xynt_streaming;
166 this.xhr = createXMLHttpRequest();
167 this.xhr.open('GET', page);
170 this.xhr.onreadystatechange = function () { self.xhr_cb(); };
173 this.stopped = false;
176 transport_xhr.prototype = {
178 xynt_streaming: "ready",
188 destroy: function () { /* public */
189 if (this.xhr != null) {
195 xhr_cb: function () {
198 if (this.xhr.readyState == 4) {
199 // console.log("SS: "+safestatus(xhr));
201 // NOTE: delay management later
203 // if ((ret = safestatus(this.xhr)) == 200) {
205 // // console.log("del a null "+this.delayed);
206 // } else if (ret != -1) {
207 // this.delay = 5000;
209 // // alert('There was a problem with the request.' + ret);
213 // this.delayed = null;
218 xhr_abort: function() {
219 if (this.xhr != null) {
224 xstr_is_init: function () { /* public */
226 if (this.xhr.responseText != null) {
227 this.ctx_new = this.xhr.responseText;
233 return (this.ctx_new != null);
236 /* only after a successfull is_initialized call */
237 xstr_is_ready: function () { /* public */
238 return (this.xynt_streaming == "ready");
241 xstr_set: function () { /* public */
245 ctx_new_is_set: function () { /* public */
246 return (this.ctx_new != null);
249 ctx_new_curlen_get: function () { /* public */
250 return (this.ctx_new.length);
253 ctx_new_getchar: function(idx) { /* public */
254 return (this.ctx_new[idx]);
257 ctx_old_len_is_set: function () { /* public */
261 ctx_old_len_get: function () { /* public */
262 return (this.ctx_old_len);
265 ctx_old_len_set: function (len) { /* public */
266 this.ctx_old_len = len;
269 ctx_old_len_add: function (len) { /* public */
270 this.ctx_old_len += len;
273 new_part: function () { /* public */
274 return (this.ctx_new.substr(this.ctx_old_len));
277 scrcls_set: function (step) { /* public */
278 // this.script_clean = step;
281 postproc: function () {
282 if (this.stopped && !this.xstr_is_ready()) {
283 this.xynt_streaming.reload();
289 // CLASS transport_htmlfile
291 function transport_htmlfile(doc, xynt_streaming, page)
294 this.xynt_streaming = xynt_streaming;
295 this.transfdoc = new ActiveXObject("htmlfile");
296 this.transfdoc.open();
297 this.transfdoc.write("<html><body><iframe id='iframe'></iframe></body></html>");
298 this.transfdoc.close();
300 this.ifra = this.transfdoc.getElementById("iframe");
301 this.ifra.contentWindow.location.href = page;
302 this.stopped = false;
305 transport_htmlfile.prototype = {
307 xynt_streaming: null,
312 destroy: function () { /* public */
313 if (this.ifra != null) {
314 // this.doc.body.removeChild(this.ifra);
319 if (this.transfdoc) {
320 delete this.transfdoc;
321 this.transfdoc = null;
325 xstr_is_init: function () { /* public */
326 return (typeof(this.ifra.contentWindow.xynt_streaming) != 'undefined');
329 /* only after a successfull is_initialized call */
330 xstr_is_ready: function () { /* public */
331 return (this.ifra.contentWindow.xynt_streaming == "ready");
334 /* only after a successfull is_initialized call */
335 xstr_set: function () { /* public */
336 if (this.ifra.contentWindow.xynt_streaming == "ready") {
337 this.ifra.contentWindow.xynt_streaming = this.xynt_streaming;
340 else if (this.ifra.contentWindow.xynt_streaming == this.xynt_streaming) {
348 ctx_new_is_set: function () { /* public */
349 return (typeof(this.ifra.contentWindow.ctx_new) != 'undefined');
352 ctx_new_curlen_get: function () { /* public */
353 return (this.ifra.contentWindow.ctx_new.length);
356 ctx_new_getchar: function(idx) { /* public */
357 return (this.ifra.contentWindow.ctx_new.charAt(idx));
360 ctx_old_len_is_set: function () { /* public */
361 return (typeof(this.ifra.contentWindow.ctx_old_len) != 'undefined');
364 ctx_old_len_get: function () { /* public */
365 return (this.ifra.contentWindow.ctx_old_len);
368 ctx_old_len_set: function (len) { /* public */
369 this.ifra.contentWindow.ctx_old_len = len;
372 ctx_old_len_add: function (len) { /* public */
373 this.ifra.contentWindow.ctx_old_len += len;
376 new_part: function () { /* public */
377 return (this.ifra.contentWindow.ctx_new.substr(this.ifra.contentWindow.ctx_old_len));
380 scrcls_set: function (step) { /* public */
381 this.ifra.contentWindow.script_clean = step;
384 postproc: function () { /* public */
385 if (this.stopped && !this.xstr_is_ready()) {
386 this.xynt_streaming.reload();
392 // CLASS transport_iframe
394 function transport_iframe(doc, xynt_streaming, page)
397 this.xynt_streaming = xynt_streaming;
398 this.ifra = doc.createElement("iframe");
399 this.ifra.style.visibility = "hidden";
400 doc.body.appendChild(this.ifra);
401 this.ifra.contentWindow.location.href = page;
402 this.stopped = false;
405 transport_iframe.prototype = {
407 xynt_streaming: null,
411 destroy: function () { /* public */
413 if (this.ifra != null) {
414 // NOTE: on Opera this remove child crash js if called from
415 // inside of the iframe, on IE on Windows without
416 // it stream abort fails.
417 // the problem is fixed setting into the iframe's onload
418 // function the stopped attribute to true and delegate
419 // postproc() fired by xynt_streaming watchdog()
420 this.doc.body.removeChild(this.ifra);
425 alert("destroy exception catched");
429 xstr_is_init: function () { /* public */
430 return (typeof(this.ifra.contentWindow.xynt_streaming) != 'undefined');
433 /* only after a successfull is_initialized call */
434 xstr_is_ready: function () { /* public */
435 return (this.ifra.contentWindow.xynt_streaming == "ready");
438 /* only after a successfull is_initialized call */
439 xstr_set: function () { /* public */
440 if (this.ifra.contentWindow.xynt_streaming == "ready") {
441 this.ifra.contentWindow.xynt_streaming = this.xynt_streaming;
444 else if (this.ifra.contentWindow.xynt_streaming == this.xynt_streaming) {
453 /* only after a successfull is_ready call to be sure the accessibility of the var */
454 xstr_set_old: function (xynt_streaming) { /* public */
455 this.ifra.contentWindow.xynt_streaming = xynt_streaming;
458 ctx_new_is_set: function () { /* public */
459 return (typeof(this.ifra.contentWindow.ctx_new) != 'undefined');
462 ctx_new_curlen_get: function () { /* public */
463 return (this.ifra.contentWindow.ctx_new.length);
466 ctx_new_getchar: function(idx) { /* public */
467 return (this.ifra.contentWindow.ctx_new.charAt(idx));
470 ctx_old_len_is_set: function () { /* public */
471 return (typeof(this.ifra.contentWindow.ctx_old_len) != 'undefined');
474 ctx_old_len_get: function () { /* public */
475 return (this.ifra.contentWindow.ctx_old_len);
478 ctx_old_len_set: function (len) { /* public */
479 this.ifra.contentWindow.ctx_old_len = len;
482 ctx_old_len_add: function (len) { /* public */
483 this.ifra.contentWindow.ctx_old_len += len;
486 new_part: function () { /* public */
487 return (this.ifra.contentWindow.ctx_new.substr(this.ifra.contentWindow.ctx_old_len));
490 scrcls_set: function (step) { /* public */
491 this.ifra.contentWindow.script_clean = step;
494 postproc: function () { /* public */
495 if (this.stopped && !this.xstr_is_ready()) {
496 this.xynt_streaming.reload();
501 function xynt_streaming(win, transp_type, transp_port, transp_fback, console, gst, from, cookiename, sess, sandbox, page, cmdproc)
504 this.transp_type = transp_type;
505 this.transp_port = transp_port;
506 this.transp_fback = transp_fback;
507 this.console = console;
510 this.cookiename = cookiename;
512 this.sandbox = sandbox;
514 this.cmdproc = cmdproc;
515 // this.cmdproc = function(com){/* console.log("COM: "+com); */ eval(com);}
516 this.doc = win.document;
517 this.keepalive_old = -1;
518 this.keepalive_new = -1;
520 this.mon_errtime = this.keepalives_eq_max * this.watchdog_checktm * this.watchdog_timeout;
521 this.mon_wrntime = this.mon_errtime / 2;
526 xynt_streaming.prototype = {
542 restart_wait: 5000, // wait restart_wait millisec before begin to check if restart is needed
545 cookiepath: "/brisk/",
547 hbit: function () {},
551 /* NOTE: right watch_timeout value to 100, for devel reasons use 1000 or more */
552 /* restart after 4 * 40 * 100 millisec if server ping is missing => 16secs */
553 keepalives_eq_max: 4,
554 watchdog_checktm: 40,
555 watchdog_timeout: 100,
559 comm_match: /_*@BEGIN@(.*?)@END@/g,
560 comm_clean: /_*@BEGIN@(.*?)@END@/,
570 mon_update: function()
572 var date = new Date();
574 this.mon_time = date.getTime();
578 ping arrives at least every RD_KEEPALIVE_TOUT secs (currently 4 secs)
580 return values: 0 unknown
585 mon_status: function()
589 var date = new Date();
591 delta = date.getTime() - this.mon_time;
593 if (delta < this.mon_wrntime)
595 else if (delta < this.mon_errtime)
601 start: function() { /* public */
605 // this.log("start "+this.transp_type+" "+this.transp_fback);
609 createCookie(this.cookiename, sess, 24*365, this.cookiepath);
611 this.log("xynt_streaming:start restart: "+this.restart_n);
612 this.keepalives_equal = 0;
615 this.page = url_complete(this.win.location.href, this.page);
617 if (this.transp_fback > 0) {
618 transp_type = "websocket";
619 transp_port = (this.transp_fback == 2 ? 80 : 8080);
622 transp_type = this.transp_type;
623 transp_port = this.transp_port;
626 if (transp_type == "websocket") {
627 var end_proto, first_slash;
630 this.log("precha ["+this.page+"]");
631 end_proto = this.page.indexOf("://");
632 first_slash = this.page.substring(end_proto+3).indexOf("/");
634 page = "ws://" + this.page.substring(end_proto+3, end_proto+3+first_slash) + ":" + transp_port + this.page.substring(end_proto+3 + first_slash);
639 // stat, subst, this.gst.st
641 page = url_append_args(page, "sess", this.sess, "stat", stat, "subst", subst, "step", this.gst.st, "from", this.from);
642 // this.log("the page:");
646 // transport instantiation
647 if (transp_type == "websocket") {
648 page = url_append_args(page, "transp", "websocket");
649 this.transp = new transport_ws(this.doc, this, page);
651 else if (transp_type == "xhr") {
652 page = url_append_args(page, "transp", "xhr");
653 this.transp = new transport_xhr(this.doc, this, page);
655 else if (transp_type == "iframe") {
656 page = url_append_args(page, "transp", "iframe");
657 this.transp = new transport_iframe(this.doc, this, page);
659 else if (transp_type == "htmlfile") {
660 page = url_append_args(page, "transp", "htmlfile");
661 this.transp = new transport_htmlfile(this.doc, this, page);
667 if (this.transp_fback > 0) {
675 this.watchdog_ct = 0;
677 this.watchdog_hdl = setTimeout(function(obj) { obj.log("tout1"); obj.watchdog(); }, this.watchdog_timeout, this);
680 var date = new Date();
681 this.start_time = date.getTime();
689 hbit_set: function (hbit) {
694 hbit_status: function () {
697 ret = this.mon_status();
698 // console.log("mon_status: "+ret+" 0: "+this.mon_time);
714 if (this.mon_stat_old != mon_stat) {
716 this.mon_stat_old = mon_stat;
721 watchdog: function () {
722 // alert("watchdog");
724 var comm_newpart, comm_len, comm_arr;
727 if (this.sandbox != null) {
728 // from old: var zug = "POLL sess = "+sess+" stat = "+stat+" subst = "+subst+" step = "+this.gst.st+" step_loc = "+this.gst.st_loc+" step_loc_new = "+this.gst.st_loc_new+" STOP: "+this.stopped;
729 var zug = "WATCHDOG sess = ["+this.sess+"] step = "+this.gst.st+" step_loc = "+this.gst.st_loc+" step_loc_new = "+this.gst.st_loc_new;
730 if (zug != this.sandbox.innerHTML)
731 this.sandbox.innerHTML = zug;
734 // WATCHDOGING THE CONNECTION
735 this.log("hs::watchdog: start, cur equal times: "+this.keepalives_equal);
736 if (!this.watchable) {
739 // if (typeof(this.ifra.contentWindow.xynt_streaming) == 'undefined')
740 if (!this.transp.xstr_is_init()) {
741 this.log("hs::watchdog: xstr_is_init = false");
746 this.log("hs::watchdog: exception");
751 on IE7 the the window frame scope is cleaned after the href is set, so we wait
752 for a well know variable value before assign this object value to it (OO is a passion)
754 // if (this.ifra.contentWindow.xynt_streaming == "ready") {
755 if (this.transp.xstr_set()) {
756 // this.ifra.contentWindow.xynt_streaming = this;
757 this.watchable = true;
758 this.watchdog_ct = 0;
759 this.log("hs::watchdog: watchable = yes");
763 if ( (this.watchdog_ct % this.watchdog_checktm) == 0) {
764 this.log("hs::watchdog: this.keepalive_old: "+this.keepalive_old+" this.keepalive_new: "+this.keepalive_new);
765 if (this.keepalive_old == this.keepalive_new) {
766 this.keepalives_equal++;
769 this.keepalive_old = this.keepalive_new;
770 this.keepalives_equal = 0;
773 if (this.keepalives_equal >= this.keepalives_eq_max) {
774 this.log("hs::watchdog: MAX ACHIEVED "+this.keepalives_equal);
776 // alert("watchdog return reload");
782 // PICK COMMANDS FROM STREAM
784 // alert("do--while begin ["+again+"]");
785 // CHECK: maybe again here isn't needed
788 /* if (typeof(this.ifra.contentWindow.ctx_new) == 'undefined' ||
789 typeof(this.ifra.contentWindow.ctx_old_len) == 'undefined') */
790 if (!this.transp.ctx_new_is_set() || !this.transp.ctx_old_len_is_set())
797 // ctx_new_len = this.ifra.contentWindow.ctx_new.length;
798 ctx_new_len = this.transp.ctx_new_curlen_get();
799 // if (ctx_new_len <= this.ifra.contentWindow.ctx_old_len) {
800 if (ctx_new_len <= this.transp.ctx_old_len_get()) {
803 this.log("new: "+ ctx_new_len + " old: "+this.transp.ctx_old_len_get());
804 this.keepalive_new++;
805 // alert("pre-loop 1");
806 for (i = this.transp.ctx_old_len_get() ; i < ctx_new_len ; i++) {
807 // if (this.ifra.contentWindow.ctx_new.charAt(i) != '_') {
808 if (this.transp.ctx_new_getchar(i) != '_') {
809 // this.log("ctx_new.char(i) != '_' ["+this.ifra.contentWindow.ctx_new.charAt(i)+"]");
816 // this.log("ctx_new.charAt(i) == '_'");
819 // this.ifra.contentWindow.ctx_old_len = i;
820 this.transp.ctx_old_len_set(i);
821 if (i == ctx_new_len) {
822 this.log("old_len == i");
826 this.log("old_len != i: "+i);
828 // alert("do--while middle ["+this.ifra.contentWindow.ctx_old_len+"]");
830 comm_newpart = this.transp.new_part();
831 this.log("COM_NEWPART: ["+comm_newpart+"]");
833 comm_arr = comm_newpart.match(this.comm_match);
835 // alert("do--while middle2 ["+again+"]");
837 var comm_arr_len = comm_arr.length;
838 for (i = 0 ; i < comm_arr_len ; i++) {
839 var temp = comm_arr[i].replace(this.comm_clean,"$1").split("|");
840 this.gst.comms = this.gst.comms.concat(temp);
841 comm_len += comm_arr[i].length;
847 // this.ifra.contentWindow.ctx_old_len += comm_len;
848 this.transp.ctx_old_len_add(comm_len);
849 // this.ifra.contentWindow.script_clean = this.gst.st;
850 this.transp.scrcls_set(this.gst.st);
851 // alert("do--while end ["+again+"]");
854 // alert("post while");
855 // EXECUTION OF STREAM COMMANDS
858 //MOP ?? xhrrestart = 0;
859 if (this.gst.st_loc < this.gst.st_loc_new) {
860 // there is some slow actions running
863 else if (this.gst.comms.length > 0) {
866 singlecomm = this.gst.comms.shift();
867 // alert("EXE"+gugu);
868 // $("xhrdeltalog").innerHTML = "EVALL: "+singlecomm.replace("<", "<", "g"); +"<br>";
871 // alert("SINGLE: ["+singlecomm+"]");
872 this.cmdproc(singlecomm);
878 var date = new Date();
879 if (date.getTime() > (this.start_time + this.restart_wait)) {
880 this.transp.postproc();
882 this.watchdog_hdl = setTimeout(function(obj) { /* obj.log("tout2"); */ obj.watchdog(); }, this.watchdog_timeout, this);
885 // alert("watchdog return normal");
891 // moved to xynt-streaming-ifra as push()
893 // keepalive: function (s) {
894 // this.log("hs::keepalive");
897 // this.ifra.contentWindow.ctx_new += "@BEGIN@"+s+"@END@";
900 // this.ifra.contentWindow.ctx_new += "_";
902 // // this.keepalive_new++;
905 abort: function () { /* public */
906 // this.log("PATH: "+this.ifra.contentWindow.location.protocol + "://" + this.ifra.contentWindow.location.host + "/" + this.ifra.contentWindow.location.pathname);
909 if (this.watchdog_hdl != null) {
910 clearTimeout(this.watchdog_hdl);
911 this.watchdog_hdl = null;
915 this.log("hs::reload");
916 this.watchable = false;
917 if (this.transp != null) {
918 this.transp.destroy();
924 reload: function () {
930 if (this.console != null) {
931 return (this.console.log(s));