updated xynt_test01 with websocket transport
[brisk.git] / web / xynt_test01.php
1 <?php
2
3 $desc = array( "Semplice: da 1 a 9 ogni secondo, poi ricomincia (status sempre verde).",
4                "Continuo: da 1 a N ogni secondo, ricomincia ogni 9 (status sempre verde).",
5                "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).",
6                "Pausa: da 1 a 5 ogni secondo, pausa 3 secondi, e poi 8 e 9 ogni secondo, e poi ricomincia (status sempre verde).",
7                "Keyword: da 1 a 5 ogni secondo, @BEGIN@, @END@, @BEGIN@ xxx yyy @END@, 9, (status sempre verde).",
8                "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).");
9
10
11 $transs = array( "iframe", "websocket", "xhr", "htmlfile" );
12 if (!isset($f_trans))
13     $f_trans = $transs[0];
14
15 if (!isset($f_test))
16     $f_test = 1;
17
18
19 function mop_flush()
20 {
21     for ($i = 0; $i < ob_get_level(); $i++)
22         ob_end_flush();
23     ob_implicit_flush(1);
24     flush();
25 }
26
27 $escpush_from = array("\\", "\"");
28 $escpush_to   = array("\\\\", "\\\"");
29 function escpush($s)
30 {
31     GLOBAL $escpush_from, $escpush_to;
32
33     return str_replace($escpush_from, $escpush_to, $s);
34 }
35
36 function xcape($s)
37 {
38   $from = array (   '\\',     '@',        '|' );
39   $to   = array ( '\\\\', '&#64;', '&brvbar;' );
40
41   return (str_replace($from, $to, htmlentities($s,ENT_COMPAT,"UTF-8")));
42 }
43
44 if ($isstream == "true") {
45
46     require_once("Obj/transports.phh");
47
48
49     if (isset($transp) && $transp == "websocket") {
50         $transp = new Transport_websocket();
51     }
52     else if (isset($transp) && $transp == "xhr") {
53         $transp = new Transport_xhr();
54     }
55     else if (isset($transp) && $transp == "htmlfile") {
56         $transp = new Transport_htmlfile();
57     }
58     else {
59         $transp = new Transport_iframe();
60     }
61     $header_out = array();
62
63     $init_string = "";
64     for ($i = 0 ; $i < 4096 ; $i++) {
65         if (($i % 128) == 0)
66             $init_string .= " ";
67         else
68             $init_string .= chr(mt_rand(65, 90));
69     }
70
71     $body = $transp->init("plain", $header_out, $init_string, "", "0");
72
73     foreach ($header_out as $key => $value) {
74         header(sprintf("%s: %s", $key, $value));
75     }
76     print($body);
77     mop_flush();
78
79     switch ($f_test) {
80     case 1:
81         // from 1 to 9 into the innerHTML and than close
82         for ($i = 1 ; $i < 10 ; $i++) {
83             $chunk = $transp->chunk($i, sprintf("\$('container').innerHTML = '%d';", $i));
84             print($chunk);
85             mop_flush();
86             sleep(1);
87         }
88         break;
89     case 2:
90         // from 1 to 9 into the innerHTML and than close
91         for ($i = 1 ; $i < 10 ; $i++) {
92             $chunk = $transp->chunk($i, sprintf("gst.st++; \$('container').innerHTML = gst.st;"));
93             print($chunk);
94             mop_flush();
95             sleep(1);
96         }
97         break;
98     case 3:
99         // from 1 to 9 with 60 secs after 8, the client js api must restart stream after 12 secs
100         for ($i = 1 ; $i < 10 ; $i++) {
101             $chunk = $transp->chunk($i, sprintf("\$('container').innerHTML = '%d';", $i));
102             print($chunk);
103             mop_flush();
104             sleep(1);
105             if ($i == 8)
106                 sleep(60);
107         }
108         break;
109     case 4:
110         // from 1 to 9 into the innerHTML and than close
111         for ($i = 1 ; $i < 10 ; $i++) {
112             if ($i != 5) {
113                 $chunk = $transp->chunk($i, sprintf("\$('container').innerHTML = '%d';", $i));
114             }
115             else {
116                 $chunk = $transp->chunk($i, sprintf("\$('container').innerHTML = '%d';|sleep(gst,3000);", $i));
117             }
118             print($chunk);
119             mop_flush();
120             sleep(1);
121         }
122         break;
123     case 5:
124         // from 1 to 9 into the innerHTML and than close
125         $cont = array('@BEGIN@', '@END@', '@BEGIN@ sleep(1); @END@');
126         for ($i = 1 ; $i < 10 ; $i++) {
127             switch($i) {
128             case 6:
129             case 7:
130             case 8:
131                 $chunk = $transp->chunk($i, sprintf("\$('container').innerHTML = '%s';", xcape($cont[$i - 6])));
132                 break;
133             default:
134                 $chunk = $transp->chunk($i, sprintf("\$('container').innerHTML = '%d';", $i));
135                 break;
136             }
137             print($chunk);
138             mop_flush();
139             if ($i < 9)
140                 sleep(1);
141         }
142         break;
143     case 6:
144         // from 1 to 9 into the innerHTML and than close
145         if ($step == 8) {
146             $chunk = $transp->chunk(1, sprintf("gst.st++; \$('container').innerHTML = gst.st;"));
147             print($chunk);
148             // without this usleep the delay is doubled in iframe stream because 
149             // no transp.xynt_streaming back-set is performed
150             usleep(250000);
151             mop_flush();
152         }
153         else {
154             for ($i = 1 ; $i < 10 ; $i++) {
155                 $chunk = $transp->chunk($i, sprintf("gst.st++; \$('container').innerHTML = gst.st;"));
156                 print($chunk);
157                 mop_flush();
158                 if ($i < 9)
159                     sleep(1);
160             }
161         }
162         break;
163     }
164     exit;
165 }
166 ?>
167 <html>
168 <head>
169 <title>XYNT TEST01</title>
170 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
171 <script type="text/javascript" src="xynt-streaming.js"></script>
172 <script type="text/javascript" src="commons.js"></script>
173 <script type="text/javascript" src="heartbit.js"></script>
174
175 <!-- <script type="text/javascript" src="myconsole.js"></script> -->
176
177 <script type="text/javascript"><!--
178      var sess = "for_test";
179      var stat = "";
180      var subst = "";
181      var gst = new globst();
182      window.onload = function() {
183
184      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);});
185      xstm.hbit_set(heartbit);
186      xstm.start();
187  }
188  //-->
189 </script>
190 </head>
191 <body>
192 <div>
193 <?php
194
195
196
197 printf("<table>");
198 for ($test = 1 ; $test <= count($desc) ; $test++) {
199     printf("<tr>");
200     foreach ($transs as $trans) {
201         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);
202     }
203     printf("</tr>\n");
204 }
205 printf("</table>");
206 printf("<br>[%s]<br>Test: %d<br>", $f_trans, $f_test);
207 ?>
208 </div>
209 <div>
210 <b>Descrizione</b>: <?php echo $desc[$f_test - 1]; ?>
211 </div>
212
213 <div>
214 <b>Status</b>: <img id="stm_stat" class="nobo" style="vertical-align: bottom;" src="img/line-status_b.png"></div>
215
216 </div>
217 <div>
218 <b>Counter</b>: <span id="container">
219 BEGIN
220 </span>
221 </div>
222 </body>
223 </html>