gpt4 book ai didi

javascript - SimpleWebRTC 对等发现无法正常工作

转载 作者:行者123 更新时间:2023-12-03 04:28:55 25 4
gpt4 key购买 nike

我正在使用此处找到的 SimpleWebRTC 库:https://simplewebrtc.com

我的信号主站正在运行,并且 STUN/TURN 配置正确。它能够检测其他对等点,因此我认为 STUN/TURN 功能正常。我的问题是,当一个对等方开始其本地视频时,其他对等方不会发现它,除非他们重新加载页面。我想要它,这样它就会自动推送到其他对等点,而不需要重新加载页面。我认为这与下面的代码有关(我从示例中获取),但我不确定。

我将 autoRequestMedia 设置为 false 的原因是因为我希望用户能够查看其他对等方的摄像头,而无需打开自己的设备(也是我在 readToCall 事件中没有 webrtc.joinRoom 的原因)。

目前,用户点击按钮,会触发startLocalVideo();并且视频是在元素中创建的。问题是除非其他对等点重新加载页面,否则不会将任何内容推送到其他对等点。希望这能解释一切,如果您需要更多详细信息,请告诉我。

var webrtc = new SimpleWebRTC({
// the id/element dom element that will hold "our" video
localVideoEl: 'localCam',
// the id/element dom element that will hold remote videos
remoteVideosEl: '',
// immediately ask for camera access
autoRequestMedia: false,
autoRemoveVideos: true,
url: 'MY SIGNAL-MASTER URL HERE',
localVideo: {
autoplay: true, // automatically play the video stream on the page
mirror: false, // flip the local video to mirror mode (for UX)
muted: true // mute local video stream to prevent echo
}
});

webrtc.joinRoom('testchannel');

// a peer video has been added
webrtc.on('videoAdded', function (video, peer) {
console.log('video added', peer);
var remotes = document.getElementById('remoteCams');
if (remotes) {
var container = document.createElement('div');
container.className = 'videoContainer';
container.id = 'container_' + webrtc.getDomId(peer);
container.appendChild(video);
// suppress contextmenu
// video.oncontextmenu = function () { return false; };
remotes.appendChild(container);
}
});

// a peer video was removed
webrtc.on('videoRemoved', function (video, peer) {
console.log('video removed ', peer.nick);
var remotes = document.getElementById('remoteCams');
var el = document.getElementById(peer ? 'container_' + webrtc.getDomId(peer) : 'localScreenContainer');
if (remotes && el) {
remotes.removeChild(el);
}
});

最佳答案

您必须将 join 语句放入 readyToCall 监听器中:

webrtc.on('readyToCall', function() {
webrtc.joinRoom('roomname');
})

将 joinRoom 调用放入 setTimout 函数中。

关于javascript - SimpleWebRTC 对等发现无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43578472/

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