我正在使用 simple-peer 来简化 webrtc 的使用,并且一切正常。但是我面临一个问题,当广播公司启动流并且观众加入流时,没有视频显示给观众。但是,如果广播公司刷新他的页面,视频就会显示在观众端。
广播者.js
componentDidMount() {
navigator.mediaDevices.getUserMedia({
video: true,
audio: true,
})
.then(stream => {
// Display own video
if("srcObject" in this.localVideo.current) {
this.localVideo.current.srcObject = stream;
}
else {
this.localVideo.current.src = window.URL.createObjectURL(stream);
}
// Stream video to attendees
this.gotMedia(stream);
})
.catch(error => {
console.error(error);
});
}
gotMedia = (stream) => {
const broadcaster = new Peer({ initiator: true, stream: stream });
broadcaster.on("signal", data => {
this.socket.emit("signal", data);
});
this.socket.on("signal", data => {
broadcaster.signal(data);
});
};
查看器.js
componentDidMount() {
const attendee = new Peer();
attendee.on("signal", data => {
this.socket.emit("signal", data);
});
this.socket.on("signal", data => {
attendee.signal(data);
});
// Get remote video stream and display it
attendee.on("stream", stream => {
if("srcObject" in this.remoteVideo.current) {
this.remoteVideo.current.srcObject = stream;
}
else {
this.remoteVideo.current.src = window.URL.createObjectURL(stream);
}
});
}
服务器
// Handle socket connections
io.on("connection", socket => {
socket.on("signal", (data) => {
socket.broadcast.emit("signal", data);
});
});