【问题标题】:Socket.io - GET returns 404 (Not Found) (polling-xhr)Socket.io - GET 返回 404(未找到)(polling-xhr)
【发布时间】:2019-05-09 05:11:45
【问题描述】:

我在这里看到过类似的问题,但到目前为止我还没有找到解决问题的方法——我现在可能有点生气了。

我正在尝试使用 socket.io,到目前为止,我已经建立了一个快速服务器,并在服务器和客户端(socket.io)上运行了 socket.io -客户端)。

不幸的是,当我尝试从客户端与服务器通信时,我立即遇到重复 404 错误

GET http://localhost:3000/socket.io/?EIO=3&transport=polling&t=MU8YJvg 404 (Not Found)
GET http://localhost:3000/socket.io/?EIO=3&transport=polling&t=MU8YJvg 404 (Not Found)
GET http://localhost:3000/socket.io/?EIO=3&transport=polling&t=MU8YJvg 404 (Not Found)
...

基本上我的 BrowserSync 将在 localhost:3000 运行,而我的 express/socket.io 将在 localhost:8080 运行

我的 express 设置如下所示:

const express = require("express");
const app = express();
const path = require("path");
const proxy = require("http-proxy-middleware");
const http = require("http").Server(app);
const io = require("socket.io").listen(http);


io.on("connection", socket => {
  console.log("a user connected: ", socket.id);
});

app.set("port", 8080);

// Attempt to proxy around BrowserSync
app.use(
  "/socket-io",
  proxy({
    target: "http://localhost:8080",
    ws: true,
    pathRewrite: { "^/socket.io": "/" }
  })
);

http.listen(8080, () => {
  console.log(`Listening on ${http.address().port}`);
});

webpack.config.js

....
plugins: [
    definePlugin,
    new BrowserSyncPlugin({
      host: process.env.IP || "localhost",
      port: process.env.PORT || 3000,
      server: {
        baseDir: ["./", "./dist"]
      }
    })
]
...

我正在使用

(dev-deps)
    "browser-sync": "^2.26.3",
    "browser-sync-webpack-plugin": "^2.2.2",
(deps)
    "express": "^4.16.4",
    "socket.io": "^2.2.0",
    "socket.io-client": "^2.2.0",

我非常感谢您对此提供的任何帮助,因为没有任何意义了。

【问题讨论】:

    标签: express socket.io phaser-framework browser-sync


    【解决方案1】:

    似乎我陷入了对配置感到困惑的兔子洞。

    我将事情简化为:

    快递:

    const express = require("express");
    const app = express();
    const http = require("http").createServer(app);
    const io = require("socket.io").listen(http);
    const { addPlayerIO } = require("./utils/addPlayer.io");
    
    io.on("connection", socket => {
       console.log("a user connected: ", socket.id);
    });
    
    http.listen(8080, () => {
      console.log(`Listening on ${http.address().port}`);
    });
    

    webpack.config.js

    ....
    plugins: [
        definePlugin,
        new BrowserSyncPlugin({
          host: process.env.IP || "localhost",
          port: process.env.PORT || 3000,
          server: {
            baseDir: ["./", "./dist"]
          }
        })
    ]
    ...
    

    任何客户端页面

    const socketURL = 'http://localhost:8080' // whatever your socket port
    const socket = io(socketURL);
    
    socket.on("someEvent", data => {
        console.log(`I can now do something with ${data}`);
    });
    

    结果是所有 404 都消失了。我不是 100% 确定是什么原因造成的,尽管清理我的机器可以确保停止任何来自延迟连接的奇怪现象。

    【讨论】:

      猜你喜欢
      • 2017-02-25
      • 2017-09-04
      • 2020-11-24
      • 1970-01-01
      • 1970-01-01
      • 2021-07-09
      • 1970-01-01
      • 2014-01-16
      • 1970-01-01
      相关资源
      最近更新 更多