workaround implemented to avoid IE7 scope reset
authorMatteo Nastasi (mop) <nastasi@alternativeoutput.com>
Sat, 7 Apr 2012 16:51:50 +0000 (18:51 +0200)
committerMatteo Nastasi (mop) <nastasi@alternativeoutput.com>
Sat, 7 Apr 2012 16:51:50 +0000 (18:51 +0200)
web/test-hs-rd.php
web/test-hs.php
web/xynt/xynt-http-streaming.js

index 67bc876..bfa7544 100644 (file)
@@ -1,13 +1,10 @@
 <html>
 <head>
 <script type="text/javascript" src="xynt/xynt-console.js"></script>
-<!-- <script type="text/javascript">
-window.onload_old = function() {
-    // parent.ifra.contentWindow.location.href = 'winstream_rd.php';     
-     window.location.href = 'winstream_rd.php';     
-}
-
-</script> -->
+<script type="text/javascript">
+var http_streaming = "ready";
+window.onload = function () { if (http_streaming != "ready") http_streaming.reload(); };
+</script> 
 <?php
 function mop_flush()
 {
@@ -21,23 +18,18 @@ function main()
     for ($i = 0 ; $i < 4096 ; $i++) {
         $s .= rand(0,255);
     }
-    for ($i = 0 ; $i < 300 ; $i++) {
+    for ($i = 0 ; $i < 20 ; $i++) {
         
-        /*                    parent.document.getElementById('cont').innerHTML = \"%d\";
-                             parent.allmythings =  \"%d\"; */
-        if ($i != 350) {
-            printf("<script id=\"hs%d\" type=\"text/javascript\">
-                    parent.allmythings = %d;
-                    console.log(\"zorro \"+parent.allmythings);
-
-                </script>", $i, $i);
+        if ($i != 18) {
+            printf("<script id='hs%d' type='text/javascript'>
+if (http_streaming != 'ready') {
+    http_streaming.keepalive();
+}
+</script>", $i, $i);
         }
         else {
-            printf("<script id=\"hs%d\" type=\"text/javascript\">
-                    parent.allmythings = %d;
-                    console.log(\"zorro \"+parent.allmythings);
-                    parent.document.location.assign('winstream2.php');
-                </script>", $i, $i);
+            // exit();
+            sleep(30);
         }
         if ($i == 0)
             printf("<!-- %s -->", $s);
index 257d895..8d9082d 100644 (file)
@@ -9,7 +9,7 @@ var allmythings="BEGIN";
 window.onload = function () {
     var hs;
     console.log("zugu");
-    hs = new http_streaming(document, console);
+    hs = new http_streaming(window, console);
     hs.start('test-hs-rd.php');
 }
     
index cb914e0..3988d1d 100644 (file)
@@ -1,57 +1,94 @@
-function http_streaming(doc, cons)
+function http_streaming(win, cons)
 {
     this.console = cons;
-    this.doc = doc;
+    this.doc = win.document;
     this.keepalive_old = -1;
     this.keepalive_new = -1;
     this.keepalives_equal = 0;
-    this.ifra = doc.createElement("iframe");
+    this.ifra = this.doc.createElement("iframe");
     this.ifra.style.visibility = "hidden";
-    doc.body.appendChild(this.ifra);
-    // this.ifra.contentWindow.onload = function () {
-    //     this.ifra.contentWindow.location.href = 'winstream_rdxxx.php';     
-    // }
-    
+    this.doc.body.appendChild(this.ifra);
 }
 
 http_streaming.prototype = {
     console:           null,
     doc:               null,
     ifra:              null,
+    page:              null,
+    wd_handle:         null,
     keepalive_old:    -1,
     keepalive_new:    -1,
     keepalives_equal:  0,
     keepalives_eq_max: 6,
     watchdog_timeout: 2000,
+    watchable: false,
+    restart_n: 0,
 
     start: function(page) {
-        this.log("http_streaming:start");
+        this.log("http_streaming:start restart: "+this.restart_n);
+        this.page = page;
         this.ifra.contentWindow.location.href = page;
-        setTimeout(function(obj) { obj.watchdog(); }, this.watchdog_timeout, this);
+        this.wd_handle = setTimeout(function(obj) { obj.log("tout1"); obj.watchdog(); }, this.watchdog_timeout, this);
     },
 
     watchdog: function () {
         /* +this.keepalives_equal */
-        this.log("hs::watchdog: start");
+        this.log("hs::watchdog: start, cur equal times: "+this.keepalives_equal);
+        if (!this.watchable) {
+            do {
+                if (typeof(this.ifra.contentWindow.http_streaming) == 'undefined')
+                    break;
+                /*
+                  on IE7 the the window frame scope is cleaned after the href is set, so we wait 
+                  for a well know variable value before assign this object value to it (OO is a passion)
+                */
+                if (this.ifra.contentWindow.http_streaming == "ready") {
+                    this.ifra.contentWindow.http_streaming = this;
+                    this.watchable = true;
+                }
+            } while (false);
+        }
+        this.log("hs::watchdog: this.keepalive_old: "+this.keepalive_old+" this.keepalive_new: "+this.keepalive_new);
         if (this.keepalive_old == this.keepalive_new) {
             this.keepalives_equal++;
         }
         else {
+            this.keepalive_old = this.keepalive_new;
             this.keepalives_equal = 0;
         }
         
         if (this.keepalives_equal > this.keepalives_eq_max) {
             this.log("hs::watchdog: MAX ACHIEVED "+this.keepalives_equal);
+            this.reload();
+            return;
         }
         else {
-            setTimeout(function(obj) { obj.watchdog() }, this.watchdog_timeout, this);
+            this.wd_handle = setTimeout(function(obj) { obj.log("tout2"); obj.watchdog(); }, this.watchdog_timeout, this);
         }
     },
     
     keepalive: function () {
+        this.log("hs::keepalive");
         this.keepalive_new++;
     },
 
+    abort: function () {
+        clearTimeout(this.wd_handle);
+        this.restart_n++;
+        this.log("hs::reload");
+        this.watchable = false;
+        delete this.ifra;
+    },
+
+    reload: function () {
+        this.abort();
+        this.keepalives_equal = 0;
+        this.ifra = this.doc.createElement("iframe");
+        this.ifra.style.visibility = "hidden";
+        this.doc.body.appendChild(this.ifra);
+        this.start(this.page);
+    },
+
     log: function (s) {
         if (this.console != null) {
             return (this.console.log(s));