简体   繁体   English

如何在 webrtc 中的候选冰上使用 .toJSON() 是 javascript?

[英]How to use .toJSON() on an ice candidate in webrtc is javascript?

In Javascript, I'm trying to send ice candidates via websockets (json).在 Javascript 中,我正在尝试通过 websockets (json) 发送候选冰。

I started out with this sample .我从这个示例开始。

Before I use candidate.toJSON() and/or JSON.stringify() the candidate looks something like this:在我使用 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

After I use.toJSON like recommended here , the it looks like this:在我使用这里推荐的.toJSON 之后,它看起来像这样:

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

As you can see, many properties are missing.如您所见,缺少许多属性。 On top of that, when I try to run "toString()" like that same link suggests, that method does not exist.最重要的是,当我尝试像同一个链接建议的那样运行“toString()”时,该方法不存在。 When i try to JSON.parse, I get an "Unexpected token o in JSON at position 1"当我尝试 JSON.parse 时,我得到一个“在 position 1 的 JSON 中的意外令牌 o”

I think the problem is that this has proto or because of its type?我认为问题在于它有原型还是因为它的类型? But I can't seem to get much further then that.但我似乎不能再进一步了。

Any help to jsonify it and send it would be appreciated.任何对 jsonify 和发送它的帮助将不胜感激。

That is intentional, see the spec .这是故意的,请参阅规范 The additional properties are encoded in the candidate string already, so if you do this on the remote end:附加属性已编码在候选字符串中,因此如果您在远程端执行此操作:

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})

the properties will be restored.属性将被恢复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM