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.ws = new WebSocket(page);
16 this.ws.onopen = function () {
17 self.xynt_streaming.log("onopen");
18 if (this.readyState == 1) {
23 this.ws.onmessage = function (msg) {
24 self.xynt_streaming.log("onmessage");
25 // new data in msg.data
26 self.ctx_new += msg.data;
28 this.ws.onclose = function (msg) {
29 self.xynt_streaming.log("onclose");
32 this.ws.onerror = function () {
34 self.xynt_streaming.log("onerror");
39 throw "websocket creation failed";
45 transport_ws.prototype = {
47 xynt_streaming: "ready",
58 destroy: function () { /* public */
59 if (this.ws != null) {
65 ws_cb: function (from) {
68 if (from == "error") {
69 if (this.xynt_streaming != "ready") {
70 if (this.xynt_streaming.transp_fback > 0) {
71 this.xynt_streaming.transp_fback--;
73 this.xynt_streaming.reload();
77 if (this.ws != null && this.ws.readyState > 1) {
82 ws_abort: function() {
83 if (this.ws != null) {
88 xstr_is_init: function () { /* public */
92 /* only after a successfull is_initialized call */
93 xstr_is_ready: function () { /* public */
94 return (this.ws.readyState == 1);
97 xstr_set: function () { /* public */
101 ctx_new_is_set: function () { /* public */
102 return (this.ctx_new != null);
105 ctx_new_curlen_get: function () { /* public */
106 return (this.ctx_new.length);
109 ctx_new_getchar: function(idx) { /* public */
110 return (this.ctx_new[idx]);
113 ctx_old_len_is_set: function () { /* public */
117 ctx_old_len_get: function () { /* public */
118 return (this.ctx_old_len);
121 ctx_old_len_set: function (len) { /* public */
122 this.ctx_old_len = len;
125 ctx_old_len_add: function (len) { /* public */
126 this.ctx_old_len += len;
129 new_part: function () { /* public */
130 return (this.ctx_new.substr(this.ctx_old_len));
133 scrcls_set: function (step) { /* public */
134 // this.script_clean = step;
137 postproc: function () {
138 if (this.stopped && !this.xstr_is_ready()) {
139 this.xynt_streaming.reload();
145 // CLASS transport_xhr
147 function transport_xhr(doc, xynt_streaming, page)
150 this.xynt_streaming = xynt_streaming;
151 this.xhr = createXMLHttpRequest();
152 this.xhr.open('GET', page);
155 this.xhr.onreadystatechange = function () { self.xhr_cb(); };
158 this.stopped = false;
161 transport_xhr.prototype = {
163 xynt_streaming: "ready",
173 destroy: function () { /* public */
174 if (this.xhr != null) {
180 xhr_cb: function () {
183 if (this.xhr.readyState == 4) {
184 // console.log("SS: "+safestatus(xhr));
186 // NOTE: delay management later
188 // if ((ret = safestatus(this.xhr)) == 200) {
190 // // console.log("del a null "+this.delayed);
191 // } else if (ret != -1) {
192 // this.delay = 5000;
194 // // alert('There was a problem with the request.' + ret);
198 // this.delayed = null;
203 xhr_abort: function() {
204 if (this.xhr != null) {
209 xstr_is_init: function () { /* public */
211 if (this.xhr.responseText != null) {
212 this.ctx_new = this.xhr.responseText;
218 return (this.ctx_new != null);
221 /* only after a successfull is_initialized call */
222 xstr_is_ready: function () { /* public */
223 return (this.xynt_streaming == "ready");
226 xstr_set: function () { /* public */
230 ctx_new_is_set: function () { /* public */
231 return (this.ctx_new != null);
234 ctx_new_curlen_get: function () { /* public */
235 return (this.ctx_new.length);
238 ctx_new_getchar: function(idx) { /* public */
239 return (this.ctx_new[idx]);
242 ctx_old_len_is_set: function () { /* public */
246 ctx_old_len_get: function () { /* public */
247 return (this.ctx_old_len);
250 ctx_old_len_set: function (len) { /* public */
251 this.ctx_old_len = len;
254 ctx_old_len_add: function (len) { /* public */
255 this.ctx_old_len += len;
258 new_part: function () { /* public */
259 return (this.ctx_new.substr(this.ctx_old_len));
262 scrcls_set: function (step) { /* public */
263 // this.script_clean = step;
266 postproc: function () {
267 if (this.stopped && !this.xstr_is_ready()) {
268 this.xynt_streaming.reload();
274 // CLASS transport_htmlfile
276 function transport_htmlfile(doc, xynt_streaming, page)
279 this.xynt_streaming = xynt_streaming;
280 this.transfdoc = new ActiveXObject("htmlfile");
281 this.transfdoc.open();
282 this.transfdoc.write("<html><body><iframe id='iframe'></iframe></body></html>");
283 this.transfdoc.close();
285 this.ifra = this.transfdoc.getElementById("iframe");
286 this.ifra.contentWindow.location.href = page;
287 this.stopped = false;
290 transport_htmlfile.prototype = {
292 xynt_streaming: null,
297 destroy: function () { /* public */
298 if (this.ifra != null) {
299 // this.doc.body.removeChild(this.ifra);
304 if (this.transfdoc) {
305 delete this.transfdoc;
306 this.transfdoc = null;
310 xstr_is_init: function () { /* public */
311 return (typeof(this.ifra.contentWindow.xynt_streaming) != 'undefined');
314 /* only after a successfull is_initialized call */
315 xstr_is_ready: function () { /* public */
316 return (this.ifra.contentWindow.xynt_streaming == "ready");
319 /* only after a successfull is_initialized call */
320 xstr_set: function () { /* public */
321 if (this.ifra.contentWindow.xynt_streaming == "ready") {
322 this.ifra.contentWindow.xynt_streaming = this.xynt_streaming;
325 else if (this.ifra.contentWindow.xynt_streaming == this.xynt_streaming) {
333 ctx_new_is_set: function () { /* public */
334 return (typeof(this.ifra.contentWindow.ctx_new) != 'undefined');
337 ctx_new_curlen_get: function () { /* public */
338 return (this.ifra.contentWindow.ctx_new.length);
341 ctx_new_getchar: function(idx) { /* public */
342 return (this.ifra.contentWindow.ctx_new.charAt(idx));
345 ctx_old_len_is_set: function () { /* public */
346 return (typeof(this.ifra.contentWindow.ctx_old_len) != 'undefined');
349 ctx_old_len_get: function () { /* public */
350 return (this.ifra.contentWindow.ctx_old_len);
353 ctx_old_len_set: function (len) { /* public */
354 this.ifra.contentWindow.ctx_old_len = len;
357 ctx_old_len_add: function (len) { /* public */
358 this.ifra.contentWindow.ctx_old_len += len;
361 new_part: function () { /* public */
362 return (this.ifra.contentWindow.ctx_new.substr(this.ifra.contentWindow.ctx_old_len));
365 scrcls_set: function (step) { /* public */
366 this.ifra.contentWindow.script_clean = step;
369 postproc: function () { /* public */
370 if (this.stopped && !this.xstr_is_ready()) {
371 this.xynt_streaming.reload();
377 // CLASS transport_iframe
379 function transport_iframe(doc, xynt_streaming, page)
382 this.xynt_streaming = xynt_streaming;
383 this.ifra = doc.createElement("iframe");
384 this.ifra.style.visibility = "hidden";
385 doc.body.appendChild(this.ifra);
386 this.ifra.contentWindow.location.href = page;
387 this.stopped = false;
390 transport_iframe.prototype = {
392 xynt_streaming: null,
396 destroy: function () { /* public */
398 if (this.ifra != null) {
399 // NOTE: on Opera this remove child crash js if called from
400 // inside of the iframe, on IE on Windows without
401 // it stream abort fails.
402 // the problem is fixed setting into the iframe's onload
403 // function the stopped attribute to true and delegate
404 // postproc() fired by xynt_streaming watchdog()
405 this.doc.body.removeChild(this.ifra);
410 alert("destroy exception catched");
414 xstr_is_init: function () { /* public */
415 return (typeof(this.ifra.contentWindow.xynt_streaming) != 'undefined');
418 /* only after a successfull is_initialized call */
419 xstr_is_ready: function () { /* public */
420 return (this.ifra.contentWindow.xynt_streaming == "ready");
423 /* only after a successfull is_initialized call */
424 xstr_set: function () { /* public */
425 if (this.ifra.contentWindow.xynt_streaming == "ready") {
426 this.ifra.contentWindow.xynt_streaming = this.xynt_streaming;
429 else if (this.ifra.contentWindow.xynt_streaming == this.xynt_streaming) {
438 /* only after a successfull is_ready call to be sure the accessibility of the var */
439 xstr_set_old: function (xynt_streaming) { /* public */
440 this.ifra.contentWindow.xynt_streaming = xynt_streaming;
443 ctx_new_is_set: function () { /* public */
444 return (typeof(this.ifra.contentWindow.ctx_new) != 'undefined');
447 ctx_new_curlen_get: function () { /* public */
448 return (this.ifra.contentWindow.ctx_new.length);
451 ctx_new_getchar: function(idx) { /* public */
452 return (this.ifra.contentWindow.ctx_new.charAt(idx));
455 ctx_old_len_is_set: function () { /* public */
456 return (typeof(this.ifra.contentWindow.ctx_old_len) != 'undefined');
459 ctx_old_len_get: function () { /* public */
460 return (this.ifra.contentWindow.ctx_old_len);
463 ctx_old_len_set: function (len) { /* public */
464 this.ifra.contentWindow.ctx_old_len = len;
467 ctx_old_len_add: function (len) { /* public */
468 this.ifra.contentWindow.ctx_old_len += len;
471 new_part: function () { /* public */
472 return (this.ifra.contentWindow.ctx_new.substr(this.ifra.contentWindow.ctx_old_len));
475 scrcls_set: function (step) { /* public */
476 this.ifra.contentWindow.script_clean = step;
479 postproc: function () { /* public */
480 if (this.stopped && !this.xstr_is_ready()) {
481 this.xynt_streaming.reload();
486 function xynt_streaming(win, transp_type, transp_port, transp_fback, console, gst, from, cookiename, sess, sandbox, page, cmdproc)
489 this.transp_type = transp_type;
490 this.transp_port = transp_port;
491 this.transp_fback = transp_fback;
492 this.console = console;
495 this.cookiename = cookiename;
497 this.sandbox = sandbox;
499 this.cmdproc = cmdproc;
500 // this.cmdproc = function(com){/* console.log("COM: "+com); */ eval(com);}
501 this.doc = win.document;
502 this.keepalive_old = -1;
503 this.keepalive_new = -1;
505 this.mon_errtime = this.keepalives_eq_max * this.watchdog_checktm * this.watchdog_timeout;
506 this.mon_wrntime = this.mon_errtime / 2;
511 xynt_streaming.prototype = {
527 restart_wait: 5000, // wait restart_wait millisec before begin to check if restart is needed
530 cookiepath: "/brisk/",
532 hbit: function () {},
536 /* NOTE: right watch_timeout value to 100, for devel reasons use 1000 or more */
537 /* restart after 4 * 40 * 100 millisec if server ping is missing => 16secs */
538 keepalives_eq_max: 4,
539 watchdog_checktm: 40,
540 watchdog_timeout: 100,
544 comm_match: /_*@BEGIN@(.*?)@END@/g,
545 comm_clean: /_*@BEGIN@(.*?)@END@/,
555 mon_update: function()
557 var date = new Date();
559 this.mon_time = date.getTime();
563 ping arrives at least every RD_KEEPALIVE_TOUT secs (currently 4 secs)
565 return values: 0 unknown
570 mon_status: function()
574 var date = new Date();
576 delta = date.getTime() - this.mon_time;
578 if (delta < this.mon_wrntime)
580 else if (delta < this.mon_errtime)
586 start: function() { /* public */
590 // this.log("start "+this.transp_type+" "+this.transp_fback);
594 createCookie(this.cookiename, sess, 24*365, this.cookiepath);
596 this.log("xynt_streaming:start restart: "+this.restart_n);
597 this.keepalives_equal = 0;
600 this.page = url_complete(this.win.location.href, this.page);
602 if (this.transp_fback > 0) {
603 transp_type = "websocket";
604 transp_port = (this.transp_fback == 2 ? 80 : 8080);
607 transp_type = this.transp_type;
608 transp_port = this.transp_port;
611 if (transp_type == "websocket") {
612 var end_proto, first_slash;
615 this.log("precha ["+this.page+"]");
616 end_proto = this.page.indexOf("://");
617 first_slash = this.page.substring(end_proto+3).indexOf("/");
619 page = "ws://" + this.page.substring(end_proto+3, end_proto+3+first_slash) + ":" + transp_port + this.page.substring(end_proto+3 + first_slash);
624 // stat, subst, this.gst.st
626 page = url_append_args(page, "sess", this.sess, "stat", stat, "subst", subst, "step", this.gst.st, "from", this.from);
627 // this.log("the page:");
631 // transport instantiation
632 if (transp_type == "websocket") {
633 page = url_append_args(page, "transp", "websocket");
634 this.transp = new transport_ws(this.doc, this, page);
636 else if (transp_type == "xhr") {
637 page = url_append_args(page, "transp", "xhr");
638 this.transp = new transport_xhr(this.doc, this, page);
640 else if (transp_type == "iframe") {
641 page = url_append_args(page, "transp", "iframe");
642 this.transp = new transport_iframe(this.doc, this, page);
644 else if (transp_type == "htmlfile") {
645 page = url_append_args(page, "transp", "htmlfile");
646 this.transp = new transport_htmlfile(this.doc, this, page);
652 if (this.transp_fback > 0) {
660 this.watchdog_ct = 0;
662 this.watchdog_hdl = setTimeout(function(obj) { obj.log("tout1"); obj.watchdog(); }, this.watchdog_timeout, this);
665 var date = new Date();
666 this.start_time = date.getTime();
674 hbit_set: function (hbit) {
679 hbit_status: function () {
682 ret = this.mon_status();
683 // console.log("mon_status: "+ret+" 0: "+this.mon_time);
699 if (this.mon_stat_old != mon_stat) {
701 this.mon_stat_old = mon_stat;
706 watchdog: function () {
707 // alert("watchdog");
709 var comm_newpart, comm_len, comm_arr;
712 if (this.sandbox != null) {
713 // 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;
714 var zug = "WATCHDOG sess = ["+this.sess+"] step = "+this.gst.st+" step_loc = "+this.gst.st_loc+" step_loc_new = "+this.gst.st_loc_new;
715 if (zug != this.sandbox.innerHTML)
716 this.sandbox.innerHTML = zug;
719 // WATCHDOGING THE CONNECTION
720 this.log("hs::watchdog: start, cur equal times: "+this.keepalives_equal);
721 if (!this.watchable) {
724 // if (typeof(this.ifra.contentWindow.xynt_streaming) == 'undefined')
725 if (!this.transp.xstr_is_init()) {
726 this.log("hs::watchdog: xstr_is_init = false");
731 this.log("hs::watchdog: exception");
736 on IE7 the the window frame scope is cleaned after the href is set, so we wait
737 for a well know variable value before assign this object value to it (OO is a passion)
739 // if (this.ifra.contentWindow.xynt_streaming == "ready") {
740 if (this.transp.xstr_set()) {
741 // this.ifra.contentWindow.xynt_streaming = this;
742 this.watchable = true;
743 this.watchdog_ct = 0;
744 this.log("hs::watchdog: watchable = yes");
748 if ( (this.watchdog_ct % this.watchdog_checktm) == 0) {
749 this.log("hs::watchdog: this.keepalive_old: "+this.keepalive_old+" this.keepalive_new: "+this.keepalive_new);
750 if (this.keepalive_old == this.keepalive_new) {
751 this.keepalives_equal++;
754 this.keepalive_old = this.keepalive_new;
755 this.keepalives_equal = 0;
758 if (this.keepalives_equal >= this.keepalives_eq_max) {
759 this.log("hs::watchdog: MAX ACHIEVED "+this.keepalives_equal);
761 // alert("watchdog return reload");
767 // PICK COMMANDS FROM STREAM
769 // alert("do--while begin ["+again+"]");
770 // CHECK: maybe again here isn't needed
773 /* if (typeof(this.ifra.contentWindow.ctx_new) == 'undefined' ||
774 typeof(this.ifra.contentWindow.ctx_old_len) == 'undefined') */
775 if (!this.transp.ctx_new_is_set() || !this.transp.ctx_old_len_is_set())
782 // ctx_new_len = this.ifra.contentWindow.ctx_new.length;
783 ctx_new_len = this.transp.ctx_new_curlen_get();
784 // if (ctx_new_len <= this.ifra.contentWindow.ctx_old_len) {
785 if (ctx_new_len <= this.transp.ctx_old_len_get()) {
788 this.log("new: "+ ctx_new_len + " old: "+this.transp.ctx_old_len_get());
789 this.keepalive_new++;
790 // alert("pre-loop 1");
791 for (i = this.transp.ctx_old_len_get() ; i < ctx_new_len ; i++) {
792 // if (this.ifra.contentWindow.ctx_new.charAt(i) != '_') {
793 if (this.transp.ctx_new_getchar(i) != '_') {
794 // this.log("ctx_new.char(i) != '_' ["+this.ifra.contentWindow.ctx_new.charAt(i)+"]");
801 // this.log("ctx_new.charAt(i) == '_'");
804 // this.ifra.contentWindow.ctx_old_len = i;
805 this.transp.ctx_old_len_set(i);
806 if (i == ctx_new_len) {
807 this.log("old_len == i");
811 this.log("old_len != i: "+i);
813 // alert("do--while middle ["+this.ifra.contentWindow.ctx_old_len+"]");
815 comm_newpart = this.transp.new_part();
816 this.log("COM_NEWPART: ["+comm_newpart+"]");
818 comm_arr = comm_newpart.match(this.comm_match);
820 // alert("do--while middle2 ["+again+"]");
822 var comm_arr_len = comm_arr.length;
823 for (i = 0 ; i < comm_arr_len ; i++) {
824 var temp = comm_arr[i].replace(this.comm_clean,"$1").split("|");
825 this.gst.comms = this.gst.comms.concat(temp);
826 comm_len += comm_arr[i].length;
832 // this.ifra.contentWindow.ctx_old_len += comm_len;
833 this.transp.ctx_old_len_add(comm_len);
834 // this.ifra.contentWindow.script_clean = this.gst.st;
835 this.transp.scrcls_set(this.gst.st);
836 // alert("do--while end ["+again+"]");
839 // alert("post while");
840 // EXECUTION OF STREAM COMMANDS
843 //MOP ?? xhrrestart = 0;
844 if (this.gst.st_loc < this.gst.st_loc_new) {
845 // there is some slow actions running
848 else if (this.gst.comms.length > 0) {
851 singlecomm = this.gst.comms.shift();
852 // alert("EXE"+gugu);
853 // $("xhrdeltalog").innerHTML = "EVALL: "+singlecomm.replace("<", "<", "g"); +"<br>";
856 // alert("SINGLE: ["+singlecomm+"]");
857 this.cmdproc(singlecomm);
863 var date = new Date();
864 if (date.getTime() > (this.start_time + this.restart_wait)) {
865 this.transp.postproc();
867 this.watchdog_hdl = setTimeout(function(obj) { /* obj.log("tout2"); */ obj.watchdog(); }, this.watchdog_timeout, this);
870 // alert("watchdog return normal");
876 // moved to xynt-streaming-ifra as push()
878 // keepalive: function (s) {
879 // this.log("hs::keepalive");
882 // this.ifra.contentWindow.ctx_new += "@BEGIN@"+s+"@END@";
885 // this.ifra.contentWindow.ctx_new += "_";
887 // // this.keepalive_new++;
890 abort: function () { /* public */
891 // this.log("PATH: "+this.ifra.contentWindow.location.protocol + "://" + this.ifra.contentWindow.location.host + "/" + this.ifra.contentWindow.location.pathname);
894 if (this.watchdog_hdl != null) {
895 clearTimeout(this.watchdog_hdl);
896 this.watchdog_hdl = null;
900 this.log("hs::reload");
901 this.watchable = false;
902 if (this.transp != null) {
903 this.transp.destroy();
909 reload: function () {
915 if (this.console != null) {
916 return (this.console.log(s));