再会。
var events = require('events');
var net = require('net');
var channel = new events.EventEmitter();
channel.clients = {};
channel.subscriptions = {};
channel.on('join', function(id, client) {
this.clients[id] = client;
this.subscriptions[id] = function(senderId, 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() {
channel.emit('join', id, client);
});
client.on('data', function(data) {
data = data.toString();
channel.emit('broadcast', id, data);
});
});
server.listen(8888);
当我运行服务器并通过 telnet“广播”连接时,发出不工作。来自“Node.js in Action”的示例。书籍档案中的代码也不起作用。请帮忙。可能有什么问题?我尝试将 id 的生成器更改为 strong inc "i" 并省略 ...if (id != senderId)... 但不工作!!!