6673d34e3111f109bf3e215303e4d1e494056e0b
[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     $header = array();
71
72     $body = $transp->init("plain", $header, $header_out, $init_string, "", "0");
73
74     foreach ($header_out as $key => $value) {
75         header(sprintf("%s: %s", $key, $value));
76     }
77     print($body);
78     mop_flush();
79
80     switch ($f_test) {
81     case 1:
82         // from 1 to 9 into the innerHTML and than close
83         for ($i = 1 ; $i < 10 ; $i++) {
84             $chunk = $transp->chunk($i, sprintf("\$('container').innerHTML = '%d';", $i));
85             print($chunk);
86             mop_flush();
87             sleep(1);
88         }
89         break;
90     case 2:
91         // from 1 to 9 into the innerHTML and than close
92         for ($i = 1 ; $i < 10 ; $i++) {
93             $chunk = $transp->chunk($i, sprintf("gst.st++; \$('container').innerHTML = gst.st;"));
94             print($chunk);
95             mop_flush();
96             sleep(1);
97         }
98         break;
99     case 3:
100         // from 1 to 9 with 60 secs after 8, the client js api must restart stream after 12 secs
101         for ($i = 1 ; $i < 10 ; $i++) {
102             $chunk = $transp->chunk($i, sprintf("\$('container').innerHTML = '%d';", $i));
103             print($chunk);
104             mop_flush();
105             sleep(1);
106             if ($i == 8)
107                 sleep(60);
108         }
109         break;
110     case 4:
111         // from 1 to 9 into the innerHTML and than close
112         for ($i = 1 ; $i < 10 ; $i++) {
113             if ($i != 5) {
114                 $chunk = $transp->chunk($i, sprintf("\$('container').innerHTML = '%d';", $i));
115             }
116             else {
117                 $chunk = $transp->chunk($i, sprintf("\$('container').innerHTML = '%d';|sleep(gst,3000);", $i));
118             }
119             print($chunk);
120             mop_flush();
121             sleep(1);
122         }
123         break;
124     case 5:
125         // from 1 to 9 into the innerHTML and than close
126         $cont = array('@BEGIN@', '@END@', '@BEGIN@ sleep(1); @END@');
127         for ($i = 1 ; $i < 10 ; $i++) {
128             switch($i) {
129             case 6:
130             case 7:
131             case 8:
132                 $chunk = $transp->chunk($i, sprintf("\$('container').innerHTML = '%s';", xcape($cont[$i - 6])));
133                 break;
134             default:
135                 $chunk = $transp->chunk($i, sprintf("\$('container').innerHTML = '%d';", $i));
136                 break;
137             }
138             print($chunk);
139             mop_flush();
140             if ($i < 9)
141                 sleep(1);
142         }
143         break;
144     case 6:
145         // from 1 to 9 into the innerHTML and than close
146         if ($step == 8) {
147             $chunk = $transp->chunk(1, sprintf("gst.st++; \$('container').innerHTML = gst.st;"));
148             print($chunk);
149             // without this usleep the delay is doubled in iframe stream because 
150             // no transp.xynt_streaming back-set is performed
151             usleep(250000);
152             mop_flush();
153         }
154         else {
155             for ($i = 1 ; $i < 10 ; $i++) {
156                 $chunk = $transp->chunk($i, sprintf("gst.st++; \$('container').innerHTML = gst.st;"));
157                 print($chunk);
158                 mop_flush();
159                 if ($i < 9)
160                     sleep(1);
161             }
162         }
163         break;
164     }
165     exit;
166 }
167 ?>
168 <html>
169 <head>
170 <title>XYNT TEST01</title>
171 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
172 <script type="text/javascript" src="xynt-streaming.js"></script>
173 <script type="text/javascript" src="commons.js"></script>
174 <script type="text/javascript" src="heartbit.js"></script>
175
176 <!-- <script type="text/javascript" src="myconsole.js"></script> -->
177
178 <script type="text/javascript"><!--
179      var sess = "for_test";
180      var stat = "";
181      var subst = "";
182      var gst = new globst();
183      window.onload = function() {
184
185      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);});
186      xstm.hbit_set(heartbit);
187      xstm.start();
188  }
189  //-->
190 </script>
191 </head>
192 <body>
193 <div>
194 <?php
195
196
197
198 printf("<table>");
199 for ($test = 1 ; $test <= count($desc) ; $test++) {
200     printf("<tr>");
201     foreach ($transs as $trans) {
202         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);
203     }
204     printf("</tr>\n");
205 }
206 printf("</table>");
207 printf("<br>[%s]<br>Test: %d<br>", $f_trans, $f_test);
208 ?>
209 </div>
210 <div>
211 <b>Descrizione</b>: <?php echo $desc[$f_test - 1]; ?>
212 </div>
213
214 <div>
215 <b>Status</b>: <img id="stm_stat" class="nobo" style="vertical-align: bottom;" src="img/line-status_b.png"></div>
216
217 </div>
218 <div>
219 <b>Counter</b>: <span id="container">
220 BEGIN
221 </span>
222 </div>
223 </body>
224 </html>