gpt4 book ai didi

node.js - peer 被销毁后无法发出信号。简单点

转载 作者:行者123 更新时间:2023-12-03 12:19:21 26 4
gpt4 key购买 nike

我是 webrtc 的初学者。我使用 simple-peer 在视频通话应用程序上连接两个对等点。当连接第二个点时,会显示他的视频,但几秒钟后会抛出一个错误,显示“点被销毁时无法发出信号”。我不知道我哪里错了。我附上了下面的代码。

此行抛出代码错误:

PeerRef.current.signal(signal);

客户端.js :

import React, { useEffect,useRef } from 'react';
import io from 'socket.io-client';
import Peer from 'simple-peer';
import styled from 'styled-components';

const Container = styled.div`
display: flex;
width: 100%;
height: 100vh;
flex-direction: row;
`;

const LeftRow = styled.div`
width: 40%;
height: 100%;
`;

const RightRow = styled.div`
flex: 1;
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
`;

const Video = styled.video`
height: 50%;
width: 100%;
border: 1px solid black;
`;

const Room=(props)=>{
const socketRef=useRef();
const userRef=useRef();
const partnerRef=useRef();
const PeerRef=useRef();


useEffect(()=>{
navigator.mediaDevices.getUserMedia({video:true,audio:true}).then((stream)=>{
userRef.current.srcObject=stream;

socketRef.current=io.connect('/');
console.log("hello");
console.log(props.match.params.roomID);
socketRef.current.emit("join room",props.match.params.roomID);

socketRef.current.on("other user",(PartnerID)=>{
console.log("creator");
if(PartnerID){
console.log("partner");
PeerRef.current=createPeer(PartnerID,socketRef.current.id,stream);
}
});
socketRef.current.on("caller signal",(incoming)=>{
console.log("Caller signal from browser");
PeerRef.current=addPeer(incoming.CallerID,incoming.signal,stream);

});
socketRef.current.on("callee signal",(signal)=>{
console.log("callee signal from browser");

PeerRef.current.signal(signal);
});

})


},[]);

function handleStream(stream){
partnerRef.current.srcObject=stream;

}
const createPeer = (PartnerID,CallerID,stream) =>{
const peer = new Peer({
initiator:true,
trickle:false,
stream
});
peer.on("signal",(signal)=>{
const payload={
PartnerID,
CallerID,
signal
}
socketRef.current.emit("call partner",payload);

});
peer.on("stream",handleStream);
//peer.on("data",handleData);
return peer;
}

const addPeer = (CallerID,insignal,stream) =>{
console.log("inside addpeer");
const peer = new Peer({
initiator:false,
trickle:false,
stream
});
peer.on("signal",(signal)=>{
console.log("inside peer");
const payload={
CallerID,
signal
}
socketRef.current.emit("accept call",payload);
});
peer.on("stream",handleStream);

peer.signal(insignal);

return peer;
}


return(
<Container>
<LeftRow>
<Video autoPlay ref={userRef} />
<Video autoPlay ref={partnerRef} />
</LeftRow>
<RightRow>

</RightRow>
</Container>
);


}

export default Room;

服务器.js:

const express = require("express");
const http = require("http");
const app = express();
const server = http.createServer(app);
const socket = require("socket.io");
const io = socket(server);
server.listen(5000);

const rooms={};

io.on('connection',(socket)=>
{
console.log("Connected");

socket.on("join room",(roomID)=>{
console.log("User here");
if(rooms[roomID])
rooms[roomID].push(socket.id);
else
rooms[roomID]=[socket.id];
const otherUser = rooms[roomID].find(id => id!==socket.id);

socket.emit("other user",otherUser);

});
socket.on("call partner",(incoming)=>{
console.log("call partner from server");
const payload={
CallerID:incoming.CallerID,
signal:incoming.signal
}
io.to(incoming.PartnerID).emit("caller signal", payload);
});
socket.on("accept call",(incoming)=>{
console.log("accept call");
io.to(incoming.CallerID).emit("callee signal",incoming.signal);
});
});

最佳答案

同样的错误,我用的是9.7.2版的simple-peer。它似乎有一个错误,所以将版本降到 9.6.2 修复了错误。

关于node.js - peer 被销毁后无法发出信号。简单点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62554153/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com