Ho il seguente esempio di ascolto di eventi di connessione e dati, per riportare il risultato ad altri client telnet che ascoltano sulla porta 8888. Le mie sessioni telnet si collegano a locahost bene, ma nessun output è echeggiato. Sto sbattendo la testa contro un muro di mattoni cercando di capire cosa c'è che non va. L'esecuzione non arriva nemmeno fino all'evento 'connect'.Gli eventi net Node.js non si attivano
/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);
Ho quindi eseguito nella riga di comando
node server.js
telnet 127.0.0.1 8888
Grazie - ha chiarito tutto. – metalaureate