NWS fixes

This commit is contained in:
calzoneman 2013-06-04 12:11:16 -04:00
parent 44fa360c60
commit cc1b378c3d
2 changed files with 16 additions and 10 deletions

View File

@ -28,8 +28,6 @@ NotWebsocket.prototype.poll = function() {
q.push(this.pktqueue[i]); q.push(this.pktqueue[i]);
} }
this.pktqueue.length = 0; this.pktqueue.length = 0;
if(q.length > 0)
console.log("sending", q.length);
return q; return q;
} }
@ -106,6 +104,7 @@ var rooms = {};
function newConnection(req, res) { function newConnection(req, res) {
var nws = new NotWebsocket(); var nws = new NotWebsocket();
clients[nws.hash] = nws; clients[nws.hash] = nws;
res.callback = req.query.callback;
sendJSON(res, nws.hash); sendJSON(res, nws.hash);
return nws; return nws;
} }
@ -114,11 +113,13 @@ exports.newConnection = newConnection;
function msgReceived(req, res) { function msgReceived(req, res) {
var h = req.params.hash; var h = req.params.hash;
if(h in clients && clients[h] != null) { 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()); sendJSON(res, clients[h].poll());
} }
else { else {
clients[h].recv(unescape(req.params.str)); clients[h].recv(unescape(str));
sendJSON(res, ""); sendJSON(res, "");
} }
} }

View File

@ -7,7 +7,7 @@ var NotWebsocket = function() {
this.recv(["connect", undefined]); this.recv(["connect", undefined]);
this.pollint = setInterval(function() { this.pollint = setInterval(function() {
this.poll(); this.poll();
}.bind(this), 500); }.bind(this), 100);
}.bind(this)); }.bind(this));
this.handlers = {}; this.handlers = {};
@ -52,17 +52,20 @@ NotWebsocket.prototype.on = function(msg, callback) {
NotWebsocket.prototype.poll = function() { NotWebsocket.prototype.poll = function() {
if(!this.connected) if(!this.connected)
return; return;
$.getJSON(WEB_URL+"/nws/"+this.hash+"/poll", function(data) { if(this.polling)
if(data.length > 0) return false;
console.log("receiving", data.length); $.getJSON(WEB_URL+"/nws/"+this.hash+"/poll?callback=?", function(data) {
this.polling = true;
for(var i = 0; i < data.length; i++) { for(var i = 0; i < data.length; i++) {
try { try {
this.recv(data[i]); this.recv(data[i]);
} }
catch(e) { } catch(e) { }
} }
this.polling = false;
}.bind(this)) }.bind(this))
.fail(function() { .fail(function() {
console.log(arguments);
this.disconnect(); this.disconnect();
}.bind(this)); }.bind(this));
} }
@ -81,6 +84,8 @@ NotWebsocket.prototype.disconnect = function() {
this.recv(["disconnect", undefined]); this.recv(["disconnect", undefined]);
clearInterval(this.pollint); clearInterval(this.pollint);
this.connected = false; this.connected = false;
this.reconndelay = 100; this.reconndelay = 1000;
this.reconnect(); setTimeout(function() {
this.reconnect();
}.bind(this), this.reconndelay);
} }