Socket.iov3不支持的协议版本错误
我坚持使用较新版本的 socket.io。一切都很好,但是当我想升级到 socket.io 3 时,一切都坏了,目前在客户端上,我收到了 400 HTTP 状态代码,其中包含以下 JSON 响应 -
{"code":5,"message":"Unsupported protocol version"}
服务器端配置 -
const io = require("socket.io")(server, {
cors: {
origin: config.clientURL,
methods: ["GET", "POST"],
credentials: true,
},
});
客户端配置 -
const socket = io(backendURL, {
withCredentials: true,
});
我尝试了很多东西并重新部署了很多次,但错误并没有消失。
作为参考,我有这些 github 存储库 -
客户端react.js- GitHub 存储库
服务器nodeJs和socket.io.js- GitHub 存储库
回答
看起来 socket.io-client 和 socket.io 服务器的版本之间可能不匹配。
首先,将allowEIO3设置为true更新服务器(在socket.io@3.1.0中添加)
const io = require("socket.io")({
allowEIO3: true // false by default
});
升级socket.io-client(现在最新的是3.1.1)后,您可以将其设置回原位,或将其删除,因为默认值为false
const io = require("socket.io")({
allowEIO3: false
});