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 self.xynt_streaming.log("onopen");
29 if (this.readyState == 1) {
35 this.ws.onmessage = function (msg) {
36 self.xynt_streaming.log("onmessage");
37 // new data in msg.data
38 self.ctx_new += msg.data;
40 this.ws.onclose = function (msg) {
44 self.xynt_streaming.log("onclose"+self.init_steps);
45 if (self.init_steps == 0)
50 this.ws.onerror = function () {
55 self.xynt_streaming.log("onerror");
60 throw "websocket creation failed";
66 transport_ws.prototype = {
69 xynt_streaming: "ready",
82 destroy: function () { /* public */
83 if (this.ws != null) {
89 ws_cb: function (from) {
92 if (from == "error") {
93 if (this.xynt_streaming != "ready") {
94 if (this.xynt_streaming.transp_fback > 0) {
95 this.xynt_streaming.log("DEC: "+this.xynt_streaming.transp_fback);
96 this.xynt_streaming.transp_fback--;
98 this.xynt_streaming.reload();
102 if (this.ws != null && this.ws.readyState > 1) {
107 ws_abort: function() {
108 if (this.ws != null) {
109 this.xynt_streaming.log("WSCLOSE");
114 xstr_is_init: function () { /* public */
118 /* only after a successfull is_initialized call */
119 xstr_is_ready: function () { /* public */
120 return (this.ws.readyState == 1);
123 xstr_set: function () { /* public */
127 ctx_new_is_set: function () { /* public */
128 return (this.ctx_new != null);
131 ctx_new_curlen_get: function () { /* public */
132 return (this.ctx_new.length);
135 ctx_new_getchar: function(idx) { /* public */
136 return (this.ctx_new[idx]);
139 ctx_old_len_is_set: function () { /* public */
143 ctx_old_len_get: function () { /* public */
144 return (this.ctx_old_len);
147 ctx_old_len_set: function (len) { /* public */
148 this.ctx_old_len = len;
151 ctx_old_len_add: function (len) { /* public */
152 this.ctx_old_len += len;
155 new_part: function () { /* public */
156 return (this.ctx_new.substr(this.ctx_old_len));
159 scrcls_set: function (step) { /* public */
160 // this.script_clean = step;
163 postproc: function () {
164 if (this.stopped && !this.xstr_is_ready()) {
165 this.xynt_streaming.reload();
171 // CLASS transport_xhr
173 function transport_xhr(doc, xynt_streaming, page)
177 this.xynt_streaming = xynt_streaming;
178 this.xhr = createXMLHttpRequest();
179 this.xhr.open('GET', page);
182 this.xhr.onreadystatechange = function () { self.xhr_cb(); };
185 this.stopped = false;
188 transport_xhr.prototype = {
191 xynt_streaming: "ready",
201 destroy: function () { /* public */
202 if (this.xhr != null) {
208 xhr_cb: function () {
211 if (this.xhr.readyState == 4) {
212 // console.log("SS: "+safestatus(xhr));
214 // NOTE: delay management later
216 // if ((ret = safestatus(this.xhr)) == 200) {
218 // // console.log("del a null "+this.delayed);
219 // } else if (ret != -1) {
220 // this.delay = 5000;
222 // // alert('There was a problem with the request.' + ret);
226 // this.delayed = null;
231 xhr_abort: function() {
232 if (this.xhr != null) {
237 xstr_is_init: function () { /* public */
239 if (this.xhr.responseText != null) {
240 this.ctx_new = this.xhr.responseText;
246 return (this.ctx_new != null);
249 /* only after a successfull is_initialized call */
250 xstr_is_ready: function () { /* public */
251 return (this.xynt_streaming == "ready");
254 xstr_set: function () { /* public */
258 ctx_new_is_set: function () { /* public */
259 return (this.ctx_new != null);
262 ctx_new_curlen_get: function () { /* public */
263 return (this.ctx_new.length);
266 ctx_new_getchar: function(idx) { /* public */
267 return (this.ctx_new[idx]);
270 ctx_old_len_is_set: function () { /* public */
274 ctx_old_len_get: function () { /* public */
275 return (this.ctx_old_len);
278 ctx_old_len_set: function (len) { /* public */
279 this.ctx_old_len = len;
282 ctx_old_len_add: function (len) { /* public */
283 this.ctx_old_len += len;
286 new_part: function () { /* public */
287 return (this.ctx_new.substr(this.ctx_old_len));
290 scrcls_set: function (step) { /* public */
291 // this.script_clean = step;
294 postproc: function () {
295 if (this.stopped && !this.xstr_is_ready()) {
296 this.xynt_streaming.reload();
302 // CLASS transport_htmlfile
304 function transport_htmlfile(doc, xynt_streaming, page)
306 this.name = "HTMLFile";
308 this.xynt_streaming = xynt_streaming;
309 this.transfdoc = new ActiveXObject("htmlfile");
310 this.transfdoc.open();
311 this.transfdoc.write("<html><body><iframe id='iframe'></iframe></body></html>");
312 this.transfdoc.close();
314 this.ifra = this.transfdoc.getElementById("iframe");
315 this.ifra.contentWindow.location.href = page;
316 this.stopped = false;
319 transport_htmlfile.prototype = {
322 xynt_streaming: null,
327 destroy: function () { /* public */
328 if (this.ifra != null) {
329 // this.doc.body.removeChild(this.ifra);
334 if (this.transfdoc) {
335 delete this.transfdoc;
336 this.transfdoc = null;
340 xstr_is_init: function () { /* public */
341 return (typeof(this.ifra.contentWindow.xynt_streaming) != 'undefined');
344 /* only after a successfull is_initialized call */
345 xstr_is_ready: function () { /* public */
346 return (this.ifra.contentWindow.xynt_streaming == "ready");
349 /* only after a successfull is_initialized call */
350 xstr_set: function () { /* public */
351 if (this.ifra.contentWindow.xynt_streaming == "ready") {
352 this.ifra.contentWindow.xynt_streaming = this.xynt_streaming;
355 else if (this.ifra.contentWindow.xynt_streaming == this.xynt_streaming) {
363 ctx_new_is_set: function () { /* public */
364 return (typeof(this.ifra.contentWindow.ctx_new) != 'undefined');
367 ctx_new_curlen_get: function () { /* public */
368 return (this.ifra.contentWindow.ctx_new.length);
371 ctx_new_getchar: function(idx) { /* public */
372 return (this.ifra.contentWindow.ctx_new.charAt(idx));
375 ctx_old_len_is_set: function () { /* public */
376 return (typeof(this.ifra.contentWindow.ctx_old_len) != 'undefined');
379 ctx_old_len_get: function () { /* public */
380 return (this.ifra.contentWindow.ctx_old_len);
383 ctx_old_len_set: function (len) { /* public */
384 this.ifra.contentWindow.ctx_old_len = len;
387 ctx_old_len_add: function (len) { /* public */
388 this.ifra.contentWindow.ctx_old_len += len;
391 new_part: function () { /* public */
392 return (this.ifra.contentWindow.ctx_new.substr(this.ifra.contentWindow.ctx_old_len));
395 scrcls_set: function (step) { /* public */
396 this.ifra.contentWindow.script_clean = step;
399 postproc: function () { /* public */
400 if (this.stopped && !this.xstr_is_ready()) {
401 this.xynt_streaming.reload();
407 // CLASS transport_iframe
409 function transport_iframe(doc, xynt_streaming, page)
411 this.name = "IFRAME";
413 this.xynt_streaming = xynt_streaming;
414 this.ifra = doc.createElement("iframe");
415 this.ifra.style.visibility = "hidden";
416 doc.body.appendChild(this.ifra);
417 this.ifra.contentWindow.location.href = page;
418 this.stopped = false;
421 transport_iframe.prototype = {
424 xynt_streaming: null,
428 destroy: function () { /* public */
430 if (this.ifra != null) {
431 // NOTE: on Opera this remove child crash js if called from
432 // inside of the iframe, on IE on Windows without
433 // it stream abort fails.
434 // the problem is fixed setting into the iframe's onload
435 // function the stopped attribute to true and delegate
436 // postproc() fired by xynt_streaming watchdog()
437 this.doc.body.removeChild(this.ifra);
442 alert("destroy exception catched");
446 xstr_is_init: function () { /* public */
447 return (typeof(this.ifra.contentWindow.xynt_streaming) != 'undefined');
450 /* only after a successfull is_initialized call */
451 xstr_is_ready: function () { /* public */
452 return (this.ifra.contentWindow.xynt_streaming == "ready");
455 /* only after a successfull is_initialized call */
456 xstr_set: function () { /* public */
457 if (this.ifra.contentWindow.xynt_streaming == "ready") {
458 this.ifra.contentWindow.xynt_streaming = this.xynt_streaming;
461 else if (this.ifra.contentWindow.xynt_streaming == this.xynt_streaming) {
470 /* only after a successfull is_ready call to be sure the accessibility of the var */
471 xstr_set_old: function (xynt_streaming) { /* public */
472 this.ifra.contentWindow.xynt_streaming = xynt_streaming;
475 ctx_new_is_set: function () { /* public */
476 return (typeof(this.ifra.contentWindow.ctx_new) != 'undefined');
479 ctx_new_curlen_get: function () { /* public */
480 return (this.ifra.contentWindow.ctx_new.length);
483 ctx_new_getchar: function(idx) { /* public */
484 return (this.ifra.contentWindow.ctx_new.charAt(idx));
487 ctx_old_len_is_set: function () { /* public */
488 return (typeof(this.ifra.contentWindow.ctx_old_len) != 'undefined');
491 ctx_old_len_get: function () { /* public */
492 return (this.ifra.contentWindow.ctx_old_len);
495 ctx_old_len_set: function (len) { /* public */
496 this.ifra.contentWindow.ctx_old_len = len;
499 ctx_old_len_add: function (len) { /* public */
500 this.ifra.contentWindow.ctx_old_len += len;
503 new_part: function () { /* public */
504 return (this.ifra.contentWindow.ctx_new.substr(this.ifra.contentWindow.ctx_old_len));
507 scrcls_set: function (step) { /* public */
508 this.ifra.contentWindow.script_clean = step;
511 postproc: function () { /* public */
512 if (this.stopped && !this.xstr_is_ready()) {
513 this.xynt_streaming.reload();
518 function xynt_streaming(win, transp_type, transp_port, transp_fback, console, gst, from, cookiename, sess, sandbox, page, cmdproc)
521 this.transp_type = transp_type;
522 this.transp_port = transp_port;
523 this.transp_fback = transp_fback;
524 this.console = console;
527 this.cookiename = cookiename;
529 this.sandbox = sandbox;
531 this.cmdproc = cmdproc;
532 // this.cmdproc = function(com){/* console.log("COM: "+com); */ eval(com);}
533 this.doc = win.document;
534 this.keepalive_old = -1;
535 this.keepalive_new = -1;
537 this.mon_errtime = this.keepalives_eq_max * this.watchdog_checktm * this.watchdog_timeout;
538 this.mon_wrntime = this.mon_errtime / 2;
543 xynt_streaming.prototype = {
547 transp_type_cur: null,
561 restart_wait: 5000, // wait restart_wait millisec before begin to check if restart is needed
564 cookiepath: "/brisk/",
566 hbit: function () {},
570 /* NOTE: right watch_timeout value to 100, for devel reasons use 1000 or more */
571 /* restart after 4 * 40 * 100 millisec if server ping is missing => 16secs */
572 keepalives_eq_max: 4,
573 watchdog_checktm: 40,
574 watchdog_timeout: 100,
578 comm_match: /_*@BEGIN@(.*?)@END@/g,
579 comm_clean: /_*@BEGIN@(.*?)@END@/,
589 mon_update: function()
591 var date = new Date();
593 this.mon_time = date.getTime();
597 ping arrives at least every RD_KEEPALIVE_TOUT secs (currently 4 secs)
599 return values: 0 unknown
604 mon_status: function()
608 var date = new Date();
610 delta = date.getTime() - this.mon_time;
612 if (delta < this.mon_wrntime)
614 else if (delta < this.mon_errtime)
620 start: function() { /* public */
624 // this.log("start "+this.transp_type+" "+this.transp_fback);
628 createCookie(this.cookiename, sess, 24*365, this.cookiepath);
630 this.log("xynt_streaming:start restart: "+this.restart_n);
631 this.keepalives_equal = 0;
634 this.page = url_complete(this.win.location.href, this.page);
636 // DEFAULT TRANSPORT PROTOCOL HERE websocketsec, websocket
637 if (this.transp_fback > 0) {
638 if (location.protocol == 'https:') {
639 transp_type = "websocketsec";
643 transp_type = "websocket";
644 transp_port = (this.transp_fback == 2 ? 80 : 8080);
649 transp_type = this.transp_type;
650 transp_port = this.transp_port;
653 this.transp_type_cur = transp_type;
654 this.transp_port_cur = transp_port;
656 if (transp_type == "websocket" || transp_type == "websocketsec") {
657 var end_proto, first_slash, newpage;
660 this.log("precha ["+this.page+"]");
661 if (transp_type == "websocketsec") {
662 newpage = this.page.replace(/\.php$/g, "_wss.php").replace(/\.php\?/g, "_wss.php?");
667 end_proto = newpage.indexOf("://");
668 first_slash = newpage.substring(end_proto+3).indexOf("/");
670 page = (transp_type == "websocketsec" ? "wss://" : "ws://")
671 + newpage.substring(end_proto+3, end_proto+3 + first_slash) + ":"
672 + transp_port + newpage.substring(end_proto+3 + first_slash);
673 // this.log("MOP WS: " + page);
678 // stat, subst, this.gst.st
680 page = url_append_args(page, "sess", this.sess, "stat", stat, "subst", subst, "step", this.gst.st, "from", this.from);
681 // this.log("the page:");
685 // transport instantiation
686 if (transp_type == "websocketsec") {
687 page = url_append_args(page, "transp", "websocketsec");
688 this.transp = new transport_ws(this.doc, this, page, true);
690 else if (transp_type == "websocket") {
691 page = url_append_args(page, "transp", "websocket");
692 this.transp = new transport_ws(this.doc, this, page);
694 else if (transp_type == "xhr") {
695 page = url_append_args(page, "transp", "xhr");
696 this.transp = new transport_xhr(this.doc, this, page);
698 else if (transp_type == "iframe") {
699 page = url_append_args(page, "transp", "iframe");
700 this.transp = new transport_iframe(this.doc, this, page);
702 else if (transp_type == "htmlfile") {
703 page = url_append_args(page, "transp", "htmlfile");
704 this.transp = new transport_htmlfile(this.doc, this, page);
710 if (this.transp_fback > 0) {
718 this.watchdog_ct = 0;
720 this.watchdog_hdl = setTimeout(function(obj) { obj.log("tout1"); obj.watchdog(); }, this.watchdog_timeout, this);
723 var date = new Date();
724 this.start_time = date.getTime();
732 hbit_set: function (hbit) {
737 hbit_status: function () {
740 ret = this.mon_status();
741 // console.log("mon_status: "+ret+" 0: "+this.mon_time);
757 if (this.mon_stat_old != mon_stat) {
759 this.mon_stat_old = mon_stat;
764 watchdog: function () {
765 // alert("watchdog");
767 var comm_newpart, comm_len, comm_arr;
770 if (this.sandbox != null) {
771 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;
772 if (zug != this.sandbox.innerHTML)
773 this.sandbox.innerHTML = zug;
776 // WATCHDOGING THE CONNECTION
777 this.log("hs::watchdog: start, cur equal times: "+this.keepalives_equal);
778 if (!this.watchable) {
781 // if (typeof(this.ifra.contentWindow.xynt_streaming) == 'undefined')
782 if (!this.transp.xstr_is_init()) {
783 this.log("hs::watchdog: xstr_is_init = false");
788 this.log("hs::watchdog: exception");
793 on IE7 the the window frame scope is cleaned after the href is set, so we wait
794 for a well know variable value before assign this object value to it (OO is a passion)
796 // if (this.ifra.contentWindow.xynt_streaming == "ready") {
797 if (this.transp.xstr_set()) {
798 // this.ifra.contentWindow.xynt_streaming = this;
799 this.watchable = true;
800 this.watchdog_ct = 0;
801 this.log("hs::watchdog: watchable = yes");
805 if ( (this.watchdog_ct % this.watchdog_checktm) == 0) {
806 this.log("hs::watchdog: this.keepalive_old: "+this.keepalive_old+" this.keepalive_new: "+this.keepalive_new);
807 if (this.keepalive_old == this.keepalive_new) {
808 this.keepalives_equal++;
811 this.keepalive_old = this.keepalive_new;
812 this.keepalives_equal = 0;
815 if (this.keepalives_equal >= this.keepalives_eq_max) {
816 this.log("hs::watchdog: MAX ACHIEVED "+this.keepalives_equal);
818 // alert("watchdog return reload");
824 // PICK COMMANDS FROM STREAM
826 // alert("do--while begin ["+again+"]");
827 // CHECK: maybe again here isn't needed
830 /* if (typeof(this.ifra.contentWindow.ctx_new) == 'undefined' ||
831 typeof(this.ifra.contentWindow.ctx_old_len) == 'undefined') */
832 if (!this.transp.ctx_new_is_set() || !this.transp.ctx_old_len_is_set())
839 // ctx_new_len = this.ifra.contentWindow.ctx_new.length;
840 ctx_new_len = this.transp.ctx_new_curlen_get();
841 // if (ctx_new_len <= this.ifra.contentWindow.ctx_old_len) {
842 if (ctx_new_len <= this.transp.ctx_old_len_get()) {
845 this.log("new: "+ ctx_new_len + " old: "+this.transp.ctx_old_len_get());
846 this.keepalive_new++;
847 // alert("pre-loop 1");
848 for (i = this.transp.ctx_old_len_get() ; i < ctx_new_len ; i++) {
849 // if (this.ifra.contentWindow.ctx_new.charAt(i) != '_') {
850 if (this.transp.ctx_new_getchar(i) != '_') {
851 // this.log("ctx_new.char(i) != '_' ["+this.ifra.contentWindow.ctx_new.charAt(i)+"]");
858 // this.log("ctx_new.charAt(i) == '_'");
861 // this.ifra.contentWindow.ctx_old_len = i;
862 this.transp.ctx_old_len_set(i);
863 if (i == ctx_new_len) {
864 this.log("old_len == i");
868 this.log("old_len != i: "+i);
870 // alert("do--while middle ["+this.ifra.contentWindow.ctx_old_len+"]");
872 comm_newpart = this.transp.new_part();
873 this.log("COM_NEWPART: ["+comm_newpart+"]");
875 comm_arr = comm_newpart.match(this.comm_match);
877 // alert("do--while middle2 ["+again+"]");
879 var comm_arr_len = comm_arr.length;
880 for (i = 0 ; i < comm_arr_len ; i++) {
881 var temp = comm_arr[i].replace(this.comm_clean,"$1").split("|");
882 this.gst.comms = this.gst.comms.concat(temp);
883 comm_len += comm_arr[i].length;
889 // this.ifra.contentWindow.ctx_old_len += comm_len;
890 this.transp.ctx_old_len_add(comm_len);
891 // this.ifra.contentWindow.script_clean = this.gst.st;
892 this.transp.scrcls_set(this.gst.st);
893 // alert("do--while end ["+again+"]");
896 // alert("post while");
897 // EXECUTION OF STREAM COMMANDS
900 //MOP ?? xhrrestart = 0;
901 if (this.gst.st_loc < this.gst.st_loc_new) {
902 // there is some slow actions running
905 else if (this.gst.comms.length > 0) {
908 singlecomm = this.gst.comms.shift();
909 // alert("EXE"+gugu);
910 // $("xhrdeltalog").innerHTML = "EVALL: "+singlecomm.replace("<", "<", "g"); +"<br>";
913 // alert("SINGLE: ["+singlecomm+"]");
914 // window.console.log("["+singlecomm+"]");
915 this.cmdproc(singlecomm);
916 if (this.transp_type_cur) {
917 this.transp_type = this.transp_type_cur;
918 this.transp_port = this.transp_port_cur;
925 var date = new Date();
926 if (date.getTime() > (this.start_time + this.restart_wait)) {
927 this.transp.postproc();
929 this.watchdog_hdl = setTimeout(function(obj) { /* obj.log("tout2"); */ obj.watchdog(); }, this.watchdog_timeout, this);
932 // alert("watchdog return normal");
938 // moved to xynt-streaming-ifra as push()
940 // keepalive: function (s) {
941 // this.log("hs::keepalive");
944 // this.ifra.contentWindow.ctx_new += "@BEGIN@"+s+"@END@";
947 // this.ifra.contentWindow.ctx_new += "_";
949 // // this.keepalive_new++;
952 abort: function () { /* public */
953 // this.log("PATH: "+this.ifra.contentWindow.location.protocol + "://" + this.ifra.contentWindow.location.host + "/" + this.ifra.contentWindow.location.pathname);
956 if (this.watchdog_hdl != null) {
957 clearTimeout(this.watchdog_hdl);
958 this.watchdog_hdl = null;
962 this.log("hs::reload");
963 this.watchable = false;
964 if (this.transp != null) {
965 this.transp.destroy();
971 reload: function () {
977 if (this.console != null) {
978 return (this.console.log(s));