我正在使用 socket.io 和 peer js,试图让两个人像在视频通话中一样共享屏幕。我可以显示初始视频,但不能显示新用户。我想我正在以正确的方式将流传递回服务器。任何见解将不胜感激。
const socket = io('/')
const myPeer = new Peer(undefined, {
host: '/',
port: '3001'
})
const videoGrid = document.getElementById('video-grid')
const myVideo = document.createElement('video')
myVideo.muted = true
navigator.mediaDevices.getDisplayMedia().then(stream => {
addVideoStream(myVideo, stream)
myPeer.on('call', call => {
call.answer(stream)
})
socket.on('user-connected', userId => {
console.log(stream)
connectToNewUser(userId, stream)
})
});
myPeer.on('open', id => {
socket.emit('join-room', ROOM_ID, id)
})
function connectToNewUser(userId, stream) {
const call = myPeer.call(userId, stream)
const video = document.createElement('video')
call.on('stream', userVideoStream => {
addVideoStream(video,userVideoStream)
console.log(userVideoStream)
})
call.on('close', () => {
video.remove()
})
}
function addVideoStream(video, stream) {
video.srcObject = stream
video.addEventListener('loadedmetadata', () => {
video.play()
})
videoGrid.append(video)
}```