2013-06-04 03:56:06 +00:00
|
|
|
var NotWebsocket = function() {
|
|
|
|
this.connected = false;
|
|
|
|
$.getJSON(WEB_URL + "/nws/connect", function(data) {
|
2013-06-04 15:46:06 +00:00
|
|
|
console.log(data);
|
2013-06-04 03:56:06 +00:00
|
|
|
this.hash = data;
|
|
|
|
this.connected = true;
|
|
|
|
this.recv(["connect", undefined]);
|
|
|
|
this.pollint = setInterval(function() {
|
|
|
|
this.poll();
|
2013-06-04 15:46:06 +00:00
|
|
|
}.bind(this), 500);
|
2013-06-04 03:56:06 +00:00
|
|
|
}.bind(this));
|
|
|
|
|
|
|
|
this.handlers = {};
|
|
|
|
}
|
|
|
|
|
2013-06-04 15:46:06 +00:00
|
|
|
NotWebsocket.prototype.reconnect = function() {
|
|
|
|
$.getJSON(WEB_URL + "/nws/connect", function(data) {
|
|
|
|
this.hash = data;
|
|
|
|
this.connected = true;
|
|
|
|
this.recv(["connect", undefined]);
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2013-06-04 03:56:06 +00:00
|
|
|
NotWebsocket.prototype.emit = function(msg, data) {
|
|
|
|
if(!this.connected) {
|
|
|
|
setTimeout(function() {
|
|
|
|
this.emit(msg, data);
|
|
|
|
}.bind(this), 100);
|
2013-06-04 15:46:06 +00:00
|
|
|
return;
|
2013-06-04 03:56:06 +00:00
|
|
|
}
|
|
|
|
var pkt = [msg, data];
|
2013-06-04 15:46:06 +00:00
|
|
|
var str = escape(JSON.stringify(pkt)).replace(/\//g, "%2F");
|
2013-06-04 03:56:06 +00:00
|
|
|
$.getJSON(WEB_URL+"/nws/"+this.hash+"/"+str, function(){});
|
|
|
|
}
|
|
|
|
|
|
|
|
NotWebsocket.prototype.on = function(msg, callback) {
|
|
|
|
if(!(msg in this.handlers))
|
|
|
|
this.handlers[msg] = [];
|
|
|
|
this.handlers[msg].push(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
NotWebsocket.prototype.poll = function() {
|
|
|
|
if(!this.connected)
|
|
|
|
return;
|
|
|
|
$.getJSON(WEB_URL+"/nws/"+this.hash+"/poll", function(data) {
|
2013-06-04 15:46:06 +00:00
|
|
|
if(data.length > 0)
|
|
|
|
console.log("receiving", data.length);
|
2013-06-04 03:56:06 +00:00
|
|
|
for(var i = 0; i < data.length; i++) {
|
2013-06-04 15:46:06 +00:00
|
|
|
try {
|
|
|
|
this.recv(data[i]);
|
|
|
|
}
|
|
|
|
catch(e) { }
|
2013-06-04 03:56:06 +00:00
|
|
|
}
|
2013-06-04 15:46:06 +00:00
|
|
|
}.bind(this))
|
|
|
|
.fail(function() {
|
|
|
|
this.disconnect();
|
2013-06-04 03:56:06 +00:00
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
NotWebsocket.prototype.recv = function(pkt) {
|
|
|
|
var msg = pkt[0], data = pkt[1];
|
2013-06-04 15:46:06 +00:00
|
|
|
if(!(msg in this.handlers)) {
|
2013-06-04 03:56:06 +00:00
|
|
|
return;
|
2013-06-04 15:46:06 +00:00
|
|
|
}
|
2013-06-04 03:56:06 +00:00
|
|
|
for(var i = 0; i < this.handlers[msg].length; i++) {
|
|
|
|
this.handlers[msg][i](data);
|
|
|
|
}
|
|
|
|
}
|
2013-06-04 15:46:06 +00:00
|
|
|
|
|
|
|
NotWebsocket.prototype.disconnect = function() {
|
|
|
|
this.recv(["disconnect", undefined]);
|
|
|
|
clearInterval(this.pollint);
|
|
|
|
this.connected = false;
|
|
|
|
this.reconndelay = 100;
|
|
|
|
this.reconnect();
|
|
|
|
}
|