节点 + 远程登录 + OS X

Node + telnet + OS X

本文关键字:OS 登录 程登录 节点      更新时间:2023-09-26

美好的一天。

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 "广播"连接时,发出不起作用。"节点.js在行动"中的示例。书籍存档中的代码也不起作用。请帮忙。可能有什么问题?我尝试将 id 的生成器更改为强大的 inc"i"并省略......if (id != senderId)...但不起作用!!

当调用 net.createServer 的回调函数时,它已经意味着客户端已连接。另外,我认为connect事件甚至不是由net.createServer生成的。

因此,与其等待connect事件再发出"join",不如立即发出它:

var server = net.createServer(function(client) {
  var id = client.remoteAddress + ':' + client.remotePort;
  // we got a new client connection:
  channel.emit('join', id, client);
  // wait for incoming data and broadcast it:
  client.on('data', function(data) {
    data = data.toString();
    channel.emit('broadcast', id, data);
  });
});