问题
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