问题描述
晚上好, 我正在为套接字连接而苦苦挣扎。 我在 nodejs-express 后端使用 socket.io,在角度前端使用 ngx-socket-io,但在连接时我不断收到 400 stat ({"code":2,"message":"Bad handshake method"})与 200 统计宽度 sid 交替:0{"sid":"REeYoGC5t_JnTp4dAABp","upgrades":"websocket"],"pingInterval":25000,"pingTimeout":5000}).
[这里是chrome的net tab截图][1]
这里是服务器
const app = express();
const server = http.createServer(app);
server.listen(PORT,() => {
console.log("Server is listening on " + PORT,colors.yellow);
init();
});
const io = require("socket.io")(server,{
cors: {
origin: ["http://localhost:4200","https://myurl.com",],credentials: true
}
});
io.on("connection",(socket) => {
console.log("Connected");
socket.emit("ok","ok");
socket.on("disconnect",(data) => {
});
});
这里有客户
const config: SocketIoConfig = {
url: environment.SERVER_ENDPOINT,options: { autoConnect: false,withCredentials: true}
};
...
imports: [
SocketIoModule.forRoot(config)
]
...
有人可以帮我解决这个问题吗? [1]:https://i.stack.imgur.com/72YSB.png
编辑 我已经使用 socket.io-client 而不是 ngx-socket-io 解决了这个问题
解决方法
尝试降级版本,socket.io 2.3.1没问题