【发布时间】:2019-05-28 04:38:19
【问题描述】:
我有两个想要通过 WebRTC 相互连接的对等方。通常,第一个对等点会创建一个提议并通过信令通道/服务器将其发送给第二个对等点,第二个对等点会以答案进行响应。此方案运行良好。
但是,是否有可能支持两个对等方碰巧同时尝试相互连接的情况,两者都通过信令服务器同时向彼此发送 SDP 提议。
// Both peers do this simultaneously:
const conn = new RTCPeerConnection(null);
const sdpOffer = await conn.createOffer();
await conn.setLocalDescription(sdpOffer);
signalingService.send(peerId, sdpOffer);
// At some point in the future both peers also receive an SDP offer
// (rather than answer) from the other peer whom they sent an offer to
// via the signaling service. If this was an answer we'd call
// RTCPeerConnection.setRemoteDescription, however this doesn't work for an
// offer:
conn.setRemoteDescription(peerSDPOffer);
// In Chrome results in "DOMException: Failed to execute 'setRemoteDescription' on 'RTCPeerConnection': Failed to set remote offer sdp: Called in wrong state: kHaveLocalOffer"
我什至尝试通过将 SDP 类型从 offer 重写为 answer 和 setup:actpass 到 setup:active 来将收到的同行提议“转换”为答案,但这似乎不起作用,相反我只是得到一个新的例外。
所以问题是,这种同时连接/提供用例是否以某种方式支持 - 还是我应该关闭一侧/对等 RTCPeerConnection 并使用 RTCPeerConnection.createAnswer 实例化一个新的?
【问题讨论】:
-
我的信令服务器本质上为每个对等点分配了一个会话 ID;我避免这种双重报价的简单策略是具有较高 id 的对等方开始报价。你需要这样的某种协调。
-
谢谢@deceze - 这正是我要做的事情
标签: javascript webrtc sdp