diff --git a/lib/index.js b/lib/index.js index d5188304..64db3b7b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1585,7 +1585,8 @@ function init(client, opts) { }; client._connecting = true; - getToken(client, opts); + setupPing(client.internals); + return getToken(client, opts); } function getToken(client, opts) { if (opts.token) return getGateway(client, opts, opts.token); @@ -1596,12 +1597,12 @@ function getToken(client, opts) { function getGateway(client, opts, token) { client.internals.token = token; - APIRequest('get', Endpoints.GATEWAY, function (err, res) { + return APIRequest('get', Endpoints.GATEWAY, function (err, res) { if (err || !goodResponse(res)) { client._connecting = false; return client.emit("disconnect", "Error GETing gateway:\n" + stringifyError(res), 0); } - startConnection(client, opts, (res.body.url + "/?encoding=json&v=" + GATEWAY_VERSION)); + return startConnection(client, opts, (res.body.url + "/?encoding=json&v=" + GATEWAY_VERSION)); }); } function startConnection(client, opts, gateway) { @@ -1625,15 +1626,26 @@ function getOfflineUsers(client, servArr, callback) { } } ); - setTimeout( getOfflineUsers, 0, client, servArr, callback ); + return setTimeout( getOfflineUsers, 0, client, servArr, callback ); } - function checkForAllServers(client, ready, message) { var all = Object.keys(client.servers).every(function(s) { return !client.servers[s].unavailable; }); if (all || ready[0]) return client.emit('ready', message); - setTimeout(checkForAllServers, 0, client, ready, message); + return setTimeout(checkForAllServers, 0, client, ready, message); +} +function setupPing(obj) { + applyProperties(obj, [ + ["_ping", []], + ["_lastHB", 0] + ]); + Object.defineProperty(obj, 'ping', { + get: function() { + return ((obj._ping.reduce(function(p, c) { return p + c; }, 0) / obj._ping.length) || 0) | 0; + }, + set: function() {} + }); } /* - Functions - Websocket Handling - */ @@ -1666,7 +1678,8 @@ function handleWSOpen(opts) { function handleWSMessage(opts, data, flags) { var message = decompressWSMessage(data, flags); var _data = message.d; - var client = this, user, server, members, member, key, old, userItem, chItem, voiceSession, + var client = this, user, server, members, member, + key, old, userItem, chItem, voiceSession, userID, serverID, channelID, currentVCID; client.internals.sequence = message.s; @@ -1677,13 +1690,18 @@ function handleWSMessage(opts, data, flags) { client.presenceStatus = 'online'; client.connected = true; - client._mainKeepAlive = setInterval(function(client) { - client.internals.ping = setTimeout(client.disconnect.bind(client), 15e3); + client._mainKeepAlive = setInterval(function() { + client.internals.heartbeat = setTimeout(client.disconnect.bind(client), 15e3); + client.internals._lastHB = Date.now(); send(client._ws, {op: 1, d: client.internals.sequence}); - }.bind(null, client), _data.heartbeat_interval); + }, _data.heartbeat_interval); } - if (message.op === 11) clearTimeout(client.internals.ping); + if (message.op === 11) { + clearTimeout(client.internals.heartbeat); + client.internals._ping.unshift(Date.now() - client.internals._lastHB); + client.internals._ping = client.internals._ping.slice(0, 10); + } //Events client.emit('any', message);