Closed gabsprates closed 7 years ago
Oi @gabsprates ! foi erro meu.. o correto é assim:
'use strict';
var net = require('net'),
chatServer = net.createServer(),
clientList = [];
var broadcast = function (message, client) {
for (var i = clientList.length - 1; i >= 0; i--) {
if (client !== clientList[i]) {
clientList[i].write(message);
}
}
};
chatServer.on('connection', function (client) {
client.write('Hi guest' + '!\n');
clientList.push(client);
client.on('data', function (data) {
broadcast(data, client);
});
client.on('end', function () {
console.log('client', clientList.indexOf(client));
clientList.splice(clientList.indexOf(client), 1);
});
client.on('error', function(err) {
console.log(err);
});
});
chatServer.listen(9000);
Ah sim! Agora eu entendi (: Obrigado.
Onde eu acho uma lista com os eventos .on()
do net.createServer();
?
Opa, eu não tinha reparado nas seções "Event: " :laughing:
Valeu (:
Olá,
Confesso que não entendi o
clientList.indexOf(5)
da linha 8 no arquivo do chat.Isso está certo? Deveria ser 5 mesmo ou era pra ser algo relacionado à variável
data
do escopo dessa função?