如何使用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 (将#修改为@)