简体   繁体   中英

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

In Javascript, I'm trying to send ice candidates via websockets (json).

I started out with this sample .

Before I use candidate.toJSON() and/or JSON.stringify() the candidate looks something like this:

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:

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. When i try to JSON.parse, I get an "Unexpected token o in JSON at position 1"

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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