Node.js Socket.IO not working completely

风格不统一 提交于 2019-12-11 04:29:31

问题


I run a standalone server which listens on port 5000. When I connect to it by Netcat or Telnet, the app doesn't print on terminal anything, but on the Netcat/Telnet application screen, it shows that connection has been established.

var io = require('socket.io')();
io.on('connection', function(socket){
console.log('Socket connection established'); 
}); 
io.listen(5000); 
console.log('Listening to port 50000');

回答1:


Socket.io is a library for websockets.

When you connect with telnet, it says that you are connected. This is sufficient to know that your server is listening.

If you want to see it in action, you have to connect to it using a socket.io client. For this, just include /socket.io/socket.io.js in an html file.

This is enough to establish the connection.

 socket = io.connect('http://localhost:5000');

The reason, why console.log is not displayed is that the websocket did not connect. Only the tcp/ip connection was established, this is what telnet is telling you.



来源:https://stackoverflow.com/questions/42538264/node-js-socket-io-not-working-completely

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!