added keepalive

This commit is contained in:
hydrabolt
2015-08-23 22:12:07 +01:00
parent ac3735d7c2
commit 31a0cba0d1
3 changed files with 38 additions and 64 deletions

View File

@@ -52,9 +52,16 @@ var Client = (function () {
}
_createClass(Client, [{
key: "debug",
key: "sendPacket",
value: function sendPacket(JSONObject) {
if (this.websocket.readyState === 1) {
this.websocket.send(JSON.stringify(JSONObject));
}
}
//def debug
}, {
key: "debug",
value: function debug(message) {
console.log(message);
}
@@ -68,6 +75,15 @@ var Client = (function () {
value: function off(event, fn) {
this.events["delete"](event);
}
}, {
key: "keepAlive",
value: function keepAlive() {
this.debug("keep alive triggered");
this.sendPacket({
op: 1,
d: Date.now()
});
}
//def trigger
}, {
@@ -190,6 +206,10 @@ var Client = (function () {
self.trigger("ready");
self.debug("cached " + self.serverCache.size + " servers, " + self.channelCache.size + " channels and " + self.userCache.size + " users.");
setInterval(function () {
self.keepAlive.apply(self);
}, data.heartbeat_interval);
break;
default:
self.debug("received unknown packet");