diff --git a/src/client.js b/src/client.js index 08d7092..6f3730c 100644 --- a/src/client.js +++ b/src/client.js @@ -156,7 +156,12 @@ Client.prototype.end = function(reason) { this.socket.end(); }; -Client.prototype.write = function(packetId, params) { +function noop(err) { + if(err) throw err; +} + +Client.prototype.write = function(packetId, params, cb) { + cb = cb || noop; if(Array.isArray(packetId)) { if(packetId[0] !== this.state) return false; @@ -175,9 +180,9 @@ Client.prototype.write = function(packetId, params) { debug("writing packetId " + that.state + "." + packetName + " (0x" + packetId.toString(16) + ")"); debug(params); var out = that.encryptionEnabled ? new Buffer(that.cipher.update(buffer), 'binary') : buffer; - that.socket.write(out); + that.socket.write(out,cb); return true; - } + }; var buffer = createPacketBuffer(packetId, this.state, params, this.isServer); if(this.compressionThreshold >= 0 && buffer.length >= this.compressionThreshold) { diff --git a/src/index.js b/src/index.js index 868b1cd..b82a679 100644 --- a/src/index.js +++ b/src/index.js @@ -125,8 +125,11 @@ function createServer(options) { } client.once([states.STATUS, 0x01], function(packet) { - client.write(0x01, {time: packet.time}); - client.end(); + client.write(0x01, {time: packet.time},function(err){ + if(err) + throw err; + client.end(); + }); }); client.write(0x00, {response: JSON.stringify(response)}); } @@ -214,19 +217,22 @@ function createServer(options) { } //client.write('compress', { threshold: 256 }); // Default threshold is 256 //client.compressionThreshold = 256; - client.write(0x02, {uuid: client.uuid, username: client.username}); - client.state = states.PLAY; - loggedIn = true; - startKeepAlive(); + client.write(0x02, {uuid: client.uuid, username: client.username},function(err){ + if(err) + throw err; + client.state = states.PLAY; + loggedIn = true; + startKeepAlive(); - clearTimeout(loginKickTimer); - loginKickTimer = null; + clearTimeout(loginKickTimer); + loginKickTimer = null; - server.playerCount += 1; - client.once('end', function() { - server.playerCount -= 1; + server.playerCount += 1; + client.once('end', function() { + server.playerCount -= 1; + }); + server.emit('login', client); }); - server.emit('login', client); } }); server.listen(port, host); @@ -297,11 +303,13 @@ function createClient(options) { serverHost: host, serverPort: port, nextState: 2 - }); - - client.state = states.LOGIN; - client.write(0x00, { - username: client.username + },function(err){ + if(err) + throw err; + client.state = states.LOGIN; + client.write(0x00, { + username: client.username + }); }); } @@ -362,8 +370,11 @@ function createClient(options) { client.write(0x01, { sharedSecret: encryptedSharedSecretBuffer, verifyToken: encryptedVerifyTokenBuffer, + },function(err){ + if(err) + throw err; + client.encryptionEnabled = true; }); - client.encryptionEnabled = true; } } } diff --git a/src/ping.js b/src/ping.js index a1fe989..93604b5 100644 --- a/src/ping.js +++ b/src/ping.js @@ -37,8 +37,11 @@ function ping(options, cb) { serverHost: host, serverPort: port, nextState: 1 + },function(err){ + if(err) + throw err; + client.state = states.STATUS; }); - client.state = states.STATUS; }); client.connect(port, host);