mirror of
https://github.com/unmojang/node-minecraft-protocol.git
synced 2025-09-28 13:45:37 -04:00
test passing: clients can log in and chat
This commit is contained in:
parent
5e0ae2713a
commit
2e75851fd6
@ -57,7 +57,7 @@ var server = mc.createServer({
|
||||
});
|
||||
server.on('login', function(client) {
|
||||
client.write(0x01, {
|
||||
entityId: 0,
|
||||
entityId: client.id,
|
||||
levelType: 'default',
|
||||
gameMode: 0,
|
||||
dimension: 0,
|
||||
|
@ -12,12 +12,12 @@ var options = {
|
||||
var server = mc.createServer(options);
|
||||
|
||||
server.on('login', function(client) {
|
||||
broadcast(yellow + player.username+' joined the game.');
|
||||
broadcast(yellow + client.username+' joined the game.');
|
||||
var addr = client.socket.remoteAddress + ':' + client.socket.remotePort;
|
||||
console.log(player.username+' connected', '('+addr+')');
|
||||
console.log(client.username+' connected', '('+addr+')');
|
||||
|
||||
client.on('end', function() {
|
||||
broadcast(yellow + player.username+' left the game.', client);
|
||||
broadcast(yellow + client.username+' left the game.', client);
|
||||
console.log(client.username+' disconnected', '('+addr+')');
|
||||
});
|
||||
|
||||
|
6
index.js
6
index.js
@ -115,6 +115,8 @@ function createClient(options) {
|
||||
assert.ok(options.username, "username is required");
|
||||
var haveCredentials = options.email && options.password;
|
||||
var keepAlive = options.keepAlive == null ? true : options.keepAlive;
|
||||
var email = options.email;
|
||||
var password = options.password;
|
||||
|
||||
var client = new Client({
|
||||
isServer: false
|
||||
@ -123,7 +125,7 @@ function createClient(options) {
|
||||
client.on('connect', function() {
|
||||
client.write(0x02, {
|
||||
protocolVersion: protocol.version,
|
||||
username: options.username,
|
||||
username: client.username,
|
||||
serverHost: host,
|
||||
serverPort: port,
|
||||
});
|
||||
@ -147,7 +149,7 @@ function createClient(options) {
|
||||
if (haveCredentials) {
|
||||
hash = crypto.createHash('sha1');
|
||||
hash.update(packet.serverId);
|
||||
batch.push(function(cb) { getLoginSession(options.email, options.password, cb); });
|
||||
batch.push(function(cb) { getLoginSession(email, password, cb); });
|
||||
}
|
||||
batch.push(function(cb) { crypto.randomBytes(16, cb); });
|
||||
batch.end(function (err, results) {
|
||||
|
@ -53,9 +53,7 @@ Client.prototype.setSocket = function(socket) {
|
||||
|
||||
Client.prototype.connect = function(port, host) {
|
||||
var self = this;
|
||||
self.setSocket(net.connect(port, host, function() {
|
||||
self.emit('connect');
|
||||
}));
|
||||
self.setSocket(net.connect(port, host));
|
||||
};
|
||||
|
||||
Client.prototype.end = function(reason) {
|
||||
|
67
test/test.js
67
test/test.js
@ -322,10 +322,73 @@ describe("mc-server", function() {
|
||||
playerCount: 0,
|
||||
maxPlayers: 120
|
||||
});
|
||||
done();
|
||||
server.close();
|
||||
});
|
||||
});
|
||||
server.on('close', done);
|
||||
});
|
||||
it("clients can log in and chat", function(done) {
|
||||
var server = mc.createServer({ 'online-mode': false, });
|
||||
var username = ['player1', 'player2'];
|
||||
var index = 0;
|
||||
server.on('login', function(client) {
|
||||
assert.notEqual(client.id, null);
|
||||
assert.strictEqual(client.username, username[index++]);
|
||||
broadcast(client.username + ' joined the game.');
|
||||
client.on('end', function() {
|
||||
broadcast(client.username + ' left the game.', client);
|
||||
if (client.username === 'player2') server.close();
|
||||
});
|
||||
client.write(0x01, {
|
||||
entityId: client.id,
|
||||
levelType: 'default',
|
||||
gameMode: 1,
|
||||
dimension: 0,
|
||||
difficulty: 2,
|
||||
maxPlayers: server.maxPlayers
|
||||
});
|
||||
client.on(0x03, function(packet) {
|
||||
var message = '<' + client.username + '>' + ' ' + packet.message;
|
||||
broadcast(message);
|
||||
});
|
||||
});
|
||||
server.on('close', done);
|
||||
server.on('listening', function() {
|
||||
var player1 = mc.createClient({ username: 'player1' });
|
||||
player1.on(0x01, function(packet) {
|
||||
assert.strictEqual(packet.gameMode, 1);
|
||||
assert.strictEqual(packet.levelType, 'default');
|
||||
assert.strictEqual(packet.dimension, 0);
|
||||
assert.strictEqual(packet.difficulty, 2);
|
||||
var player2 = mc.createClient({ username: 'player2' });
|
||||
player2.on(0x01, function(packet) {
|
||||
player1.once(0x03, function(packet) {
|
||||
assert.strictEqual(packet.message, 'player2 joined the game.');
|
||||
player1.once(0x03, function(packet) {
|
||||
assert.strictEqual(packet.message, '<player2> hi');
|
||||
player2.once(0x03, function(packet) {
|
||||
assert.strictEqual(packet.message, '<player1> hello');
|
||||
player1.once(0x03, function(packet) {
|
||||
assert.strictEqual(packet.message, 'player2 left the game.');
|
||||
player1.end();
|
||||
});
|
||||
player2.end();
|
||||
});
|
||||
player1.write(0x03, { message: "hello" } );
|
||||
});
|
||||
player2.write(0x03, { message: "hi" } );
|
||||
});
|
||||
});
|
||||
});
|
||||
it("clients can log in and chat");
|
||||
});
|
||||
|
||||
function broadcast(message, exclude) {
|
||||
var client;
|
||||
for (var clientId in server.clients) {
|
||||
client = server.clients[clientId];
|
||||
if (client !== exclude) client.write(0x03, { message: message });
|
||||
}
|
||||
}
|
||||
});
|
||||
it("gives correct reason for kicking clients when shutting down");
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user