Continue working on NWS
This commit is contained in:
parent
9eafc53c91
commit
44fa360c60
2 changed files with 104 additions and 28 deletions
|
|
@ -1,5 +1,19 @@
|
|||
var NotWebsocket = function() {
|
||||
this.connected = false;
|
||||
$.getJSON(WEB_URL + "/nws/connect", function(data) {
|
||||
console.log(data);
|
||||
this.hash = data;
|
||||
this.connected = true;
|
||||
this.recv(["connect", undefined]);
|
||||
this.pollint = setInterval(function() {
|
||||
this.poll();
|
||||
}.bind(this), 500);
|
||||
}.bind(this));
|
||||
|
||||
this.handlers = {};
|
||||
}
|
||||
|
||||
NotWebsocket.prototype.reconnect = function() {
|
||||
$.getJSON(WEB_URL + "/nws/connect", function(data) {
|
||||
this.hash = data;
|
||||
this.connected = true;
|
||||
|
|
@ -7,9 +21,14 @@ var NotWebsocket = function() {
|
|||
this.pollint = setInterval(function() {
|
||||
this.poll();
|
||||
}.bind(this), 100);
|
||||
}.bind(this))
|
||||
.fail(function() {
|
||||
if(this.reconndelay < 10000)
|
||||
this.reconndelay += 100;
|
||||
setTimeout(function() {
|
||||
this.reconnect();
|
||||
}.bind(this), this.reconndelay);
|
||||
}.bind(this));
|
||||
|
||||
this.handlers = {};
|
||||
}
|
||||
|
||||
NotWebsocket.prototype.emit = function(msg, data) {
|
||||
|
|
@ -17,9 +36,10 @@ NotWebsocket.prototype.emit = function(msg, data) {
|
|||
setTimeout(function() {
|
||||
this.emit(msg, data);
|
||||
}.bind(this), 100);
|
||||
return;
|
||||
}
|
||||
var pkt = [msg, data];
|
||||
var str = escape(JSON.stringify(pkt));
|
||||
var str = escape(JSON.stringify(pkt)).replace(/\//g, "%2F");
|
||||
$.getJSON(WEB_URL+"/nws/"+this.hash+"/"+str, function(){});
|
||||
}
|
||||
|
||||
|
|
@ -33,18 +53,34 @@ 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);
|
||||
for(var i = 0; i < data.length; i++) {
|
||||
console.log("DBG", data[i]);
|
||||
this.recv(data[i]);
|
||||
try {
|
||||
this.recv(data[i]);
|
||||
}
|
||||
catch(e) { }
|
||||
}
|
||||
}.bind(this))
|
||||
.fail(function() {
|
||||
this.disconnect();
|
||||
}.bind(this));
|
||||
}
|
||||
|
||||
NotWebsocket.prototype.recv = function(pkt) {
|
||||
var msg = pkt[0], data = pkt[1];
|
||||
if(!(msg in this.handlers))
|
||||
if(!(msg in this.handlers)) {
|
||||
return;
|
||||
}
|
||||
for(var i = 0; i < this.handlers[msg].length; i++) {
|
||||
this.handlers[msg][i](data);
|
||||
}
|
||||
}
|
||||
|
||||
NotWebsocket.prototype.disconnect = function() {
|
||||
this.recv(["disconnect", undefined]);
|
||||
clearInterval(this.pollint);
|
||||
this.connected = false;
|
||||
this.reconndelay = 100;
|
||||
this.reconnect();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue