Skip to content
This repository has been archived by the owner on Mar 25, 2020. It is now read-only.

Commit

Permalink
Added client.internals.ping
Browse files Browse the repository at this point in the history
  • Loading branch information
izy521 committed Jan 6, 2017
1 parent 523f906 commit b76fda4
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -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 - */
Expand Down Expand Up @@ -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;

Expand All @@ -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);
Expand Down

0 comments on commit b76fda4

Please sign in to comment.