I am trying to build a socket server using Node.js. The server need to handle multiple TCP connections. Here is my code:
const HOST = '127.0.0.1';
const PORT = 5000;
var app = require('net')();
var sleep = require('sleep');
var server = net.createServer().listen(PORT, HOST);
server.on('connection', function(sock){
console.log('CONNECTED: ' + sock.remoteAddress + ':' + sock.remotePort);
var c = 0;
while(true){
sock.write(c.toString()+' ');
c++;
sleep.sleep(1);
}
});
console.log('Server listening on ' + HOST + ':' + PORT);
How to handle all connections parallelly instead of queueing? Thanks :D
How to handle all connections parallelly instead of queueing?you are 'queuing' because you blocked the event loop with thiswhile(true). this said, node is single thread, everything is sequential, but fast enough to give the feel it is //, for true //ism, use another language likego.