= require('net');
const channel = new events.EventEmitter();
channel.clients = {};
channel.subscriptions = {};
channel.on('join', function(id, client){
this.clients[id] = client;
this.subscriptions[id] = (SenderId, message) => {
if (id != SenderId) {
this.clients[id].write(message);
}
};
this.on('broadcast', this.subscriptions[id]);
});
const server = net.createServer((client, error) => {
if (error) {
console.log(error.toString());
}
const id = ${client.remoteAddress}:${client.remotePort};
channel.emit('join', id, client);
client.on('data', data =>{
data = data.toString();
channel.emit('broadcast', id, data);
});
});
server.listen(8080);
Выхлоп:
TypeError: "encoding" must be a valid string encoding
at fromString (buffer.js:308:13)
at Function.Buffer.from (buffer.js:177:12)
at createWriteReq (net.js:825:45)
at Socket._writeGeneric (net.js:776:11)
at Socket._write (net.js:799:8)
at doWrite (_stream_writable.js:397:12)
at writeOrBuffer (_stream_writable.js:383:5)
at Socket.Writable.write (_stream_writable.js:290:11)
at Socket.write (net.js:717:40)
at EventEmitter.subscriptions.(anonymous function) (/Users/oxxxra/hello_express/cli.js:12:18)
Основная в том, что запостил простыню кода сюда вместо песочницы.
Обсуждают сегодня