我有以下示例來監聽連接和數據事件,以將結果回顯給偵聽端口8888的其他telnet客戶端。我的telnet會話連接到locahost正常,但沒有輸出被呼應。我正試圖弄清楚什麼是錯誤,正在撞到一堵磚牆。執行甚至沒有達到「連接」事件的程度。Node.js網絡事件不會觸發
/server.js
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;
console.log(id);
client.on('connect', function() {
console.log('A new connection was made');
channel.emit('join', id, client);
});
client.on('data', function (data) {
data = data.toString();
channel.emit('broadcast', id, data);
});
});
server.listen(8888);
然後我在命令行
node server.js
telnet 127.0.0.1 8888
謝謝 - 這清除了整個事情。 – metalaureate