switch to json, which turns out to be faster than flashsocket
authorMike Shaver <shaver@mozilla.org>
Sat, 29 Jan 2011 00:34:31 +0000
changeset 29 9c999d1d3d81
parent 28 32b430ddb1b3
child 30 1d783f4418e5
push id22
push usershaver@mozilla.com
push dateSat, 29 Jan 2011 00:37:43 +0000
switch to json, which turns out to be faster than flashsocket
server/entry.html
server/websocket.js
--- a/server/entry.html
+++ b/server/entry.html
@@ -5,72 +5,69 @@
 <style>
 #matchingbugs {
   font-family: helvetica, arial, sans-serif;
 /*  font-size: .8em; */
   padding-bottom: 40px;
 }
 </style>
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
-<script src="/socket.io/socket.io.js"></script>
   </head>
   <body>
 <div id="controls">
 <hr />
 <input type="text" id="keywords"/> <button id="submitton">Search</button> &larr; type 4-letter-or-longer words here to search bugzilla (space or enter ends words, click button for mobile)
 </div>
 <div id="matchingbugs"> </div>
     <script type="text/javascript">
-var conn;
 var results;
 $(function(){
   $("#keywords").focus();
 
   $(window).bind("focus", function(){
     $("#keywords").focus();
   });
 
   function sendmsg(){
-    if(conn && conn.connected){
-      var msg = $("#keywords").val();
-      conn.sendTime = Date.now();
-      conn.send(msg);
-    } else {
-      conn.connect();
-    }
+    var terms = $("#keywords").val();
+    location.hash = "#" + encodeURIComponent(terms);
+    var xhr = new XMLHttpRequest();
+    xhr.open("GET", "/json?" + encodeURI(terms), true);
+    xhr.onreadystatechange = function() {
+        if (xhr.readyState != 4 || xhr.status != 200)
+            return;
+        var results = JSON.parse(xhr.responseText);
+        var div = $("#matchingbugs");
+        div.html(results.bugs.length + " results for '" + $("#keywords").val() + "' in " +  (Date.now() - xhr.sendTime) + " (total)/" +
+                 results.times.server + " (server)/" + results.times.redis + " (redis) ms<br>");
+
+        results.bugs.forEach(function (v) { div.append("<a href='https://bugzilla.mozilla.org/show_bug.cgi?id=" + v.id + "'>" + v.id + "</a>: ");
+        div.append(document.createTextNode(v.summary)); div.append("<br>"); });
+        if (results.hasMore)
+            div.append("<br>(Showing " + results.bugs.length + " of " + results.totalResults + " results)<br>");
+        };
+    xhr.sendTime = Date.now();
+    xhr.send(null);
   };
 
   $("#submitton").bind("click", sendmsg);
 
   $("#keywords").bind("keydown", function(e){
 
  if(e.keyCode == 32 || e.keyCode == 13 ||
    (e.keyCode == 8 && $("#keywords").val().substr(-2, 1) == " "))
 {
       sendmsg();
   }});
 
-  if (location.search)
-      $("#keywords").val(location.search.substring(1)); 
+  if (location.hash) {
+      $("#keywords").val(decodeURIComponent(location.hash.substring(1)));
+      sendmsg();
+  }
 
   function log(data){
     $("#matchingbugs").html(data);
   };
 
-  conn = new io.Socket(document.domain, { rememberTransport: false });
-  conn.on("connecting", function(type) { $("#matchingbugs").html("Trying <i>" + type + "</i>"); });
-  conn.on("connect", sendmsg);
-  conn.on("message",  function(data) {
-  results = JSON.parse(data);
-      var div = $("#matchingbugs");
-      div.html(results.bugs.length + " results for '" + $("#keywords").val() + "' in " +  (Date.now() - conn.sendTime) + " (total)/" +
-               results.times.server + " (server)/" + results.times.redis + " (redis) ms<br>");
-
-      results.bugs.forEach(function (v) { div.append("<a href='https://bugzilla.mozilla.org/show_bug.cgi?id=" + v.id + "'>" + v.id + "</a>: ");
-div.append(document.createTextNode(v.summary)); div.append("<br>"); });
-      if (results.hasMore)
-         div.append("<br>(Showing " + results.bugs.length + " of " + results.totalResults + " results)<br>");
-    });
-  conn.connect();
 });
     </script>
   </body>
 </html>
--- a/server/websocket.js
+++ b/server/websocket.js
@@ -86,17 +86,17 @@ function handleRequest(req, res)
     if (req.method == "GET" && (pathname == "/" || pathname == "/entry.html")) {
         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));
     }
     
     if (req.method == "GET" && pathname == "/json") {
-        terms = urlx.query;
+        terms = decodeURIComponent(urlx.query);
         res.writeHead(200, {'Content-Type': 'text/plain'});
         runSearch(terms, function (data) { res.write(data); res.end(); });
     }
            
 }
 
 var httpServer = http.createServer(handleRequest);
 
@@ -111,8 +111,9 @@ socket.on("clientConnect",
               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);
+