Merge pull request #243 from roblabla/feature-usePacketNames

Use packet names instead of ID inside NMP's code.
This commit is contained in:
Robin Lambertz 2015-09-15 16:34:22 +02:00
commit 4a1b2bf052
4 changed files with 20 additions and 20 deletions

View file

@ -46,8 +46,8 @@ function createClient(options) {
var client = new Client(false);
client.on('connect', onConnect);
if(keepAlive) client.on('keep_alive', onKeepAlive);
client.once([states.LOGIN, 0x01], onEncryptionKeyRequest);
client.once([states.LOGIN, 0x02], onLogin);
client.once('encryption_begin', onEncryptionKeyRequest);
client.once('success', onLogin);
client.once("compress", onCompressionRequest);
client.on("set_compression", onCompressionRequest);
if(haveCredentials) {
@ -75,14 +75,14 @@ function createClient(options) {
return client;
function onConnect() {
client.write(0x00, {
client.write('set_protocol', {
protocolVersion: version.version,
serverHost: host,
serverPort: port,
nextState: 2
});
client.state = states.LOGIN;
client.write(0x00, {
client.write('login_start', {
username: client.username
});
}
@ -139,7 +139,7 @@ function createClient(options) {
var pubKey = mcPubKeyToURsa(packet.publicKey);
var encryptedSharedSecretBuffer = pubKey.encrypt(sharedSecret, undefined, undefined, ursa.RSA_PKCS1_PADDING);
var encryptedVerifyTokenBuffer = pubKey.encrypt(packet.verifyToken, undefined, undefined, ursa.RSA_PKCS1_PADDING);
client.write(0x01, {
client.write('encryption_begin', {
sharedSecret: encryptedSharedSecretBuffer,
verifyToken: encryptedVerifyTokenBuffer,
});

View file

@ -36,9 +36,9 @@ function createServer(options) {
server.playerCount = 0;
server.onlineModeExceptions = {};
server.on("connection", function(client) {
client.once([states.HANDSHAKING, 0x00], onHandshake);
client.once([states.LOGIN, 0x00], onLogin);
client.once([states.STATUS, 0x00], onPing);
client.once('set_protocol', onHandshake);
client.once('login_start', onLogin);
client.once('ping_start', onPing);
client.on('end', onEnd);
var keepAlive = false;
@ -104,11 +104,11 @@ function createServer(options) {
response = beforePing(response, client) || response;
}
client.once([states.STATUS, 0x01], function(packet) {
client.write(0x01, {time: packet.time});
client.once('ping', function(packet) {
client.write('ping', {time: packet.time});
client.end();
});
client.write(0x00, {response: JSON.stringify(response)});
client.write('server_info', {response: JSON.stringify(response)});
}
function onLogin(packet) {
@ -126,8 +126,8 @@ function createServer(options) {
client.publicKey = new Buffer(publicKeyStr, 'base64');
hash = crypto.createHash("sha1");
hash.update(serverId);
client.once([states.LOGIN, 0x01], onEncryptionKeyResponse);
client.write(0x01, {
client.once('encryption_begin', onEncryptionKeyResponse);
client.write('encryption_begin', {
serverId: serverId,
publicKey: client.publicKey,
verifyToken: client.verifyToken
@ -193,7 +193,7 @@ 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.write('success', {uuid: client.uuid, username: client.username});
client.state = states.PLAY;
loggedIn = true;
if(enableKeepAlive) startKeepAlive();

View file

@ -14,24 +14,24 @@ function ping(options, cb) {
cb(err);
});
client.once([states.STATUS, 0x00], function(packet) {
client.once('server_info', function(packet) {
var data = JSON.parse(packet.response);
var start = Date.now();
client.once(0x01, function(packet) {
client.once('ping', function(packet) {
data.latency = Date.now() - start;
cb(null, data);
client.end();
});
client.write(0x01, {time: [0, 0]});
client.write('ping', {time: [0, 0]});
});
client.on('state', function(newState) {
if(newState === states.STATUS)
client.write(0x00, {});
client.write('ping_start', {});
});
client.on('connect', function() {
client.write(0x00, {
client.write('set_protocol', {
protocolVersion: 4,
serverHost: host,
serverPort: port,

View file

@ -29,7 +29,7 @@ Server.prototype.listen = function(port, host) {
if(client.state === states.PLAY) {
client.write('kick_disconnect', {reason: endReason});
} else if(client.state === states.LOGIN) {
client.write(0x00, {reason: endReason});
client.write('disconnect', {reason: endReason});
}
client._end(endReason);
};