【发布时间】:2021-04-25 04:29:07
【问题描述】:
我正在尝试制作视频聊天应用,它适用于 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 个对等方挂断,我想要的是对等方断开连接或销毁,并自动关闭他们的视频。有人知道怎么做吗?
【问题讨论】:
-
尝试评论
disconnect,只留下destroy。看起来断开连接不是您需要的peerjs.com/docs.html#peerdisconnect -
另一件事要尝试
peer.connect.close()。看起来它应该是关闭连接的首选方法peerjs.com/docs.html#dataconnection-close -
我相信没有
peer.connect.close()因为 connect 需要一个 targetId 参数 -
其实我只是想关闭当前会话,你可以说,destroy 会断开与服务器的所有连接
-
尝试在 mediaConnection 上使用
.close()。你会得到一个作为on('call')listener 的参数
标签: angular ionic-framework peerjs