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) {
24 this.ws.onmessage = function (msg) {
25 self.xynt_streaming.log("onmessage");
26 // new data in msg.data
27 self.ctx_new += msg.data;
29 this.ws.onclose = function (msg) {
30 self.xynt_streaming.log("onclose"+self.init_steps);
31 if (self.init_steps == 0)
36 this.ws.onerror = function () {
38 self.xynt_streaming.log("onerror");
43 throw "websocket creation failed";
49 transport_ws.prototype = {
51 xynt_streaming: "ready",
64 destroy: function () { /* public */
65 if (this.ws != null) {
71 ws_cb: function (from) {
74 if (from == "error") {
75 if (this.xynt_streaming != "ready") {
76 if (this.xynt_streaming.transp_fback > 0) {
77 this.xynt_streaming.transp_fback--;
79 this.xynt_streaming.reload();
83 if (this.ws != null && this.ws.readyState > 1) {
88 ws_abort: function() {
89 if (this.ws != null) {
94 xstr_is_init: function () { /* public */
98 /* only after a successfull is_initialized call */
99 xstr_is_ready: function () { /* public */
100 return (this.ws.readyState == 1);
103 xstr_set: function () { /* public */
107 ctx_new_is_set: function () { /* public */
108 return (this.ctx_new != null);
111 ctx_new_curlen_get: function () { /* public */
112 return (this.ctx_new.length);
115 ctx_new_getchar: function(idx) { /* public */
116 return (this.ctx_new[idx]);
119 ctx_old_len_is_set: function () { /* public */
123 ctx_old_len_get: function () { /* public */
124 return (this.ctx_old_len);
127 ctx_old_len_set: function (len) { /* public */
128 this.ctx_old_len = len;
131 ctx_old_len_add: function (len) { /* public */
132 this.ctx_old_len += len;
135 new_part: function () { /* public */
136 return (this.ctx_new.substr(this.ctx_old_len));
139 scrcls_set: function (step) { /* public */
140 // this.script_clean = step;
143 postproc: function () {
144 if (this.stopped && !this.xstr_is_ready()) {
145 this.xynt_streaming.reload();
151 // CLASS transport_xhr
153 function transport_xhr(doc, xynt_streaming, page)
156 this.xynt_streaming = xynt_streaming;
157 this.xhr = createXMLHttpRequest();
158 this.xhr.open('GET', page);
161 this.xhr.onreadystatechange = function () { self.xhr_cb(); };
164 this.stopped = false;
167 transport_xhr.prototype = {
169 xynt_streaming: "ready",
179 destroy: function () { /* public */
180 if (this.xhr != null) {
186 xhr_cb: function () {
189 if (this.xhr.readyState == 4) {
190 // console.log("SS: "+safestatus(xhr));
192 // NOTE: delay management later
194 // if ((ret = safestatus(this.xhr)) == 200) {
196 // // console.log("del a null "+this.delayed);
197 // } else if (ret != -1) {
198 // this.delay = 5000;
200 // // alert('There was a problem with the request.' + ret);
204 // this.delayed = null;
209 xhr_abort: function() {
210 if (this.xhr != null) {
215 xstr_is_init: function () { /* public */
217 if (this.xhr.responseText != null) {
218 this.ctx_new = this.xhr.responseText;
224 return (this.ctx_new != null);
227 /* only after a successfull is_initialized call */
228 xstr_is_ready: function () { /* public */
229 return (this.xynt_streaming == "ready");
232 xstr_set: function () { /* public */
236 ctx_new_is_set: function () { /* public */
237 return (this.ctx_new != null);
240 ctx_new_curlen_get: function () { /* public */
241 return (this.ctx_new.length);
244 ctx_new_getchar: function(idx) { /* public */
245 return (this.ctx_new[idx]);
248 ctx_old_len_is_set: function () { /* public */
252 ctx_old_len_get: function () { /* public */
253 return (this.ctx_old_len);
256 ctx_old_len_set: function (len) { /* public */
257 this.ctx_old_len = len;
260 ctx_old_len_add: function (len) { /* public */
261 this.ctx_old_len += len;
264 new_part: function () { /* public */
265 return (this.ctx_new.substr(this.ctx_old_len));
268 scrcls_set: function (step) { /* public */
269 // this.script_clean = step;
272 postproc: function () {
273 if (this.stopped && !this.xstr_is_ready()) {
274 this.xynt_streaming.reload();
280 // CLASS transport_htmlfile
282 function transport_htmlfile(doc, xynt_streaming, page)
285 this.xynt_streaming = xynt_streaming;
286 this.transfdoc = new ActiveXObject("htmlfile");
287 this.transfdoc.open();
288 this.transfdoc.write("<html><body><iframe id='iframe'></iframe></body></html>");
289 this.transfdoc.close();
291 this.ifra = this.transfdoc.getElementById("iframe");
292 this.ifra.contentWindow.location.href = page;
293 this.stopped = false;
296 transport_htmlfile.prototype = {
298 xynt_streaming: null,
303 destroy: function () { /* public */
304 if (this.ifra != null) {
305 // this.doc.body.removeChild(this.ifra);
310 if (this.transfdoc) {
311 delete this.transfdoc;
312 this.transfdoc = null;
316 xstr_is_init: function () { /* public */
317 return (typeof(this.ifra.contentWindow.xynt_streaming) != 'undefined');
320 /* only after a successfull is_initialized call */
321 xstr_is_ready: function () { /* public */
322 return (this.ifra.contentWindow.xynt_streaming == "ready");
325 /* only after a successfull is_initialized call */
326 xstr_set: function () { /* public */
327 if (this.ifra.contentWindow.xynt_streaming == "ready") {
328 this.ifra.contentWindow.xynt_streaming = this.xynt_streaming;
331 else if (this.ifra.contentWindow.xynt_streaming == this.xynt_streaming) {
339 ctx_new_is_set: function () { /* public */
340 return (typeof(this.ifra.contentWindow.ctx_new) != 'undefined');
343 ctx_new_curlen_get: function () { /* public */
344 return (this.ifra.contentWindow.ctx_new.length);
347 ctx_new_getchar: function(idx) { /* public */
348 return (this.ifra.contentWindow.ctx_new.charAt(idx));
351 ctx_old_len_is_set: function () { /* public */
352 return (typeof(this.ifra.contentWindow.ctx_old_len) != 'undefined');
355 ctx_old_len_get: function () { /* public */
356 return (this.ifra.contentWindow.ctx_old_len);
359 ctx_old_len_set: function (len) { /* public */
360 this.ifra.contentWindow.ctx_old_len = len;
363 ctx_old_len_add: function (len) { /* public */
364 this.ifra.contentWindow.ctx_old_len += len;
367 new_part: function () { /* public */
368 return (this.ifra.contentWindow.ctx_new.substr(this.ifra.contentWindow.ctx_old_len));
371 scrcls_set: function (step) { /* public */
372 this.ifra.contentWindow.script_clean = step;
375 postproc: function () { /* public */
376 if (this.stopped && !this.xstr_is_ready()) {
377 this.xynt_streaming.reload();
383 // CLASS transport_iframe
385 function transport_iframe(doc, xynt_streaming, page)
388 this.xynt_streaming = xynt_streaming;
389 this.ifra = doc.createElement("iframe");
390 this.ifra.style.visibility = "hidden";
391 doc.body.appendChild(this.ifra);
392 this.ifra.contentWindow.location.href = page;
393 this.stopped = false;
396 transport_iframe.prototype = {
398 xynt_streaming: null,
402 destroy: function () { /* public */
404 if (this.ifra != null) {
405 // NOTE: on Opera this remove child crash js if called from
406 // inside of the iframe, on IE on Windows without
407 // it stream abort fails.
408 // the problem is fixed setting into the iframe's onload
409 // function the stopped attribute to true and delegate
410 // postproc() fired by xynt_streaming watchdog()
411 this.doc.body.removeChild(this.ifra);
416 alert("destroy exception catched");
420 xstr_is_init: function () { /* public */
421 return (typeof(this.ifra.contentWindow.xynt_streaming) != 'undefined');
424 /* only after a successfull is_initialized call */
425 xstr_is_ready: function () { /* public */
426 return (this.ifra.contentWindow.xynt_streaming == "ready");
429 /* only after a successfull is_initialized call */
430 xstr_set: function () { /* public */
431 if (this.ifra.contentWindow.xynt_streaming == "ready") {
432 this.ifra.contentWindow.xynt_streaming = this.xynt_streaming;
435 else if (this.ifra.contentWindow.xynt_streaming == this.xynt_streaming) {
444 /* only after a successfull is_ready call to be sure the accessibility of the var */
445 xstr_set_old: function (xynt_streaming) { /* public */
446 this.ifra.contentWindow.xynt_streaming = xynt_streaming;
449 ctx_new_is_set: function () { /* public */
450 return (typeof(this.ifra.contentWindow.ctx_new) != 'undefined');
453 ctx_new_curlen_get: function () { /* public */
454 return (this.ifra.contentWindow.ctx_new.length);
457 ctx_new_getchar: function(idx) { /* public */
458 return (this.ifra.contentWindow.ctx_new.charAt(idx));
461 ctx_old_len_is_set: function () { /* public */
462 return (typeof(this.ifra.contentWindow.ctx_old_len) != 'undefined');
465 ctx_old_len_get: function () { /* public */
466 return (this.ifra.contentWindow.ctx_old_len);
469 ctx_old_len_set: function (len) { /* public */
470 this.ifra.contentWindow.ctx_old_len = len;
473 ctx_old_len_add: function (len) { /* public */
474 this.ifra.contentWindow.ctx_old_len += len;
477 new_part: function () { /* public */
478 return (this.ifra.contentWindow.ctx_new.substr(this.ifra.contentWindow.ctx_old_len));
481 scrcls_set: function (step) { /* public */
482 this.ifra.contentWindow.script_clean = step;
485 postproc: function () { /* public */
486 if (this.stopped && !this.xstr_is_ready()) {
487 this.xynt_streaming.reload();
492 function xynt_streaming(win, transp_type, transp_port, transp_fback, console, gst, from, cookiename, sess, sandbox, page, cmdproc)
495 this.transp_type = transp_type;
496 this.transp_port = transp_port;
497 this.transp_fback = transp_fback;
498 this.console = console;
501 this.cookiename = cookiename;
503 this.sandbox = sandbox;
505 this.cmdproc = cmdproc;
506 // this.cmdproc = function(com){/* console.log("COM: "+com); */ eval(com);}
507 this.doc = win.document;
508 this.keepalive_old = -1;
509 this.keepalive_new = -1;
511 this.mon_errtime = this.keepalives_eq_max * this.watchdog_checktm * this.watchdog_timeout;
512 this.mon_wrntime = this.mon_errtime / 2;
517 xynt_streaming.prototype = {
533 restart_wait: 5000, // wait restart_wait millisec before begin to check if restart is needed
536 cookiepath: "/brisk/",
538 hbit: function () {},
542 /* NOTE: right watch_timeout value to 100, for devel reasons use 1000 or more */
543 /* restart after 4 * 40 * 100 millisec if server ping is missing => 16secs */
544 keepalives_eq_max: 4,
545 watchdog_checktm: 40,
546 watchdog_timeout: 100,
550 comm_match: /_*@BEGIN@(.*?)@END@/g,
551 comm_clean: /_*@BEGIN@(.*?)@END@/,
561 mon_update: function()
563 var date = new Date();
565 this.mon_time = date.getTime();
569 ping arrives at least every RD_KEEPALIVE_TOUT secs (currently 4 secs)
571 return values: 0 unknown
576 mon_status: function()
580 var date = new Date();
582 delta = date.getTime() - this.mon_time;
584 if (delta < this.mon_wrntime)
586 else if (delta < this.mon_errtime)
592 start: function() { /* public */
596 // this.log("start "+this.transp_type+" "+this.transp_fback);
600 createCookie(this.cookiename, sess, 24*365, this.cookiepath);
602 this.log("xynt_streaming:start restart: "+this.restart_n);
603 this.keepalives_equal = 0;
606 this.page = url_complete(this.win.location.href, this.page);
608 if (this.transp_fback > 0) {
609 transp_type = "websocket";
610 transp_port = (this.transp_fback == 2 ? 80 : 8080);
613 transp_type = this.transp_type;
614 transp_port = this.transp_port;
617 if (transp_type == "websocket") {
618 var end_proto, first_slash;
621 this.log("precha ["+this.page+"]");
622 end_proto = this.page.indexOf("://");
623 first_slash = this.page.substring(end_proto+3).indexOf("/");
625 page = "ws://" + this.page.substring(end_proto+3, end_proto+3+first_slash) + ":" + transp_port + this.page.substring(end_proto+3 + first_slash);
630 // stat, subst, this.gst.st
632 page = url_append_args(page, "sess", this.sess, "stat", stat, "subst", subst, "step", this.gst.st, "from", this.from);
633 // this.log("the page:");
637 // transport instantiation
638 if (transp_type == "websocket") {
639 page = url_append_args(page, "transp", "websocket");
640 this.transp = new transport_ws(this.doc, this, page);
642 else if (transp_type == "xhr") {
643 page = url_append_args(page, "transp", "xhr");
644 this.transp = new transport_xhr(this.doc, this, page);
646 else if (transp_type == "iframe") {
647 page = url_append_args(page, "transp", "iframe");
648 this.transp = new transport_iframe(this.doc, this, page);
650 else if (transp_type == "htmlfile") {
651 page = url_append_args(page, "transp", "htmlfile");
652 this.transp = new transport_htmlfile(this.doc, this, page);
658 if (this.transp_fback > 0) {
666 this.watchdog_ct = 0;
668 this.watchdog_hdl = setTimeout(function(obj) { obj.log("tout1"); obj.watchdog(); }, this.watchdog_timeout, this);
671 var date = new Date();
672 this.start_time = date.getTime();
680 hbit_set: function (hbit) {
685 hbit_status: function () {
688 ret = this.mon_status();
689 // console.log("mon_status: "+ret+" 0: "+this.mon_time);
705 if (this.mon_stat_old != mon_stat) {
707 this.mon_stat_old = mon_stat;
712 watchdog: function () {
713 // alert("watchdog");
715 var comm_newpart, comm_len, comm_arr;
718 if (this.sandbox != null) {
719 // 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;
720 var zug = "WATCHDOG sess = ["+this.sess+"] step = "+this.gst.st+" step_loc = "+this.gst.st_loc+" step_loc_new = "+this.gst.st_loc_new;
721 if (zug != this.sandbox.innerHTML)
722 this.sandbox.innerHTML = zug;
725 // WATCHDOGING THE CONNECTION
726 this.log("hs::watchdog: start, cur equal times: "+this.keepalives_equal);
727 if (!this.watchable) {
730 // if (typeof(this.ifra.contentWindow.xynt_streaming) == 'undefined')
731 if (!this.transp.xstr_is_init()) {
732 this.log("hs::watchdog: xstr_is_init = false");
737 this.log("hs::watchdog: exception");
742 on IE7 the the window frame scope is cleaned after the href is set, so we wait
743 for a well know variable value before assign this object value to it (OO is a passion)
745 // if (this.ifra.contentWindow.xynt_streaming == "ready") {
746 if (this.transp.xstr_set()) {
747 // this.ifra.contentWindow.xynt_streaming = this;
748 this.watchable = true;
749 this.watchdog_ct = 0;
750 this.log("hs::watchdog: watchable = yes");
754 if ( (this.watchdog_ct % this.watchdog_checktm) == 0) {
755 this.log("hs::watchdog: this.keepalive_old: "+this.keepalive_old+" this.keepalive_new: "+this.keepalive_new);
756 if (this.keepalive_old == this.keepalive_new) {
757 this.keepalives_equal++;
760 this.keepalive_old = this.keepalive_new;
761 this.keepalives_equal = 0;
764 if (this.keepalives_equal >= this.keepalives_eq_max) {
765 this.log("hs::watchdog: MAX ACHIEVED "+this.keepalives_equal);
767 // alert("watchdog return reload");
773 // PICK COMMANDS FROM STREAM
775 // alert("do--while begin ["+again+"]");
776 // CHECK: maybe again here isn't needed
779 /* if (typeof(this.ifra.contentWindow.ctx_new) == 'undefined' ||
780 typeof(this.ifra.contentWindow.ctx_old_len) == 'undefined') */
781 if (!this.transp.ctx_new_is_set() || !this.transp.ctx_old_len_is_set())
788 // ctx_new_len = this.ifra.contentWindow.ctx_new.length;
789 ctx_new_len = this.transp.ctx_new_curlen_get();
790 // if (ctx_new_len <= this.ifra.contentWindow.ctx_old_len) {
791 if (ctx_new_len <= this.transp.ctx_old_len_get()) {
794 this.log("new: "+ ctx_new_len + " old: "+this.transp.ctx_old_len_get());
795 this.keepalive_new++;
796 // alert("pre-loop 1");
797 for (i = this.transp.ctx_old_len_get() ; i < ctx_new_len ; i++) {
798 // if (this.ifra.contentWindow.ctx_new.charAt(i) != '_') {
799 if (this.transp.ctx_new_getchar(i) != '_') {
800 // this.log("ctx_new.char(i) != '_' ["+this.ifra.contentWindow.ctx_new.charAt(i)+"]");
807 // this.log("ctx_new.charAt(i) == '_'");
810 // this.ifra.contentWindow.ctx_old_len = i;
811 this.transp.ctx_old_len_set(i);
812 if (i == ctx_new_len) {
813 this.log("old_len == i");
817 this.log("old_len != i: "+i);
819 // alert("do--while middle ["+this.ifra.contentWindow.ctx_old_len+"]");
821 comm_newpart = this.transp.new_part();
822 this.log("COM_NEWPART: ["+comm_newpart+"]");
824 comm_arr = comm_newpart.match(this.comm_match);
826 // alert("do--while middle2 ["+again+"]");
828 var comm_arr_len = comm_arr.length;
829 for (i = 0 ; i < comm_arr_len ; i++) {
830 var temp = comm_arr[i].replace(this.comm_clean,"$1").split("|");
831 this.gst.comms = this.gst.comms.concat(temp);
832 comm_len += comm_arr[i].length;
838 // this.ifra.contentWindow.ctx_old_len += comm_len;
839 this.transp.ctx_old_len_add(comm_len);
840 // this.ifra.contentWindow.script_clean = this.gst.st;
841 this.transp.scrcls_set(this.gst.st);
842 // alert("do--while end ["+again+"]");
845 // alert("post while");
846 // EXECUTION OF STREAM COMMANDS
849 //MOP ?? xhrrestart = 0;
850 if (this.gst.st_loc < this.gst.st_loc_new) {
851 // there is some slow actions running
854 else if (this.gst.comms.length > 0) {
857 singlecomm = this.gst.comms.shift();
858 // alert("EXE"+gugu);
859 // $("xhrdeltalog").innerHTML = "EVALL: "+singlecomm.replace("<", "<", "g"); +"<br>";
862 // alert("SINGLE: ["+singlecomm+"]");
863 this.cmdproc(singlecomm);
869 var date = new Date();
870 if (date.getTime() > (this.start_time + this.restart_wait)) {
871 this.transp.postproc();
873 this.watchdog_hdl = setTimeout(function(obj) { /* obj.log("tout2"); */ obj.watchdog(); }, this.watchdog_timeout, this);
876 // alert("watchdog return normal");
882 // moved to xynt-streaming-ifra as push()
884 // keepalive: function (s) {
885 // this.log("hs::keepalive");
888 // this.ifra.contentWindow.ctx_new += "@BEGIN@"+s+"@END@";
891 // this.ifra.contentWindow.ctx_new += "_";
893 // // this.keepalive_new++;
896 abort: function () { /* public */
897 // this.log("PATH: "+this.ifra.contentWindow.location.protocol + "://" + this.ifra.contentWindow.location.host + "/" + this.ifra.contentWindow.location.pathname);
900 if (this.watchdog_hdl != null) {
901 clearTimeout(this.watchdog_hdl);
902 this.watchdog_hdl = null;
906 this.log("hs::reload");
907 this.watchable = false;
908 if (this.transp != null) {
909 this.transp.destroy();
915 reload: function () {
921 if (this.console != null) {
922 return (this.console.log(s));