mirror of
https://github.com/unmojang/node-minecraft-protocol.git
synced 2025-09-29 06:03:33 -04:00
add callback to write (socket.write *is* async) fix #168
This commit is contained in:
parent
e2519f0b9f
commit
6446b205f0
@ -156,7 +156,12 @@ Client.prototype.end = function(reason) {
|
|||||||
this.socket.end();
|
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(Array.isArray(packetId)) {
|
||||||
if(packetId[0] !== this.state)
|
if(packetId[0] !== this.state)
|
||||||
return false;
|
return false;
|
||||||
@ -175,9 +180,9 @@ Client.prototype.write = function(packetId, params) {
|
|||||||
debug("writing packetId " + that.state + "." + packetName + " (0x" + packetId.toString(16) + ")");
|
debug("writing packetId " + that.state + "." + packetName + " (0x" + packetId.toString(16) + ")");
|
||||||
debug(params);
|
debug(params);
|
||||||
var out = that.encryptionEnabled ? new Buffer(that.cipher.update(buffer), 'binary') : buffer;
|
var out = that.encryptionEnabled ? new Buffer(that.cipher.update(buffer), 'binary') : buffer;
|
||||||
that.socket.write(out);
|
that.socket.write(out,cb);
|
||||||
return true;
|
return true;
|
||||||
}
|
};
|
||||||
|
|
||||||
var buffer = createPacketBuffer(packetId, this.state, params, this.isServer);
|
var buffer = createPacketBuffer(packetId, this.state, params, this.isServer);
|
||||||
if(this.compressionThreshold >= 0 && buffer.length >= this.compressionThreshold) {
|
if(this.compressionThreshold >= 0 && buffer.length >= this.compressionThreshold) {
|
||||||
|
47
src/index.js
47
src/index.js
@ -125,8 +125,11 @@ function createServer(options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
client.once([states.STATUS, 0x01], function(packet) {
|
client.once([states.STATUS, 0x01], function(packet) {
|
||||||
client.write(0x01, {time: packet.time});
|
client.write(0x01, {time: packet.time},function(err){
|
||||||
client.end();
|
if(err)
|
||||||
|
throw err;
|
||||||
|
client.end();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
client.write(0x00, {response: JSON.stringify(response)});
|
client.write(0x00, {response: JSON.stringify(response)});
|
||||||
}
|
}
|
||||||
@ -214,19 +217,22 @@ function createServer(options) {
|
|||||||
}
|
}
|
||||||
//client.write('compress', { threshold: 256 }); // Default threshold is 256
|
//client.write('compress', { threshold: 256 }); // Default threshold is 256
|
||||||
//client.compressionThreshold = 256;
|
//client.compressionThreshold = 256;
|
||||||
client.write(0x02, {uuid: client.uuid, username: client.username});
|
client.write(0x02, {uuid: client.uuid, username: client.username},function(err){
|
||||||
client.state = states.PLAY;
|
if(err)
|
||||||
loggedIn = true;
|
throw err;
|
||||||
startKeepAlive();
|
client.state = states.PLAY;
|
||||||
|
loggedIn = true;
|
||||||
|
startKeepAlive();
|
||||||
|
|
||||||
clearTimeout(loginKickTimer);
|
clearTimeout(loginKickTimer);
|
||||||
loginKickTimer = null;
|
loginKickTimer = null;
|
||||||
|
|
||||||
server.playerCount += 1;
|
server.playerCount += 1;
|
||||||
client.once('end', function() {
|
client.once('end', function() {
|
||||||
server.playerCount -= 1;
|
server.playerCount -= 1;
|
||||||
|
});
|
||||||
|
server.emit('login', client);
|
||||||
});
|
});
|
||||||
server.emit('login', client);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
server.listen(port, host);
|
server.listen(port, host);
|
||||||
@ -297,11 +303,13 @@ function createClient(options) {
|
|||||||
serverHost: host,
|
serverHost: host,
|
||||||
serverPort: port,
|
serverPort: port,
|
||||||
nextState: 2
|
nextState: 2
|
||||||
});
|
},function(err){
|
||||||
|
if(err)
|
||||||
client.state = states.LOGIN;
|
throw err;
|
||||||
client.write(0x00, {
|
client.state = states.LOGIN;
|
||||||
username: client.username
|
client.write(0x00, {
|
||||||
|
username: client.username
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -362,8 +370,11 @@ function createClient(options) {
|
|||||||
client.write(0x01, {
|
client.write(0x01, {
|
||||||
sharedSecret: encryptedSharedSecretBuffer,
|
sharedSecret: encryptedSharedSecretBuffer,
|
||||||
verifyToken: encryptedVerifyTokenBuffer,
|
verifyToken: encryptedVerifyTokenBuffer,
|
||||||
|
},function(err){
|
||||||
|
if(err)
|
||||||
|
throw err;
|
||||||
|
client.encryptionEnabled = true;
|
||||||
});
|
});
|
||||||
client.encryptionEnabled = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,8 +37,11 @@ function ping(options, cb) {
|
|||||||
serverHost: host,
|
serverHost: host,
|
||||||
serverPort: port,
|
serverPort: port,
|
||||||
nextState: 1
|
nextState: 1
|
||||||
|
},function(err){
|
||||||
|
if(err)
|
||||||
|
throw err;
|
||||||
|
client.state = states.STATUS;
|
||||||
});
|
});
|
||||||
client.state = states.STATUS;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
client.connect(port, host);
|
client.connect(port, host);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user