|
1 // this will take strings_to_send.length*500 ms = 5 sec |
|
2 |
|
3 var timer = null; |
|
4 var strings_to_send = ["retry:999999999\ndata\r\n\nda", "ta", ":", "de", "layed1\n\n", |
|
5 "", |
|
6 "", |
|
7 "data:delayed2\n\n", "", ""]; |
|
8 var resp = null; |
|
9 |
|
10 function sendNextString() |
|
11 { |
|
12 if (strings_to_send.length == 0) { |
|
13 timer.cancel(); |
|
14 resp.finish(); |
|
15 timer = null; |
|
16 resp = null; |
|
17 return; |
|
18 } |
|
19 |
|
20 resp.write(strings_to_send[0]); |
|
21 strings_to_send = strings_to_send.splice(1, strings_to_send.length - 1); |
|
22 } |
|
23 |
|
24 function handleRequest(request, response) |
|
25 { |
|
26 var b = 0; |
|
27 for (var i=0; i < strings_to_send.length; ++i) { |
|
28 b += strings_to_send[i].length; |
|
29 } |
|
30 |
|
31 response.seizePower(); |
|
32 response.write("HTTP/1.1 200 OK\r\n") |
|
33 response.write("Content-Length: " + b + "\r\n"); |
|
34 response.write("Content-Type: text/event-stream; charset=utf-8\r\n"); |
|
35 response.write("Cache-Control: no-cache, must-revalidate\r\n"); |
|
36 response.write("\r\n"); |
|
37 |
|
38 resp = response; |
|
39 |
|
40 timer = Components.classes["@mozilla.org/timer;1"].createInstance(Components.interfaces.nsITimer); |
|
41 timer.initWithCallback(sendNextString, 500, Components.interfaces.nsITimer.TYPE_REPEATING_SLACK); |
|
42 } |