对等点被破坏后如何​​修复信号?

How to fix signal after peer is destroyed?

我正在创建一个视频聊天应用程序,但是当第二个人连接时,服务器 return 一些奇怪的错误。请帮我解决这个错误。

当我打开新标签并粘贴相同的 URL 进行测试时,实际问题出现了。

我正在使用“简单对等”:“^9.7.2”,

Error: cannot signal after peer is destroyed

这里也是截图

服务器代码

  socket.on("returning-signal", (payload) => {
    io.to(payload.callerID).emit("receiving-returned-signal", {
      signal: payload.signal,
      id: socket.id,
    });
  });

客户端

 socketRef.current = io.connect("http://localhost:1234");
    navigator.mediaDevices
      .getUserMedia({ video: videoConstraints, audio: false })
      .then((stream) => {
        userVideo.current.srcObject = stream;
        socketRef.current.emit("join-room", roomID);
        socketRef.current.on("all-users", (users) => {
          const peers = [];
          users.forEach((userID) => {
            const peer = createPeer(userID, socketRef.current.id, stream);
            peersRef.current.push({
              peerID: userID,
              peer,
            });
            peers.push(peer);
          });
          setPeers(peers);
        });

        socketRef.current.on("user-joined", (payload) => {
          const peer = addPeer(payload.signal, payload.callerID, stream);
          peersRef.current.push({
            peerID: payload.callerID,
            peer,
          });

          setPeers((users) => [...users, peer]);
        });

        socketRef.current.on("receiving-returned-signal", (payload) => {
          const item = peersRef.current[0];
          item.peer.signal(payload.signal);
        });
      });

  function createPeer(userToSignal, callerID, stream) {
    const peer = new Peer({
      initiator: true,
      trickle: false,
      stream,
    });

    peer.on("signal", (signal) => {
      socketRef.current.emit("sending-signal", {
        userToSignal,
        callerID,
        signal,
      });
    });

    return peer;
  }

找到解决方案

问题出在这个版本 "simple-peer": "^9.7.2" 中,我将版本更改为 9.6.2 并且效果很好。 ✨