From a727829a98455800b8e1c0223cf0c70556916173 Mon Sep 17 00:00:00 2001 From: deathcap Date: Mon, 25 Jan 2016 23:19:46 -0800 Subject: [PATCH] Move onKeepAlive to client/keepalive --- src/client/keepalive.js | 15 +++++++++++++++ src/createClient.js | 26 ++------------------------ 2 files changed, 17 insertions(+), 24 deletions(-) create mode 100644 src/client/keepalive.js diff --git a/src/client/keepalive.js b/src/client/keepalive.js new file mode 100644 index 0000000..59b9b88 --- /dev/null +++ b/src/client/keepalive.js @@ -0,0 +1,15 @@ +module.exports = function(client) { + client.on('keep_alive', onKeepAlive); + + var timeout = null; + + function onKeepAlive(packet) { + if (timeout) + clearTimeout(timeout); + timeout = setTimeout(() => client.end(), checkTimeoutInterval); + client.write('keep_alive', { + keepAliveId: packet.keepAliveId + }); + } + +}; diff --git a/src/createClient.js b/src/createClient.js index 1bba4e3..11f343c 100644 --- a/src/createClient.js +++ b/src/createClient.js @@ -8,6 +8,7 @@ var states = require("./states"); var debug = require("./debug"); var UUID = require('uuid-1345'); var encrypt = require('./client/encrypt'); +var keepalive = require('./client/keepalive'); module.exports=createClient; @@ -45,7 +46,7 @@ function createClient(options) { var client = new Client(false,version.majorVersion); client.on('connect', onConnect); - if(keepAlive) client.on('keep_alive', onKeepAlive); + if(keepAlive) keepalive(client); encrypt(client); client.once('success', onLogin); client.once("compress", onCompressionRequest); @@ -85,7 +86,6 @@ function createClient(options) { client.connect(port, host); } - var timeout = null; return client; function onConnect() { @@ -104,14 +104,6 @@ function createClient(options) { function onCompressionRequest(packet) { client.compressionThreshold = packet.threshold; } - function onKeepAlive(packet) { - if (timeout) - clearTimeout(timeout); - timeout = setTimeout(() => client.end(), checkTimeoutInterval); - client.write('keep_alive', { - keepAliveId: packet.keepAliveId - }); - } function onLogin(packet) { client.state = states.PLAY; @@ -119,17 +111,3 @@ function createClient(options) { client.username = packet.username; } } - - - -function mcPubKeyToURsa(mcPubKeyBuffer) { - var pem = "-----BEGIN PUBLIC KEY-----\n"; - var base64PubKey = mcPubKeyBuffer.toString('base64'); - var maxLineLength = 65; - while(base64PubKey.length > 0) { - pem += base64PubKey.substring(0, maxLineLength) + "\n"; - base64PubKey = base64PubKey.substring(maxLineLength); - } - pem += "-----END PUBLIC KEY-----\n"; - return ursa.createPublicKey(pem, 'utf8'); -}