From cc1b378c3d1f7a230c33607eeaa7a48f8a2a0798 Mon Sep 17 00:00:00 2001 From: calzoneman Date: Tue, 4 Jun 2013 12:11:16 -0400 Subject: [PATCH] NWS fixes --- notwebsocket.js | 9 +++++---- www/assets/js/notwebsocket.js | 17 +++++++++++------ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/notwebsocket.js b/notwebsocket.js index ca2c72bb..f1b65f91 100644 --- a/notwebsocket.js +++ b/notwebsocket.js @@ -28,8 +28,6 @@ NotWebsocket.prototype.poll = function() { q.push(this.pktqueue[i]); } this.pktqueue.length = 0; - if(q.length > 0) - console.log("sending", q.length); return q; } @@ -106,6 +104,7 @@ var rooms = {}; function newConnection(req, res) { var nws = new NotWebsocket(); clients[nws.hash] = nws; + res.callback = req.query.callback; sendJSON(res, nws.hash); return nws; } @@ -114,11 +113,13 @@ exports.newConnection = newConnection; function msgReceived(req, res) { var h = req.params.hash; if(h in clients && clients[h] != null) { - if(req.params.str == "poll") { + var str = req.params.str; + res.callback = req.query.callback; + if(str == "poll") { sendJSON(res, clients[h].poll()); } else { - clients[h].recv(unescape(req.params.str)); + clients[h].recv(unescape(str)); sendJSON(res, ""); } } diff --git a/www/assets/js/notwebsocket.js b/www/assets/js/notwebsocket.js index 59d3ae1e..67d6765a 100644 --- a/www/assets/js/notwebsocket.js +++ b/www/assets/js/notwebsocket.js @@ -7,7 +7,7 @@ var NotWebsocket = function() { this.recv(["connect", undefined]); this.pollint = setInterval(function() { this.poll(); - }.bind(this), 500); + }.bind(this), 100); }.bind(this)); this.handlers = {}; @@ -52,17 +52,20 @@ NotWebsocket.prototype.on = function(msg, callback) { NotWebsocket.prototype.poll = function() { if(!this.connected) return; - $.getJSON(WEB_URL+"/nws/"+this.hash+"/poll", function(data) { - if(data.length > 0) - console.log("receiving", data.length); + if(this.polling) + return false; + $.getJSON(WEB_URL+"/nws/"+this.hash+"/poll?callback=?", function(data) { + this.polling = true; for(var i = 0; i < data.length; i++) { try { this.recv(data[i]); } catch(e) { } } + this.polling = false; }.bind(this)) .fail(function() { + console.log(arguments); this.disconnect(); }.bind(this)); } @@ -81,6 +84,8 @@ NotWebsocket.prototype.disconnect = function() { this.recv(["disconnect", undefined]); clearInterval(this.pollint); this.connected = false; - this.reconndelay = 100; - this.reconnect(); + this.reconndelay = 1000; + setTimeout(function() { + this.reconnect(); + }.bind(this), this.reconndelay); }