问题描述
我已经使用WebRTC
在两个同伴之间建立了视频聊天.我想允许一个对等方结束聊天,让另一个对等方知道聊天已结束.聊天结束后,需要为两个对等方执行一些代码. PeerConnection
对象具有removeStream()
方法,该方法应触发onremovestream()
侦听器.这对于我的用例来说是完美的,但是,在调用onremovestream()之前,对等体之间需要重新协商(报价/答复).就我而言,这似乎很奇怪.为什么我只重新谈判才能断开连接? PeerConnection对象不应该意识到其他用户没有流吗? 我的问题:如何结束PeerConnection并提醒其他用户?
I've set up a video chat between two peers using WebRTC
. I would like to allow a peer to end the chat and for the other peer to know that the chat was ended. Once the chat ends, some code needs to be executed for both peers. The PeerConnection
object has a removeStream()
method which should trigger the onremovestream()
listener. This would be perfect for my use case, however, before onremovestream() is called there needs to be a re-negotiation (offer/answer) between the peers. For my case this seems odd. Why would I re-negotiate only to disconnect? Shouldn't the PeerConnection object realize no stream is coming from the other user? My question: how can I end a PeerConnection and alert the other user?
推荐答案
您可以简单地关闭连接.另一个对等方可以侦听信号oniceconnectionstatechange
,并且状态disconnected
表示您的对等方已关闭连接/不再可用.如果您是我,我会在关闭之前进行超时,因为这种状态可能是由于不稳定的网络连接所致. MDN具有一些不错的信息
You can simply close the connection. The other peer can listen for the signal oniceconnectionstatechange
and the state of disconnected
means that your peer has closed the connection/is no longer available. If I were you, I would do a timeout before closing as this state could be because of a flaky network connection. MDN has some good info
或者,您可以跟踪与信令服务器的连接,并在有人离开时向对等节点发信号.无论哪种方法都可以.
Or, you could keep track of connections with your signalling server and signal the peers when somebody leaves. Either way should work.
这篇关于在两个对等端之间结束WebRTC视频通话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!