获取 http://localhost:8080/socket.io/?EIO=3&transport=polling&t=McNiz_D 404(未找到)



我正在基于nodejs中的websocket作为后端和离子4应用程序创建服务器作为前端,当我试图连接到我的服务器时,我会收到此错误

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

这是我的服务器代码

const PORT = 8080;
const WebSocket = require("ws").Server;
var express = require("express");
var app = express();
app.use(function(req, res, next) {
 res.header("Access-Control-Allow-Origin", "http://localhost:8100");
 res.header(
  "Access-Control-Allow-Headers",
  "Origin, X-Requested-With, Content-Type, Accept"
);
 res.header("Access-Control-Allow-Credentials", true);
 next();
});
var http = require("http").createServer(app);
http.listen(PORT, "localhost", function() {
console.log("listening in http://localhost:" + PORT);
});
const wss = new WebSocket({ server: http });
 wss.on("connection", function connection(ws) {
  ws.on("message", function incoming(message) {
  console.log("received: %s", message);
 });
 ws.send("I am server ^_^");
});

以供将来参考,并且您需要更多澄清,这是因为在后端使用webockockets协议在前端使用WebSockets协议,而不是普通的WebSockockets Client。


socket.io想要连接到服务器时,升级请求将发送到http://localhost:8080/socket.io/?EIO=3&transport=polling&t=McNiz_D 404此路径。由于后端没有配置插座IO服务器,因此显然Express Server发送404 not found而不是升级响应。