1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/base/test/delayedServerEvents.sjs Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,42 @@ 1.4 +// this will take strings_to_send.length*500 ms = 5 sec 1.5 + 1.6 +var timer = null; 1.7 +var strings_to_send = ["retry:999999999\ndata\r\n\nda", "ta", ":", "de", "layed1\n\n", 1.8 + "", 1.9 + "", 1.10 + "data:delayed2\n\n", "", ""]; 1.11 +var resp = null; 1.12 + 1.13 +function sendNextString() 1.14 +{ 1.15 + if (strings_to_send.length == 0) { 1.16 + timer.cancel(); 1.17 + resp.finish(); 1.18 + timer = null; 1.19 + resp = null; 1.20 + return; 1.21 + } 1.22 + 1.23 + resp.write(strings_to_send[0]); 1.24 + strings_to_send = strings_to_send.splice(1, strings_to_send.length - 1); 1.25 +} 1.26 + 1.27 +function handleRequest(request, response) 1.28 +{ 1.29 + var b = 0; 1.30 + for (var i=0; i < strings_to_send.length; ++i) { 1.31 + b += strings_to_send[i].length; 1.32 + } 1.33 + 1.34 + response.seizePower(); 1.35 + response.write("HTTP/1.1 200 OK\r\n") 1.36 + response.write("Content-Length: " + b + "\r\n"); 1.37 + response.write("Content-Type: text/event-stream; charset=utf-8\r\n"); 1.38 + response.write("Cache-Control: no-cache, must-revalidate\r\n"); 1.39 + response.write("\r\n"); 1.40 + 1.41 + resp = response; 1.42 + 1.43 + timer = Components.classes["@mozilla.org/timer;1"].createInstance(Components.interfaces.nsITimer); 1.44 + timer.initWithCallback(sendNextString, 500, Components.interfaces.nsITimer.TYPE_REPEATING_SLACK); 1.45 +}