在 Javascript 中,我尝试通过 websockets (json) 发送ice候选人。
我从这个开始 sample .
在我使用 Candidate.toJSON() 和/或 JSON.stringify() 之前,候选者看起来像这样:
address: "192.168.1.2"
candidate: "candidate:149645548 1 udp 1685987071 192.168.1.2 50697 typ srflx raddr 192.168.8.162 rport 50697 generation 0 ufrag o8rk network-id 1 network-cost 10"
component: "rtp"
foundation: "149645548"
port: 50697
priority: 1685987071
protocol: "udp"
relatedAddress: "192.168.8.162"
relatedPort: 50697
sdpMLineIndex: 1
sdpMid: "1"
tcpType: ""
type: "srflx"
usernameFragment: "o8rk"
__proto__: RTCIceCandidate
在我像推荐的那样使用.toJSON之后here ,它看起来像这样:
candidate: "candidate:149645548 1 udp 1685987071 192.168.1.2 50697 typ srflx raddr 192.168.8.162 rport 50697 generation 0 ufrag o8rk network-id 1 network-cost 10"
sdpMLineIndex: 1
sdpMid: "1"
__proto__: Object
如您所见,缺少许多属性。最重要的是,当我尝试像同一个链接所示运行“toString()”时,该方法不存在。当我尝试 JSON.parse 时,我得到一个“JSON 中位置 1 处出现意外的标记 o”
我认为问题是它有原型(prototype)还是因为它的类型?但我似乎无法更进一步。
任何将其json化并发送的帮助将不胜感激。
最佳答案
这是故意的,请参阅 the spec 。 其他属性已编码在候选字符串中,因此如果您在远程端执行此操作:
new RTCIceCandidate({candidate: "candidate:149645548 1 udp 1685987071 192.168.1.2 50697 typ srflx raddr 192.168.8.162 rport 50697 generation 0 ufrag o8rk network-id 1 network-cost 10", sdpMid: 0})
属性将被恢复。
关于javascript - 如何在 webrtc 的 javascript 中使用 .toJSON() ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61577412/