mirror of
https://github.com/unmojang/node-minecraft-protocol.git
synced 2025-09-27 13:14:41 -04:00
make keepalive self-contained
This commit is contained in:
parent
a2598cefda
commit
8dd313b5de
@ -1,8 +1,12 @@
|
||||
module.exports=function(client,server,{kickTimeout = 30 * 1000,checkTimeoutInterval = 4 * 1000}) {
|
||||
module.exports=function(client,server,{
|
||||
kickTimeout = 30 * 1000,
|
||||
checkTimeoutInterval = 4 * 1000,
|
||||
keepAlive : enableKeepAlive = true
|
||||
}) {
|
||||
|
||||
let keepAlive = false;
|
||||
let lastKeepAlive = null;
|
||||
client._keepAliveTimer = null;
|
||||
let keepAliveTimer = null;
|
||||
let sendKeepAliveTime;
|
||||
|
||||
|
||||
@ -27,11 +31,20 @@ module.exports=function(client,server,{kickTimeout = 30 * 1000,checkTimeoutInter
|
||||
lastKeepAlive = new Date();
|
||||
}
|
||||
|
||||
client._startKeepAlive= () => {
|
||||
function startKeepAlive() {
|
||||
keepAlive = true;
|
||||
lastKeepAlive = new Date();
|
||||
client._keepAliveTimer = setInterval(keepAliveLoop, checkTimeoutInterval);
|
||||
keepAliveTimer = setInterval(keepAliveLoop, checkTimeoutInterval);
|
||||
client.on('keep_alive', onKeepAlive);
|
||||
}
|
||||
|
||||
if(enableKeepAlive) client.on('state',state => {
|
||||
if(state === "play") {
|
||||
startKeepAlive();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
client.on('end', () => clearInterval(keepAliveTimer));
|
||||
|
||||
};
|
||||
|
@ -9,7 +9,6 @@ module.exports=function(client,server,options) {
|
||||
const {
|
||||
'online-mode' : onlineMode = true,
|
||||
kickTimeout = 30 * 1000,
|
||||
keepAlive : enableKeepAlive = true,
|
||||
errorHandler : clientErrorHandler=(client,err) => client.end(),
|
||||
} = options;
|
||||
|
||||
@ -18,13 +17,9 @@ module.exports=function(client,server,options) {
|
||||
client.on('error', function(err) {
|
||||
clientErrorHandler(client, err);
|
||||
});
|
||||
client.on('end', onEnd);
|
||||
|
||||
|
||||
function onEnd() {
|
||||
clearInterval(client._keepAliveTimer);
|
||||
client.on('end', () => {
|
||||
clearTimeout(loginKickTimer);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
client.once('login_start', onLogin);
|
||||
@ -127,7 +122,6 @@ module.exports=function(client,server,options) {
|
||||
client.write('success', {uuid: client.uuid, username: client.username});
|
||||
client.state = states.PLAY;
|
||||
loggedIn = true;
|
||||
if(enableKeepAlive) client._startKeepAlive();
|
||||
|
||||
clearTimeout(loginKickTimer);
|
||||
loginKickTimer = null;
|
||||
|
Loading…
x
Reference in New Issue
Block a user