diff --git a/HISTORY.md b/HISTORY.md index ea2f1e4..027f421 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,5 +1,9 @@ # History +## 0.16.1 (unreleased) + + * add checkTimeoutInterval to createClient + ## 0.16.0 * cross version support exposed : version option in createClient and createServer diff --git a/doc/README.md b/doc/README.md index 081edb4..d2ddb77 100644 --- a/doc/README.md +++ b/doc/README.md @@ -72,6 +72,7 @@ Returns a `Client` instance and perform login. * clientToken : generated if a password is given * accessToken : generated if a password is given * keepAlive : send keep alive packets : default to true + * checkTimeoutInterval : default to `10*1000` (10s), check if keepalive received at that period, disconnect otherwise. * version : 1.8 or 1.9 : default to 1.8 ## mc.Client(isServer,version) diff --git a/src/createClient.js b/src/createClient.js index aa5fc0e..9400938 100644 --- a/src/createClient.js +++ b/src/createClient.js @@ -37,6 +37,7 @@ function createClient(options) { assert.ok(options.username, "username is required"); var haveCredentials = options.password != null || (clientToken != null && options.session != null); var keepAlive = options.keepAlive == null ? true : options.keepAlive; + var checkTimeoutInterval = options.checkTimeoutInterval || 10 * 1000; var optVersion = options.version || require("./version").defaultVersion; var mcData=require("minecraft-data")(optVersion); @@ -103,8 +104,11 @@ function createClient(options) { function onCompressionRequest(packet) { client.compressionThreshold = packet.threshold; } - + var timeout = null; function onKeepAlive(packet) { + if (timeout) + clearTimeout(timeout); + timeout = setTimeout(() => client.end(), checkTimeoutInterval); client.write('keep_alive', { keepAliveId: packet.keepAliveId });