node-minecraft-protocol/examples/server.js

65 lines
1.5 KiB
JavaScript
Raw Normal View History

2013-01-03 22:01:17 -05:00
var mc = require('../');
var yellow = '§e';
var options = {
motd: 'Vox Industries',
2013-01-04 01:45:57 -05:00
'max-players': 127,
port: 25565,
'online-mode': false,
2013-01-03 22:01:17 -05:00
};
var server = mc.createServer(options);
2013-01-04 01:45:57 -05:00
server.on('login', function(client) {
broadcast(yellow + client.username+' joined the game.');
2013-01-04 01:45:57 -05:00
var addr = client.socket.remoteAddress + ':' + client.socket.remotePort;
console.log(client.username+' connected', '('+addr+')');
2013-01-03 22:01:17 -05:00
2013-01-04 01:45:57 -05:00
client.on('end', function() {
broadcast(yellow + client.username+' left the game.', client);
console.log(client.username+' disconnected', '('+addr+')');
2013-01-04 01:45:57 -05:00
});
2013-01-03 22:01:17 -05:00
2013-01-04 01:45:57 -05:00
// send init data so client will start rendering world
client.write(0x01, {
entityId: client.id,
2013-01-04 01:45:57 -05:00
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
});
2013-01-03 22:01:17 -05:00
2013-01-04 01:45:57 -05:00
client.on(0x03, function(data) {
var message = '<'+client.username+'>' + ' ' + data.message;
2013-01-04 01:45:57 -05:00
broadcast(message);
console.log(message);
});
});
2013-01-03 22:01:17 -05:00
server.on('error', function(error) {
console.log('Error:', error);
});
2013-01-04 01:45:57 -05:00
server.on('listening', function() {
console.log('Server listening on port', server.socketServer.address().port);
2013-01-03 22:01:17 -05:00
});
function broadcast(message, exclude) {
var client;
for (var clientId in server.clients) {
client = server.clients[clientId];
if (client !== exclude) client.write(0x03, { message: message });
}
2013-01-03 22:01:17 -05:00
}