writePacket -> write

This commit is contained in:
Andrew Kelley 2013-01-03 21:42:35 -05:00
parent f70110a646
commit 62e66be053
4 changed files with 14 additions and 15 deletions

View File

@ -25,9 +25,7 @@ Supports Minecraft version 1.4.7pre
## Usage ## Usage
### Echo example ### Echo client example
Listen for chat messages and echo them back.
```js ```js
var mc = require('minecraft-protocol'); var mc = require('minecraft-protocol');
@ -39,8 +37,9 @@ var client = mc.createClient({
password: "12345678", // online-mode=true servers password: "12345678", // online-mode=true servers
}); });
client.on(0x03, function(packet) { client.on(0x03, function(packet) {
// Listen for chat messages and echo them back.
if (packet.message.indexOf(client.session.username) !== -1) return; if (packet.message.indexOf(client.session.username) !== -1) return;
client.writePacket(0x03, { client.write(0x03, {
message: packet.message, message: packet.message,
}); });
}); });

View File

@ -14,5 +14,5 @@ client.on(0x03, function(packet) {
var username = match[1]; var username = match[1];
var msg = match[2]; var msg = match[2];
if (username === client.username) return; if (username === client.username) return;
client.writePacket(0x03, {message: msg}); client.write(0x03, {message: msg});
}); });

View File

@ -26,7 +26,7 @@ function createClient(options) {
var client = new Client(); var client = new Client();
client.username = options.username; client.username = options.username;
client.on('connect', function() { client.on('connect', function() {
client.writePacket(0x02, { client.write(0x02, {
protocolVersion: packets.meta.protocolVersion, protocolVersion: packets.meta.protocolVersion,
username: options.username, username: options.username,
serverHost: host, serverHost: host,
@ -34,14 +34,14 @@ function createClient(options) {
}); });
}); });
client.on(0x00, onKeepAlive); client.on(0x00, onKeepAlive);
client.on(0xFC, onEncryptionKeyResponse); client.once(0xFC, onEncryptionKeyResponse);
client.on(0xFD, onEncryptionKeyRequest); client.once(0xFD, onEncryptionKeyRequest);
client.connect(port, host); client.connect(port, host);
return client; return client;
function onKeepAlive(packet) { function onKeepAlive(packet) {
client.writePacket(0x00, { client.write(0x00, {
keepAliveId: packet.keepAliveId keepAliveId: packet.keepAliveId
}); });
} }
@ -120,7 +120,7 @@ function createClient(options) {
var encryptedVerifyTokenBuffer = new Buffer(encryptedVerifyToken, 'base64'); var encryptedVerifyTokenBuffer = new Buffer(encryptedVerifyToken, 'base64');
client.cipher = crypto.createCipheriv('aes-128-cfb8', sharedSecret, sharedSecret); client.cipher = crypto.createCipheriv('aes-128-cfb8', sharedSecret, sharedSecret);
client.decipher = crypto.createDecipheriv('aes-128-cfb8', sharedSecret, sharedSecret); client.decipher = crypto.createDecipheriv('aes-128-cfb8', sharedSecret, sharedSecret);
client.writePacket(0xfc, { client.write(0xfc, {
sharedSecret: encryptedSharedSecretBuffer, sharedSecret: encryptedSharedSecretBuffer,
verifyToken: encryptedVerifyTokenBuffer, verifyToken: encryptedVerifyTokenBuffer,
}); });
@ -132,7 +132,7 @@ function createClient(options) {
assert.strictEqual(packet.sharedSecret.length, 0); assert.strictEqual(packet.sharedSecret.length, 0);
assert.strictEqual(packet.verifyToken.length, 0); assert.strictEqual(packet.verifyToken.length, 0);
client.encryptionEnabled = true; client.encryptionEnabled = true;
client.writePacket(0xcd, { payload: 0 }); client.write(0xcd, { payload: 0 });
} }
} }
@ -179,7 +179,7 @@ Client.prototype.end = function() {
this.socket.end(); this.socket.end();
}; };
Client.prototype.writePacket = function(packetId, params) { Client.prototype.write = function(packetId, params) {
var buffer = createPacketBuffer(packetId, params); var buffer = createPacketBuffer(packetId, params);
var out = this.encryptionEnabled ? new Buffer(this.cipher.update(buffer), 'binary') : buffer; var out = this.encryptionEnabled ? new Buffer(this.cipher.update(buffer), 'binary') : buffer;
this.socket.write(out); this.socket.write(out);

View File

@ -128,7 +128,7 @@ describe("minecraft protocol", function() {
assert.strictEqual(packet.difficulty, 1); assert.strictEqual(packet.difficulty, 1);
assert.strictEqual(packet.dimension, 0); assert.strictEqual(packet.dimension, 0);
assert.strictEqual(packet.gameMode, 0); assert.strictEqual(packet.gameMode, 0);
client.writePacket(0x03, { client.write(0x03, {
message: "hello everyone; I have logged in." message: "hello everyone; I have logged in."
}); });
}); });
@ -162,7 +162,7 @@ describe("minecraft protocol", function() {
assert.strictEqual(packet.difficulty, 1); assert.strictEqual(packet.difficulty, 1);
assert.strictEqual(packet.dimension, 0); assert.strictEqual(packet.dimension, 0);
assert.strictEqual(packet.gameMode, 0); assert.strictEqual(packet.gameMode, 0);
client.writePacket(0x03, { client.write(0x03, {
message: "hello everyone; I have logged in." message: "hello everyone; I have logged in."
}); });
}); });
@ -200,7 +200,7 @@ describe("minecraft protocol", function() {
username: process.env.MC_USERNAME, username: process.env.MC_USERNAME,
}); });
client.on(0x01, function(packet) { client.on(0x01, function(packet) {
client.writePacket(0x03, { client.write(0x03, {
message: "hello everyone; I have logged in." message: "hello everyone; I have logged in."
}); });
}); });