Socket.io 客户端初始化似乎没有按照记录工作 (4.x)

Socket.io client initialization doesn't seem to be working as documented (4.x)

我的 scoket.io 服务器托管在暴露在 example.com/<id>/socket.io/

端点的虚拟化硬件上

和 socket.io 默认情况下会尝试连接到 example.com/socket.io/ 所以我需要自定义使用的端点。 According to the docs for 4.x:

In the examples above, the client will connect to the main namespace. Using only the main namespace should be sufficient for most use cases, but you can specify the namespace with:

// same origin version
const socket = io("/admin");
// cross origin version
const socket = io("https://server-domain.com/admin");

所以我选择了:

const socket = io("/<id>/socket.io");

也尝试过:

const socket = io("/<id>/socket.io/");

const socket = io("https://example.com/<id>/socket.io");

const socket = io("https://example.com/<id>/socket.io/");

但是失败的轮询消息显示它正在尝试连接到默认 https://example.com/socket.io/

我可以验证套接字服务器 运行 在正确的端点,因为访问 https://example.com/<id>/socket.io/ 提供消息:{"code":0,"message":"Transport unknown"}https://example.com/<id>/socket.io/socket.io.js 提供 Socket.IO v4.0.0

我可以做些什么来进一步调试或解决这个问题?

编辑:尝试使用 v3 并得到相同的结果。

您要做的是让 Socket.IO 连接到不同的 路径 ,而不是 命名空间 .简而言之,您应该使用 path 选项,如下所示:

// The string passed as the first argument is the Namespace to use,
// not the path to connect to.
const socket = io("/", {
  // Set the <id> to your actual path ID.
  path: "/<id>/socket.io/"
});

详细了解 Socket.IO 命名空间 here