Merge branch 'master' into ws
[brisk.git] / web / xynt_test01.php
index 78aeca0..6673d34 100644 (file)
@@ -1,6 +1,14 @@
 <?php
 
-$transs = array( "iframe", "xhr", "htmlfile" );
+$desc = array( "Semplice: da 1 a 9 ogni secondo, poi ricomincia (status sempre verde).",
+               "Continuo: da 1 a N ogni secondo, ricomincia ogni 9 (status sempre verde).",
+               "Restart: da 1 a 8 ogni secondo, pausa 16 secondi (status passa ad arancione e poi a rosso), poi ricomincia (e status torna a verde).",
+               "Pausa: da 1 a 5 ogni secondo, pausa 3 secondi, e poi 8 e 9 ogni secondo, e poi ricomincia (status sempre verde).",
+               "Keyword: da 1 a 5 ogni secondo, @BEGIN@, @END@, @BEGIN@ xxx yyy @END@, 9, (status sempre verde).",
+               "Reload limiter: da 1 a 8 ogni secondo e chiude, 9 setta e chiude subito,<br>il client aspetta 3 secondi, e poi da 10 a N ogni secondo, (status sempre verde).");
+
+
+$transs = array( "iframe", "websocket", "xhr", "htmlfile" );
 if (!isset($f_trans))
     $f_trans = $transs[0];
 
@@ -25,12 +33,23 @@ function escpush($s)
     return str_replace($escpush_from, $escpush_to, $s);
 }
 
+function xcape($s)
+{
+  $from = array (   '\\',     '@',        '|' );
+  $to   = array ( '\\\\', '&#64;', '&brvbar;' );
+
+  return (str_replace($from, $to, htmlentities($s,ENT_COMPAT,"UTF-8")));
+}
+
 if ($isstream == "true") {
 
     require_once("Obj/transports.phh");
 
 
-    if (isset($transp) && $transp == "xhr") {
+    if (isset($transp) && $transp == "websocket") {
+        $transp = new Transport_websocket();
+    }
+    else if (isset($transp) && $transp == "xhr") {
         $transp = new Transport_xhr();
     }
     else if (isset($transp) && $transp == "htmlfile") {
@@ -48,8 +67,9 @@ if ($isstream == "true") {
         else
             $init_string .= chr(mt_rand(65, 90));
     }
+    $header = array();
 
-    $body = $transp->init("plain", $header_out, $init_string, "", "0");
+    $body = $transp->init("plain", $header, $header_out, $init_string, "", "0");
 
     foreach ($header_out as $key => $value) {
         header(sprintf("%s: %s", $key, $value));
@@ -68,6 +88,15 @@ if ($isstream == "true") {
         }
         break;
     case 2:
+        // from 1 to 9 into the innerHTML and than close
+        for ($i = 1 ; $i < 10 ; $i++) {
+            $chunk = $transp->chunk($i, sprintf("gst.st++; \$('container').innerHTML = gst.st;"));
+            print($chunk);
+            mop_flush();
+            sleep(1);
+        }
+        break;
+    case 3:
         // from 1 to 9 with 60 secs after 8, the client js api must restart stream after 12 secs
         for ($i = 1 ; $i < 10 ; $i++) {
             $chunk = $transp->chunk($i, sprintf("\$('container').innerHTML = '%d';", $i));
@@ -78,7 +107,7 @@ if ($isstream == "true") {
                 sleep(60);
         }
         break;
-    case 3:
+    case 4:
         // from 1 to 9 into the innerHTML and than close
         for ($i = 1 ; $i < 10 ; $i++) {
             if ($i != 5) {
@@ -92,6 +121,46 @@ if ($isstream == "true") {
             sleep(1);
         }
         break;
+    case 5:
+        // from 1 to 9 into the innerHTML and than close
+        $cont = array('@BEGIN@', '@END@', '@BEGIN@ sleep(1); @END@');
+        for ($i = 1 ; $i < 10 ; $i++) {
+            switch($i) {
+            case 6:
+            case 7:
+            case 8:
+                $chunk = $transp->chunk($i, sprintf("\$('container').innerHTML = '%s';", xcape($cont[$i - 6])));
+                break;
+            default:
+                $chunk = $transp->chunk($i, sprintf("\$('container').innerHTML = '%d';", $i));
+                break;
+            }
+            print($chunk);
+            mop_flush();
+            if ($i < 9)
+                sleep(1);
+        }
+        break;
+    case 6:
+        // from 1 to 9 into the innerHTML and than close
+        if ($step == 8) {
+            $chunk = $transp->chunk(1, sprintf("gst.st++; \$('container').innerHTML = gst.st;"));
+            print($chunk);
+            // without this usleep the delay is doubled in iframe stream because 
+            // no transp.xynt_streaming back-set is performed
+            usleep(250000);
+            mop_flush();
+        }
+        else {
+            for ($i = 1 ; $i < 10 ; $i++) {
+                $chunk = $transp->chunk($i, sprintf("gst.st++; \$('container').innerHTML = gst.st;"));
+                print($chunk);
+                mop_flush();
+                if ($i < 9)
+                    sleep(1);
+            }
+        }
+        break;
     }
     exit;
 }
@@ -102,6 +171,7 @@ if ($isstream == "true") {
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 <script type="text/javascript" src="xynt-streaming.js"></script>
 <script type="text/javascript" src="commons.js"></script>
+<script type="text/javascript" src="heartbit.js"></script>
 
 <!-- <script type="text/javascript" src="myconsole.js"></script> -->
 
@@ -113,7 +183,7 @@ if ($isstream == "true") {
      window.onload = function() {
 
      xstm = new xynt_streaming(window, "<?php echo "$f_trans";?>", null /* console */, gst, 'xynt_test01_php', 'sess', sess, null, 'xynt_test01.php?isstream=true&f_test=<?php echo "$f_test";?>', function(com){eval(com);});
-     /*     xstm.hbit_set(heartbit); */
+     xstm.hbit_set(heartbit);
      xstm.start();
  }
  //-->
@@ -123,13 +193,10 @@ if ($isstream == "true") {
 <div>
 <?php
 
-$desc = array( "Semplice: da 1 a 9 ogni secondo, poi ricomincia.",
-               "Restart: da 1 a 8 ogni secondo, pausa 16 secondi, poi ricomincia.",
-               "Pausa: da 1 a 5 ogni secondo, pausa 3 secondi, e poi 8 e 9 ogni secondo, e poi ricomincia.");
 
 
 printf("<table>");
-for ($test = 1 ; $test <= 3 ; $test++) {
+for ($test = 1 ; $test <= count($desc) ; $test++) {
     printf("<tr>");
     foreach ($transs as $trans) {
         printf("<td style=\"padding: 8px; border: 1px solid black;\"><a href=\"?f_trans=%s&f_test=%d\">Test %s %02d</a></td>", $trans, $test, $trans, $test);
@@ -142,6 +209,11 @@ printf("<br>[%s]<br>Test: %d<br>", $f_trans, $f_test);
 </div>
 <div>
 <b>Descrizione</b>: <?php echo $desc[$f_test - 1]; ?>
+</div>
+
+<div>
+<b>Status</b>: <img id="stm_stat" class="nobo" style="vertical-align: bottom;" src="img/line-status_b.png"></div>
+
 </div>
 <div>
 <b>Counter</b>: <span id="container">