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");

View File

@@ -1,63 +0,0 @@
"use strict";
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var request = require("superagent");
var defaultOptions = {
cache_tokens: false
};
var Client = (function () {
function Client() {
var options = arguments.length <= 0 || arguments[0] === undefined ? defaultOptions : arguments[0];
var token = arguments.length <= 1 || arguments[1] === undefined ? undefined : arguments[1];
_classCallCheck(this, Client);
/*
When created, if a token is specified the Client will
try connecting with it. If the token is incorrect, no
further efforts will be made to connect.
*/
this.options = options;
this.token = token;
this.state = 0;
this.websocket = null;
this.events = new Map();
this.user = null;
/*
State values:
0 - idle
1 - logging in
2 - logged in
3 - ready
4 - disconnected
*/
}
_createClass(Client, [{
key: "login",
//def login
value: function login() {
var email = arguments.length <= 0 || arguments[0] === undefined ? "foo@bar.com" : arguments[0];
var password = arguments.length <= 1 || arguments[1] === undefined ? "pass1234s" : arguments[1];
if (this.state === 0 || this.state === 4) {
this.state = 1;
request.post();
}
}
}, {
key: "ready",
get: function get() {
return this.state === 3;
}
}]);
return Client;
})();