微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用Twilio配置WebRTC对等连接

如何解决如何使用Twilio配置WebRTC对等连接

我正在尝试配置WebRTC应用程序以使用Twilio的TURN服务器。请注意,我正在使用Twilio的试用帐户。

我已经尝试过了

首先,我正在发送一个从Twilio检索ICE服务器的请求(是的,我知道从客户端请求是不安全的。此刻,我只想知道它是如何工作的。)

function requestIceServers(){
let accountSid = 'xxx';
let authToken = 'xxx';

const url = "https://api.twilio.com/2010-04-01/Accounts/" + accountSid + "/Tokens.json";

fetch(url,{
    headers: {
      Authorization: "Basic " + btoa(accountSid + ":" + authToken)
    },method: "POST"
  })
.then(response => {
    if(response.ok)
        return response.json();
    else
        throw new Error("Failed to get ICE servers");
})
.then( data => pcConfig = data.ice_servers)
.catch((error) => {
    console.log(error);
    pcConfig = {
        iceServers: [
            {
                urls: 'stun:stun.l.google.com:19302'
            }
        ]
    };
})
};

然后我尝试配置对等连接

function createPeerConnection() {
if (typeof pcConfig !== "undefined"){
    try {
        turnServers = pcConfig.shift(); //trying to force turn servers. But anyway I also tried without shifting the array.
        console.log(turnServers);
        pc = new RTCPeerConnection(turnServers);
        pc.onicecandidate = handleIceCandidate;
        pc.onaddstream = handleRemoteStreamAdded;
        pc.onremovestream = handleRemoteStreamRemoved;
        console.log('Created RTCPeerConnnection');
    } catch (e) {
        console.log('Failed to create PeerConnection,exception: ' + e.message);
        alert('Cannot create RTCPeerConnection object.');
        return;
    }
}
else {
    console.log("ICE servers are undefined");
}
}

从日志中可以看到,我从Twilio获得了成功的响应。我认为问题在于我如何处理返回的数据或如何配置对等连接。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。