我在这里看到过类似的问题,但到目前为止,我一直无法找到解决问题的方法-我现在可能会发疯。
我正在尝试使用 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运行
我的表达设置如下:
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",
在此方面的任何帮助,我将非常感谢,因为没有任何意义了。
答案 0 :(得分:0)
似乎我陷入了将自己与config混淆的困境。
我将事情简化为:
表达:
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%知道是什么原因造成的,尽管清理机器可以确保不再因连接中断而感到陌生。