简体   繁体   English

如何生成 ICE 候选人?

[英]How to generate an ICE candidate?

I'm developing a video conference with WebRTC in a local network, so I use only one signaling server to exchage SDP data.我正在本地网络中使用 WebRTC 开发视频会议,因此我只使用一个信令服务器来交换 SDP 数据。 As I understand, I also need to exchange ICE candidates, but I don't know how to generate them.据我了解,我也需要交换 ICE 候选人,但我不知道如何生成它们。 Thanks.谢谢。

You can get the generated iceCandidate by setting the peerConnection.onicecandidate event.您可以通过设置 peerConnection.onicecandidate 事件来获取生成的 iceCandidate。

(async () => {
  const pc = new RTCPeerConnection();
  pc.onicecandidate = evt => {
    console.log(evt.candidate?.candidate);
  };
  const stream = await navigator.mediaDevices.getUserMedia({video:true});
  stream.getTracks().forEach(track => pc.addTrack(track, stream));
  const offer = await pc.createOffer();
  await pc.setLocalDescription(offer);
})();

Do you create a configuration which contains at least one ice server url, and then use this configuration to create your RTCPeerConnection instance?您是否创建了一个包含至少一个 ice 服务器 url 的配置,然后使用此配置创建您的 RTCPeerConnection 实例? When you set an ice server url, the 'icecandidate' event should be fired.当你设置一个 ice 服务器 url 时,应该触发 'icecandidate' 事件。

 const configuration = { iceServers: [{ urls: "stun:stun.l.google.com:19302" }], }; const pc = new RTCPeerConnection(configuration); pc.addEventListener('icecandidate', event => { if (event.candidate) { console.log('icecandidate received: ', event.candidate); } });

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

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