content/base/test/delayedServerEvents.sjs

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial