var events = require('events');
var net = require('net');
var channel = new events.EventEmitter();
channel.clients = {};
channel.subscriptions = {};
channel.on('join',function(id,client){
console.log('connect');
this.clients[id] = client;
this.subscriptions[id] = function(senderId,message){
console.log(message);
if(id != senderId){
this.clients[id].write(message);
}
};
this.on('broadcast',this.subscriptions[id]);
});
var server = net.createServer(function(client){
var id = client.remoteAddress+':'+client.remotePort;
client.on('connect',function(){
console.log('client connect');
channel.emit('join',id,client);
});
client.on('data',function(data){
data = data.toString();
console.log(data);
channel.emit('broadcast',id,data);
});
});
server.listen(8887);
这句始终不被执行,为什么? client.on('connect',function(){ console.log('client connect'); channel.emit('join',id,client); });
相关分类