Node js - Socket.io-client 未连接到 socket.io 服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29108594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:32:19  来源:igfitidea点击:

Node js - Socket.io-client is not connecting to socket.io server

node.jswebsocketsocket.io

提问by exilonX

I am trying to connect to a socket.io-client using the following code:

我正在尝试使用以下代码连接到 socket.io-client:

Server:

服务器:

// Load requirements
var http = require('http'),
    io = require('socket.io');

// Create server & socket
var server = http.createServer(function(req, res){

    // Send HTML headers and message
    res.writeHead(404, {'Content-Type': 'text/html'});
    res.end('<h1>Aw, snap! 404</h1>');
});
server.listen(8080);
io = io.listen(server);

// Add a connect listener
io.sockets.on('connection', function(socket) {

    console.log('Client connected.');

    // Disconnect listener
    socket.on('disconnect', function() {
        console.log('Client disconnected.');
    });
});

Client:

客户:

console.log('1');

// Connect to server
var io = require('socket.io-client')
var socket = io.connect('localhost:8080', {reconnect: true});

console.log('2');

// Add a connect listener
socket.on('connect', function(socket) {
    console.log('Connected!');
});

console.log('3');

I don't get the Connected console log or Client Connected console log and I don't know why! The code sample is taken from another question posted: Linkand I don't see any solution to the problem...

我没有得到连接的控制台日志或客户端连接的控制台日志,我不知道为什么!代码示例取自发布的另一个问题:链接,我没有看到该问题的任何解决方案...

采纳答案by jfriend00

Assuming you are using a socket.io version greater than 1.0, on the server, change this:

假设您使用的是大于 1.0 的 socket.io 版本,请在服务器上更改以下内容:

// Add a connect listener
io.sockets.on('connection', function(socket) {

    console.log('Client connected.');

    // Disconnect listener
    socket.on('disconnect', function() {
        console.log('Client disconnected.');
    });
});

to this:

对此:

// Add a connect listener
io.on('connection', function(socket) {

    console.log('Client connected.');

    // Disconnect listener
    socket.on('disconnect', function() {
        console.log('Client disconnected.');
    });
});

See the socket.io documentation reference here.

请参阅此处的 socket.io 文档参考



You don't want to be listening for this event only on already connected sockets. You want to listen for this event on any socket, even a newly created one.

您不想仅在已连接的套接字上侦听此事件。您想在任何套接字上侦听此事件,甚至是新创建的套接字。



Also, be very careful when reading socket.io code in random places on the internet. Some things changed significantly from v0.9 to v1.0 (I don't know if this was one of those things or not). You should generally always start with the socket.io documentation site first since that will always represent the latest version. Then, if looking at other internet references, make sure you only use articles that are later than mid-2014. If you don't know the vintage of an article, it's best not to rely on it without corroboration from a more recent article.

另外,在互联网上随机位置阅读 socket.io 代码时要非常小心。有些东西从 v0.9 到 v1.0 发生了显着变化(我不知道这是否是其中之一)。您通常应该首先从 socket.io 文档站点开始,因为它始终代表最新版本。然后,如果查看其他互联网参考资料,请确保您只使用 2014 年年中之后的文章。如果你不知道一篇文章的年份,最好不要在没有最近文章证实的情况下依赖它。

回答by jinson

Also you need to add protocol with path.

您还需要添加带有路径的协议。

change

改变

var socket = io.connect('localhost:8080', {reconnect: true});

to

var socket = io.connect('http://localhost:8080', {reconnect: true});

回答by Guille_2783

you can use localhost. It works for me as well. You must use your ip address and port that works for you

你可以使用本地主机。它也适用于我。您必须使用适合您的 IP 地址和端口