【问题标题】:Getting xhr poll error with socket.io on desktop only仅在桌面上使用 socket.io 获取 xhr 轮询错误
【发布时间】:2021-06-08 08:07:44
【问题描述】:

我正在使用 socket.io 在网络上制作多人游戏,但只有我在桌面上(而不是在手机上)收到此错误。
完整错误是

socket.min.io.js:6 GET http://my.public.ip:8080/socket.io/?EIO=4&transport=polling&t=Ndge5Hh net::ERR_CONNECTION_TIMED_OUT

我通过这样做发现了 xhr poll 错误:

socket.on("connect_error", (err) => {
    console.log(`connect_error due to ${err.message}`);
});

它显示connect_error due to xhr poll error

我已正确完成所有端口转发。

奇怪的是,我在同一个文件上运行了 2 台服务器(但添加了两个事件监听器),其中一台服务器运行良好,但另一台没有,所以我认为同一个文件是问题,所以我将它们合并为单独的文件,但错误仍然出现。

这是两个服务器文件:

//Login.js THE ONE THAT WORKS

const httpServer = require("http").createServer();

const io = require("socket.io")(httpServer, {
    cors: {
        origin: "http://my.website",
        methods: ["GET", "POST"]
    }
});

const port = 8081;

io.on("connection", (client) => {
    console.log(`${client.id} joined`)
});

httpServer.listen(port);
//Game.js THE ONE THAT DOSENT WORK
const httpServer = require("http").createServer();

const io = require("socket.io")(httpServer, {
    cors: {
        origin: "http://my.website",
        methods: ["GET", "POST"]
    }
});


io.on("connection", (client) => {
    console.log('joined')
});

httpServer.listen(port);

Update(); //A Function Thought this might be useful

编辑: 我忘记添加客户端 javascript 所以这里是:

//Connecting to Game.js
const socket = io("http://same.ip.checked:8080");//Ive tried http / https / none they all dont work


socket.on("connect", () => {
    console.log("connected");
})

//Connecting to Login.js (One that works)
const socket = io("same.ip.checked:8081");

socket.once("connect", () => {

})

【问题讨论】:

    标签: javascript node.js sockets websocket socket.io


    【解决方案1】:

    我发现我的服务器路由器正在使用端口0808 作为默认网关,所以我更改了端口并且一切正常(:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-02
      • 2018-01-13
      • 1970-01-01
      • 1970-01-01
      • 2012-02-06
      • 1970-01-01
      • 2018-03-14
      • 1970-01-01
      相关资源
      最近更新 更多