0

The general rule in writing Node.js code is that all code should be non-blocking and communicate via events. I would like to know if this code written using the Socket.IO library for Node.js create a blocked connection, or does it follow the general Node.js rules?

sio.sockets.on('connection', function (socket) {
  socket.on('message', function (msg) {
    console.log("Received message"+message);
  });

  socket.on('cookie', function (msg) {
    console.log("Cookie Received");
    console.log(msg);
  });

  this.send('hello');


  socket.on('disconnect', function (){
    console.log('Disconnected');
  });
});

Would be grateful for any help.

4

1 回答 1

2

不,node.js socket.io 服务器使用标准节点非阻塞 api 监听 tcp

也就是说,您的控件在 之后立即转到下一条语句sio.sockets.on(..);,此调用中唯一发生的事情是“构造 javascript 函数对象 abd 分配给侦听器数组”。

于 2011-07-12T02:41:49.127 回答