move response generation to a callback model, prepping for JSON output
authorMike Shaver <shaver@mozilla.org>
Fri, 28 Jan 2011 23:33:01 +0000
changeset 27 925f3a6b49a73e4868a0ad1edd6cc35af5407e5f
parent 26 ae8b3ff677cadc92b6d51299062fd2a5aee4e4ce
child 28 32b430ddb1b399e8b76be7f23cdd70f5f87c8dd3
push id22
push usershaver@mozilla.com
push dateSat, 29 Jan 2011 00:37:43 +0000
move response generation to a callback model, prepping for JSON output
server/websocket.js
--- a/server/websocket.js
+++ b/server/websocket.js
@@ -9,29 +9,29 @@ var indexer = require('../lib/indexer');
 
 var redis = require('redis');
 var redisConn = redis.createClient();
 
 redisConn.on("error",  function (err) {
     util.log("Redis connection error to " + redisConn.host + ":" + redisConn.port + " - " + err);
 });
 
-function runSearch(message, conn)
+function runSearch(message, cb)
 {
-    util.log("searching for " + message)
+
     var startTime = Date.now();
     var resultmsg = { bugs: [] };
 
     function sendResponse()
     {
         var now = Date.now();
         resultmsg.times = { redis: (now - redisStartTime) || 0, server: now - startTime };
 
-        conn.send(JSON.stringify(resultmsg));
-        util.log(conn.id + ": search " + words.length + " -> " + (resultmsg.totalResults || resultmsg.bugs.length) + " in " +
+        cb(JSON.stringify(resultmsg));
+        util.log("search " + words.length + "/" + message + " -> " + (resultmsg.totalResults || resultmsg.bugs.length) + " in " +
                     resultmsg.times.redis+ "/" + resultmsg.times.server + " ms");
     }
 
     var words = indexer.keywordsForSummary(message);
     if (words.length == 0)
         return sendResponse();
 
     var redisStartTime = Date.now();
@@ -78,32 +78,34 @@ function runSearch(message, conn)
 
 const ENTRYHTML_PATH = path.normalize(path.join(__dirname, "entry.html"));
 
 function handleRequest(req, res)
 {
     var pathname = url.parse(req.url).pathname;
     
     if (req.method == "GET" && (pathname == "/" || pathname == "/entry.html")) {
-        res.writeHead(200, {'Content-Type': 'text/html', 'Connection': 'close'});
+        res.writeHead(200, {'Content-Type': 'text/html'});
         fs.createReadStream(ENTRYHTML_PATH, { flags: 'r', bufferSize: 4*1024 }).
             addListener('data', function(chunk) { res.write(chunk); }).
             addListener('end', res.end.bind(res));
     }
+           
 }
 
 var httpServer = http.createServer(handleRequest);
 
 var clientCount = 0;
 
 var socket = io.listen(httpServer);
 socket.on("clientConnect",
           function(client) {
               util.log("connect (" + ++clientCount + ")");
-              client.on("message", function(message) { runSearch(message, client); });
+              client.on("message", function(message) { runSearch(message, 
+                                          function(data) { client.send(data) }); });
               client.on("error", function(err) { util.log("ERROR " + err); });
               client.on("disconnect", function() { util.log ("disconnect (" + --clientCount + ")")});
           }); 
 
 httpServer.on("close", function(conn) { util.log("connection closed"); });
 httpServer.on("error", function (conn, err) { util.log("server error: " + err); });
 
 httpServer.listen(parseInt(process.argv[2]) || 8000);