Intento hacer un servidor tcp de nodejs. En la devolución de llamada server.on('connection',...) ¿el objeto de socket no es una variable local? ¿Cómo puedo seguir con vida porque la comunicación sigue funcionando si no puedo empaquetar el zócalo en una matriz? Mi objetivo es empaquetar los sockets en una matriz y manejarlos desde allí. Implementé un tiempo de espera para eliminar los sockets no utilizados, pero si elimino un socket de la matriz, también experimento un tiempo de espera si no lo eliminé. Entonces, creo que tengo un duplicado de cada socket en segundo plano. ¿Cómo puedo hacer que los sockets existan solo en la matriz?
// Include Nodejs' net module. const Net = require('net'); // The port on which the server is listening. const port = 8080; clients = []; const server = new Net.Server(); server.listen(port, function() { console.log(`Server listening for connection requests on socket localhost:${port}`.); }); server.on('connection', function(socket) { console.log('A new connection has been established.'); socket.write('Hello, client.'); socket.on('data', function(chunk) { console.log(`Data received from client: ${chunk.toString()`.}); }); socket.on('end', function() { console.log('Closing connection with the client'); }); socket.on('error', function(err) { console.log(`Error: ${err}`); }); socket.timeoutHandle = setTimeout(() => { console.log(`client timed out: ${socket.name}`); socket.end(); socket.destroy(); clients.forEach((client, index) => { if (client.remoteAddress == socket.remoteAddress) { clients.splice(index, 1); } }); }, 15000); clients.push(socket); });