我正在创建一个视频聊天应用程序,但是当第二个人连接时,服务器返回一些奇怪的错误。请帮我解决这个错误。
当我打开新标签并粘贴相同的 URL 进行测试时,实际问题出现了。
我正在使用“simple-peer”:“^9.7.2”,
错误:对等体销毁后无法发出信号
这里也是截图
服务器代码
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;
}