簡體   English   中英

僅在桌面上使用 socket.io 出現 xhr 輪詢錯誤

[英]Getting xhr poll error with socket.io on desktop only

我正在使用 socket.io 在 web 上制作多人游戲,但只有我在桌面上(而不是在手機上)遇到此錯誤。
完整錯誤是

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", () => {

})

我發現我的服務器路由器正在使用端口0808作為默認網關,所以我更改了端口並且一切正常(:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM