node-minecraft-protocol/examples/server.js
Andrew Kelley c9ed8a2792 server API update
* add passing test for "starts listening"
 * server.socket -> server.socketServer
 * add client.id which can be used as entity id
 * add server.clients which is {clientId: client}
 * update server examples
 * server emits 'close' event, not 'end' event
 * add server.close()
2013-01-04 20:22:19 -05:00

64 lines
1.5 KiB
JavaScript

var mc = require('../');
var yellow = '§e';
var options = {
'online-mode': false,
motd: 'Vox Industries',
'max-players': 127,
port: 25565,
};
var server = mc.createServer(options);
server.on('login', function(client) {
broadcast(yellow + player.username+' joined the game.');
var addr = client.socket.remoteAddress + ':' + client.socket.remotePort;
console.log(player.username+' connected', '('+addr+')');
client.on('end', function() {
broadcast(yellow + player.username+' left the game.', client);
console.log(client.username+' disconnected', '('+addr+')');
});
// send init data so client will start rendering world
client.write(0x01, {
entityId: client.id,
levelType: 'default',
gameMode: 1,
dimension: 0,
difficulty: 2,
maxPlayers: server.maxPlayers
});
client.write(0x0d, {
x: 0,
y: 256,
stance: 255,
z: 0,
yaw: 0,
pitch: 0,
onGround: true
});
client.on(0x03, function(data) {
var message = '<'+client.username+'>' + ' ' + data.message;
broadcast(message);
console.log(message);
});
});
server.on('error', function(error) {
console.log('Error:', error);
});
server.on('listening', function() {
console.log('Server listening on port', server.socketServer.address().port);
});
function broadcast(message, exclude) {
var client;
for (var clientId in server.clients) {
client = server.clients[clientId];
if (client !== exclude) client.write(0x03, { message: message });
}
}