0
我試圖讓使用Node.js的,當我打開多個終端,並輸入文字Node.js的遠程登錄聊天服務器
沒有出現一個telnet聊天服務器。 即文本不會傳輸到其他終端
我打開serveral的終端,並進入
telnet 127.0.0.1 8888
然後開始打字,但
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("join");
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) {
console.log("createServer");
var id = client.remoteAddress + ':' + client.remotePort;
client.on('connect', function() {
console.log("connect");
channel.emit('join', id, client);
});
client.on('data', function(data) {
console.log("data");
data = data.toString();
channel.emit('broadcast', id, data);
});
});
server.listen(8888);