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()
This commit is contained in:
Andrew Kelley 2013-01-04 20:22:19 -05:00
parent 8096a08afd
commit c9ed8a2792
5 changed files with 46 additions and 33 deletions

View file

@ -1,7 +1,6 @@
var mc = require('../');
var yellow = '§e';
var players = [];
var options = {
'online-mode': false,
@ -13,29 +12,18 @@ var options = {
var server = mc.createServer(options);
server.on('login', function(client) {
var player = {
client: client,
username: client.username,
index: players.length
};
players.push(player);
server.players = players.length;
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() {
players.splice(player.index, 1);
server.players = players.length;
broadcast(yellow + player.username+' left the game.', player);
console.log(player.username+' disconnected', '('+addr+')');
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: 0,
entityId: client.id,
levelType: 'default',
gameMode: 1,
dimension: 0,
@ -53,7 +41,7 @@ server.on('login', function(client) {
});
client.on(0x03, function(data) {
var message = '<'+player.username+'>' + ' ' + data.message;
var message = '<'+client.username+'>' + ' ' + data.message;
broadcast(message);
console.log(message);
});
@ -64,13 +52,13 @@ server.on('error', function(error) {
});
server.on('listening', function() {
console.log('Server listening on port', server.socket.address().port);
console.log('Server listening on port', server.socketServer.address().port);
});
function broadcast(message, exclude) {
for(var i = 0; i < players.length; i++) {
if(players[i].username !== exclude && i !== exclude && players[i] !== exclude) {
players[i].client.write(0x03, { message: message });
}
}
var client;
for (var clientId in server.clients) {
client = server.clients[clientId];
if (client !== exclude) client.write(0x03, { message: message });
}
}

View file

@ -16,7 +16,7 @@ server.on('login', function(client) {
// send init data so client will start rendering world
client.write(0x01, {
entityId: 0,
entityId: client.id,
levelType: 'default',
gameMode: 0,
dimension: 0,
@ -41,5 +41,5 @@ server.on('error', function(error) {
});
server.on('listening', function() {
console.log('Server listening on port', server.socket.address().port);
console.log('Server listening on port', server.socketServer.address().port);
});

View file

@ -90,7 +90,6 @@ function createServer(options) {
});
server.listen(port, host);
return server;
}
function createClient(options) {

View file

@ -12,37 +12,51 @@ function Server(options) {
this.maxPlayers = options['max-players'] || 20;
this.playerCount = 0
this.socket = null;
this.socketServer = null;
this.cipher = null;
this.decipher = null;
this.clients = {};
}
util.inherits(Server, EventEmitter);
Server.prototype.listen = function(port, host) {
var self = this;
self.socket = net.createServer();
self.socket.on('connection', function(socket) {
var nextId = 0;
self.socketServer = net.createServer();
self.socketServer.on('connection', function(socket) {
var client = new Client({
isServer: true,
});
client.id = nextId++;
self.clients[client.id] = client;
client.on('error', function(err) {
self.emit('error', err);
});
client.setSocket(socket);
self.emit('connection', client);
client.on('end', function() {
delete self.clients[client.id];
this.playerCount -= 1;
});
this.playerCount += 1;
});
self.socket.on('error', function(err) {
self.socketServer.on('error', function(err) {
self.emit('error', err);
});
self.socket.on('close', function() {
self.emit('end');
self.socketServer.on('close', function() {
self.emit('close');
});
self.socket.on('listening', function() {
self.socketServer.on('listening', function() {
self.emit('listening');
});
self.socket.listen(port, host);
self.socketServer.listen(port, host);
};
Server.prototype.close = function() {
var client;
for(var clientId in this.clients) {
client = this.clients[clientId];
client.end();
}
this.socketServer.close();
};

View file

@ -214,6 +214,18 @@ describe("client", function() {
});
});
describe("server", function() {
it("starts listening", function(done) {
var server = mc.createServer({ 'online-mode': false });
var listening = false;
server.on('listening', function() {
listening = true;
server.close();
});
server.on('close', function() {
assert.ok(listening);
done();
});
});
it("kicks clients that do not emit keep alive");
it("responds to ping requests");
});