【问题标题】:socket.io net::ERR_CONNECTION_TIMED_OUTsocket.io 网络::ERR_CONNECTION_TIMED_OUT
【发布时间】:2015-04-08 16:30:30
【问题描述】:

我正在使用 socket.io 和 unity 进行测试。

在我的本地主机(我的电脑)中确实可以完美运行,但现在我在主机上查看它是如何在线工作的,但我遇到了以下问题:

URL应用程序在线:http://almightysystem.com.ar/UnityApps/UnityChatSocket.io/

我有这个循环错误:

GET http://almightysystem.com.ar:29011/socket.io/?EIO=3&transport=polling&t=1423454072758-3 net::ERR_CONNECTION_TIMED_OUT

server.js:

var express = require('express');
var port = process.env.PORT || 29011;

var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.use(express.static(__dirname + '/public'));

io.on('connection', function(socket){
    socket.on('chat message', function(message){
        io.emit('chat message', message);
    });
});

http.listen(port, function(){
    console.log('Express server listening on port ' + port);
});

client.js:

//SOCKET.IO
var socket = io.connect('http://almightysystem.com.ar:29011/');

socket.on('chat message', function(message){
    SendMessage ('ChatSystem', 'GetChatMessage', message);
    console.log("Mensaje recibido del Servidor hacia Unity: " + message);
});

function UnitySendMessage(message){
    socket.emit('chat message', message);
    console.log("Mensaje de Unity al Servidor: " + message);
}

有什么想法吗?

【问题讨论】:

  • 这个'http://almightysystem.com.ar/UnityApps:29011'中的:29011是什么?如果它是一个端口号,那么它不在正确的位置。它会紧跟在域之后。
  • 天哪,你是对的。现在可以了,但我还有另一个问题 GET almightysystem.com.ar:29011/socket.io/… net::ERR_CONNECTION_TIMED_OUT

标签: node.js express socket.io


【解决方案1】:

我有这个问题的答案。

在您的客户端代码中。

// SOCKET.IO

var socket = io.connect('http://almightysystem.com.ar:29011/');

socket.on('chat message', function(message){
    SendMessage ('ChatSystem', 'GetChatMessage', message);
    console.log('Mensaje recibido del Servidor hacia Unity: ' + message);
});

function UnitySendMessage(message){
    socket.emit('chat message', message);
    console.log('Mensaje de Unity al Servidor: ' + message);
}

删除connect函数中的端口号。

var socket = io.connect('http://almightysystem.com.ar:29011/');

像这样。

var socket = io.connect('http://almightysystem.com.ar');

【讨论】:

    【解决方案2】:

    在您的服务器代码中,您应该使用 server.listen(port, ...) 而不是 app.listen(port, ...),因为 app.listen() creates a new http server 并附加到它,您不想要它,因为您已经有一个 (server)。

    另外,在您的客户端代码中,您应该使用io.connect('http://almightysystem.com.ar:29011/'); 而不是io.connect('http://almightysystem.com.ar/UnityApps:29011');,因为socket.io 服务器不知道要监听/UnityApps(因为您没有传递配置对象(使用@ 987654331@ 设置)到socket.io server constructor),即使您已将:29011 部分移动到正确的位置。

    【讨论】:

    【解决方案3】:

    我遇到了同样的问题,用

    解决了
    var socket = io.connect();
    

    而不是

    var socket = io.connect('http://almightysystem.com.ar:29011/');
    

    【讨论】:

      【解决方案4】:

      在您的客户端 const socket = io("http://almightysystem.com.ar:29011/", { transports: ["websocket"] }); 上尝试此操作以指定您要使用 websockets

      【讨论】:

        猜你喜欢
        • 2017-09-01
        • 2017-01-22
        • 2018-02-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-30
        相关资源
        最近更新 更多