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