如何修复“WebSocket 连接到‘wss://apiURL/socket.io/?EIO=4&transport=websocket’失败:”

问题描述 投票:0回答:0

socket.io 在本地主机服务器上运行良好,但通过 https 部署后,我不断收到此错误。

客户代码:

export const socket = io.connect(process.env.REACT_APP_SERVER_URL, {
  transports: ["websocket"],
  // withCredentials: true,
  cors: {
    origin: [process.env.REACT_APP_SERVER_URL],
    // origin: "*",
    credentials: true,
  },
});

后端代码:

  const io = socketIO(server, {
    cors: {
      origin: [process.env.CLIENT_URL],
      // origin: "*",
      credentials: true,
    },
  });

当我在console.log中输入“process.env.REACT_APP_SERVER_URL”的时候,https部署的server API就进来了,但是不知道socket通讯有什么问题。

如 socket.io 文档中所述,我尝试使用 [credentials: true, withCredentials: true],并尝试了与 Google 的 [origin: "*"] 相同的方法,但出现了所有相同的错误。 我该如何解决..? 任何帮助将不胜感激!

node.js reactjs websocket wss
© www.soinside.com 2019 - 2024. All rights reserved.