良い一日。
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 のジェネレーターを強い inc "i" に変更して ...if (id != senderId) を省略しようとしましたが、機能しませんでした!!!