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]);
}
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, "");
}
}

View File

@ -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.reconndelay = 1000;
setTimeout(function() {
this.reconnect();
}.bind(this), this.reconndelay);
}