大佬教程收集整理的这篇文章主要介绍了无法关闭 peerjs 上的 peer 之间的连接,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试制作视频聊天应用程序,它适用于 peerJs。现在我想添加一些功能,比如如果连接被用户破坏,连接到他的另一个用户会收到通知。
这是我调用其他用户的代码
call(userID: String,partnerID: String) {
const conn = this.peer.connect(partnerID);
conn.on('open',() => {
conn.on('data',(data) => {
console.log(data);
})
conn.send(userID);
})
if (this.peer.destroyed) {
this.createPeer(thiS.UserID);
}
if (this.myEl.classList.contains('disableCall')) {
console.log('Show');
this.myEl.classList.add('showCall');
this.partnerEl.classList.add('showCall');
this.myEl.classList.remove('disableCall');
this.partnerEl.classList.remove('disableCall');
}
this.hIDeCallLogin = false;
const call = this.peer.call(partnerID,this.myStream);
this.mediaConnection = call;
call.on('stream',(stream) => {
this.partnerEl.srcObject = stream;
this.hIDeCall = true;
this.hIDeCallLogin = false;
if (call.peerConnection.connectionState == 'connecTing') {
this.swapVIDeo('my-vIDeo');
}
});
call.on('close',() => {
console.log('Di hentikan oleh penerima');
this.mediaConnection.close();
this.myEl.classList.remove('showCall');
this.partnerEl.classList.remove('showCall');
this.myEl.classList.add('disableCall');
this.partnerEl.classList.add('disableCall');
this.hIDeCall = false;
this.hIDeCallLogin = false;
})
}
以下是其他用户的回答
wait() {
this.peer.on('call',(call) => {
this.mediaConnection = call;
//change this to a modal for confirm a call
var acceptsCall = confirm(partner + " is calling,do you want to accept it ?");
if (acceptsCall) {
call.answer(this.myStream); // Answer the call with an A/V stream.
call.on('stream',(stream) => {
if (this.myEl.classList.contains('disableCall')) {
console.log('Show');
this.partnerEl.classList.add('showCall');
this.myEl.classList.add('showCall');
this.partnerEl.classList.remove('disableCall');
this.myEl.classList.remove('disableCall');
}
this.partnerEl.srcObject = stream;
this.status = 'Connected';
this.hIDeCallLogin = false;
this.swapVIDeo('my-vIDeo');
});
call.on('close',() => {
console.log('Di hentikan oleh penelpon');
this.myEl.classList.remove('showCall');
this.partnerEl.classList.remove('showCall');
this.myEl.classList.add('disableCall');
this.partnerEl.classList.add('disableCall');
console.log(this.status);
this.hIDeCall = false;
this.hIDeCallLogin = false;
})
}
});
//getTing partner ID
let partner = '';
this.peer.on('connection',(conn) => {
conn.on('open',() => {
conn.on('data',(data) => {
partner = data;
})
})
})
}
我想要的是,如果 1 个对等挂断并自动关闭他们的视频,对等连接断开或销毁,有人知道该怎么做吗?
wait() {
this.peer.on('call',(call) => {
this.mediaConnection = call; // Save reference to the `mediaConnection` object
var acceptsCall = confirm("Videocall incoming,do you want to accept it ?");
if (acceptsCall) {
call.answer(this.myStream); // Answer the call with an A/V stream.
call.on('stream',(stream) => {
this.partnerEl.srcObject = stream;
this.status = 'Connected';
console.log(this.status);
});
}
});
}
然后当你想挂断时你应该可以拨打this.mediaConnection.close()
为了让它在调用端工作,你还必须在那里存储 mediaConnection。
call(partnerId: String) {
this.partnerId = partnerId;
console.log(this.peer.destroyed);
if (this.peer.destroyed) {
this.createPeer(thiS.UserId);
}
const call = this.peer.call(partnerId,this.myStream);
this.mediaConnection = call; // Save reference to the `mediaConnection` object
call.on('stream',(stream) => {
this.partnerEl.srcObject = stream;
this.hideCall = true;
this.hideCallLogin = false;
});
}
以上是大佬教程为你收集整理的无法关闭 peerjs 上的 peer 之间的连接全部内容,希望文章能够帮你解决无法关闭 peerjs 上的 peer 之间的连接所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。