From 42388147751d6d298efee06a9c1f243e5a979ab9 Mon Sep 17 00:00:00 2001 From: Vlad Glavtchev Date: Wed, 2 May 2018 20:42:30 -0700 Subject: [PATCH] Fix race condition in sending a response before the request has been handled by the server. --- web/channel_server.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/web/channel_server.js b/web/channel_server.js index f7bf1b5..a21e524 100644 --- a/web/channel_server.js +++ b/web/channel_server.js @@ -123,14 +123,15 @@ function requestListener(request, response) { request.on("end", function () { console.log("@" + sessionId + " - " + userId + " => " + peerId + " :"); // console.log(body); + + // to avoid "no element found" warning in Firefox (bug 521301) + headers["Content-Type"] = "text/plain"; + response.writeHead(204, headers); + response.end(); + var evtdata = "data:" + body.replace(/\n/g, "\ndata:") + "\n"; peer.esResponse.write("event:user-" + userId + "\n" + evtdata + "\n"); }); - - // to avoid "no element found" warning in Firefox (bug 521301) - headers["Content-Type"] = "text/plain"; - response.writeHead(204, headers); - response.end(); } return;