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.

     1 // this will take strings_to_send.length*500 ms = 5 sec
     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;
    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   }
    20   resp.write(strings_to_send[0]);
    21   strings_to_send = strings_to_send.splice(1, strings_to_send.length - 1);
    22 }
    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   }
    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");
    38   resp = response;
    40   timer = Components.classes["@mozilla.org/timer;1"].createInstance(Components.interfaces.nsITimer);
    41   timer.initWithCallback(sendNextString, 500, Components.interfaces.nsITimer.TYPE_REPEATING_SLACK);
    42 }

mercurial