问题描述
我运行一个独立的服务器,它侦听端口 5000.当我通过 Netcat 或 Telnet 连接到它时,应用程序不会在终端上打印任何内容,但在 Netcat/Telnet 应用程序屏幕上,它显示已建立连接.
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');
推荐答案
Socket.io 是一个 websockets 库.
Socket.io is a library for websockets.
当您使用 telnet 连接时,它表示您已连接.这足以知道您的服务器正在侦听.
When you connect with telnet, it says that you are connected. This is sufficient to know that your server is listening.
如果你想看到它的运行,你必须使用 socket.io 客户端连接到它.为此,只需在 html 文件中包含 /socket.io/socket.io.js
.
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.
这足以建立连接.
socket = io.connect('http://localhost:5000');
没有显示console.log
的原因是websocket没有连接.只建立了 tcp/ip 连接,这就是 telnet 告诉你的.
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.
这篇关于Node.js Socket.IO 不能完全工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!