mirror of
https://github.com/unmojang/node-minecraft-protocol.git
synced 2025-09-28 05:35:56 -04:00
all tests passing. closes #3
This commit is contained in:
parent
46d862b1c4
commit
4b0780db66
19
examples/echo.js
Normal file
19
examples/echo.js
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
var mc = require('../');
|
||||||
|
var client = mc.createClient({
|
||||||
|
username: process.env.MC_USERNAME,
|
||||||
|
email: process.env.MC_EMAIL,
|
||||||
|
password: process.env.MC_PASSWORD,
|
||||||
|
verbose: true,
|
||||||
|
});
|
||||||
|
client.on('connect', function() {
|
||||||
|
console.info("connected");
|
||||||
|
});
|
||||||
|
client.on('packet', function(packet) {
|
||||||
|
if (packet.id !== 0x03) return;
|
||||||
|
var match = packet.message.match(/^<(.+?)> (.*)$/);
|
||||||
|
if (! match) return;
|
||||||
|
var username = match[1];
|
||||||
|
var msg = match[2];
|
||||||
|
if (username === client.username) return;
|
||||||
|
client.writePacket(0x03, {message: msg});
|
||||||
|
});
|
3
index.js
3
index.js
@ -30,6 +30,7 @@ function createClient(options) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
var client = new Client();
|
var client = new Client();
|
||||||
|
client.username = options.username;
|
||||||
client.on('connect', function() {
|
client.on('connect', function() {
|
||||||
client.writePacket(0x02, {
|
client.writePacket(0x02, {
|
||||||
protocolVersion: packets.meta.protocolVersion,
|
protocolVersion: packets.meta.protocolVersion,
|
||||||
@ -39,6 +40,7 @@ function createClient(options) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
client.on('packet', function(packet) {
|
client.on('packet', function(packet) {
|
||||||
|
if (options.verbose) console.info("packet", packet);
|
||||||
var handler = packetHandlers[packet.id];
|
var handler = packetHandlers[packet.id];
|
||||||
if (handler) handler(packet);
|
if (handler) handler(packet);
|
||||||
});
|
});
|
||||||
@ -71,6 +73,7 @@ function createClient(options) {
|
|||||||
var sharedSecret;
|
var sharedSecret;
|
||||||
if (haveCredentials) {
|
if (haveCredentials) {
|
||||||
client.session = results[0];
|
client.session = results[0];
|
||||||
|
client.username = client.session.username;
|
||||||
client.emit('session');
|
client.emit('session');
|
||||||
sharedSecret = results[1];
|
sharedSecret = results[1];
|
||||||
joinServerRequest(onJoinServerResponse);
|
joinServerRequest(onJoinServerResponse);
|
||||||
|
@ -744,7 +744,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "data",
|
"name": "data",
|
||||||
"type": "byteArray"
|
"type": "bigByteArray"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"53": [
|
"53": [
|
||||||
|
24
test/test.js
24
test/test.js
@ -85,10 +85,10 @@ describe("minecraft protocol", function() {
|
|||||||
}
|
}
|
||||||
mcServer.on('line', onLine);
|
mcServer.on('line', onLine);
|
||||||
mcServer.on('line', function(line) {
|
mcServer.on('line', function(line) {
|
||||||
//process.stderr.write('.');
|
process.stderr.write('.');
|
||||||
// uncomment this line when debugging for more insight as to what is
|
// uncomment this line when debugging for more insight as to what is
|
||||||
// happening on the minecraft server
|
// happening on the minecraft server
|
||||||
console.error("[MC]", line);
|
//console.error("[MC]", line);
|
||||||
});
|
});
|
||||||
function onLine(line) {
|
function onLine(line) {
|
||||||
if (/\[INFO\] Done/.test(line)) {
|
if (/\[INFO\] Done/.test(line)) {
|
||||||
@ -198,5 +198,23 @@ describe("minecraft protocol", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it("survives for " + SURVIVE_TIME + "ms");
|
it("does not crash for " + SURVIVE_TIME + "ms", function(done) {
|
||||||
|
startServer({ 'online-mode': 'false' }, function() {
|
||||||
|
var client = mc.createClient({
|
||||||
|
username: process.env.MC_USERNAME,
|
||||||
|
});
|
||||||
|
client.on('packet', function(packet) {
|
||||||
|
if (packet.id === 0x01) {
|
||||||
|
client.writePacket(0x03, {
|
||||||
|
message: "hello everyone; I have logged in."
|
||||||
|
});
|
||||||
|
} else if (packet.id === 0x03) {
|
||||||
|
assert.strictEqual(packet.message, "<" + process.env.MC_USERNAME + ">" + " hello everyone; I have logged in.");
|
||||||
|
setTimeout(function() {
|
||||||
|
done();
|
||||||
|
}, SURVIVE_TIME);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user