大家好,我如何拒绝或接受来自发件人对等方的呼叫或要约,我只是使用Peerjs客户端和对等方服务器
这是我的发件人客户端
const peer = new Peer('sender', { host: '1.0.0.99', port: 9000, path: '/' })
var call = document.getElementById('call');
call.addEventListener('click',startChat);
function startChat(){
navigator.mediaDevices.getUserMedia({ video: true}).then((localStream) =>{
document.querySelector('video#local').srcObject = localStream;
var call = peer.call('receiver',localStream);
call.on('stream',remoteStream => {
document.querySelector('video#remote').srcObject = remoteStream
})
})
}
这是我的接收者
const peer = new Peer('receiver', { host: '1.0.0.99', port: 9000, path: '/' })
peer.on('call', call => {
const startChat = async () => {
const localStream = await navigator.mediaDevices.getUserMedia({
video: true
})
document.querySelector('video#local').srcObject = localStream
// call.answer(localStream)
call.close(mediaStream);
call.on('stream', remoteStream => {
document.querySelector('video#remote').srcObject = remoteStream
})
}
startChat();
})
我对接收者的目标可能会下降并接受,抱歉,我是新手,谢谢您的帮助
答案 0 :(得分:0)
根据官方文档,MediaConnection API指出应使用close()
方法拒绝呼叫,并使用answer()
方法接受呼叫。在您的代码中,即使您将参数传递给close()
函数,它也没有尝试任何情况,但是您都尝试了这两种方法。现在,如果您关闭媒体连接,则我认为'stream'消息上的回调无效。
答案 1 :(得分:0)
我找到了方法
peer.on('call', call => {
var acceptsCall = confirm("Videocall incoming, do you want to accept it ?");
if (acceptsCall) {
const startChat = async () => {
const localStream = await navigator.mediaDevices.getUserMedia({
video: true
})
document.querySelector('video#local').srcObject = localStream
call.answer(localStream)
call.on('stream', remoteStream => {
document.querySelector('video#remote').srcObject = remoteStream
});
// Handle when the call finishes
call.on('close', function () {
alert("The videocall has finished");
});
}
startChat();
} else {
alert('call decline!');
}
})
答案 2 :(得分:0)
我发现的解决方案之一是开始对话,并在0.1秒后关闭连接,这不是最好的解决方案之一,但它对我的服务最多。