content/base/test/delayedServerEvents.sjs

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

     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