首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WebRTC连通性问题

WebRTC连通性问题
EN

Stack Overflow用户
提问于 2017-12-05 15:35:41
回答 1查看 179关注 0票数 0

最近我一直在用WebRTC做实验。我试着把我的头绕在它周围,同时看看我是否能测试它。

所以我写了一个测试来检查两件事

  1. 联系是否一致?
  2. 节点的数量会减缓连接的速度吗?如果是的话,那么在它变得明显之前,阈值是多少?

我在考试中注意到的是,(1)不是真的。我所发现的是,这个测试的平均结果是100次中的10到20次?

我要求的是有人看一看,让我知道我是否写错了我的测试,或者也许找出了失败的另一个原因。

请注意,我只在Chrome 62上运行这个程序,冰服务器列表是从https://github.com/DamonOehlman/freeice获取的。

下面是我写的一个测试:

代码语言:javascript
复制
const localStream = await navigator.mediaDevices.getUserMedia({video:true});
const iceServers = [{
    urls: [
        "stun:stun.l.google.com:19302",
        "stun:stun1.l.google.com:19302",
        "stun:stun2.l.google.com:19302",
        "stun:stun3.l.google.com:19302",
        "stun:stun4.l.google.com:19302",
        "stun:stun.ekiga.net",
        "stun:stun.ideasip.com",
        "stun:stun.rixtelecom.se",
        "stun:stun.schlund.de",
        "stun:stun.stunprotocol.org:3478",
        "stun:stun.voiparound.com",
        "stun:stun.voipbuster.com",
        "stun:stun.voipstunt.com",
        "stun:stun.voxgratia.org"
    ]
}]

async function initExample(){
    try{
        // Init Local Connections
        var localConnection = new RTCPeerConnection({iceServers});
        var remoteConnection = new RTCPeerConnection({iceServers});

        // Create Data Channels
        var remoteDataChannel;// this is added later
        var localDataChannel = localConnection.createDataChannel('DataChannel');

        // Configure Stream
        var remoteStream;// this is added later
        localConnection.addStream(localStream)

        // Catch DataChannel when it is sent from local to remote
        remoteConnection.ondatachannel = dataChannelEvent => {
            remoteDataChannel = dataChannelEvent.channel
        }

        // Catch MediaStream when it is sent from local to remote
        remoteConnection.onaddstream = mediaStreamEvent => {
            remoteStream = mediaStreamEvent.stream;
        }

        // Create Offer
        var offer = await localConnection.createOffer();
        await localConnection.setLocalDescription(offer);
        await remoteConnection.setRemoteDescription(localConnection.localDescription);

        // Create Answer
        var answer = await remoteConnection.createAnswer();
        await remoteConnection.setLocalDescription(answer);
        await localConnection.setRemoteDescription(remoteConnection.localDescription);

        // Return Results
        return {
            localConnection, remoteConnection,
            localDataChannel, localStream,
            get remoteDataChannel(){
                return remoteDataChannel
            },
            get remoteStream(){
                return remoteStream;
            },
        }
    }catch(e){
        console.error(e)
    }
}

async function test(N=100){
    const stats = {pass:0, fail: 0, N};
    const results = [];
    for (var i = 0; i < N; i++) {
        console.log({...stats, i})
        let conn = await initExample();
        if(conn.remoteDataChannel){
            stats.pass++;
        }else{
            stats.fail++;
        }
        results.push(conn);
    }
    return results;
}

var r = await test();
EN

回答 1

Stack Overflow用户

发布于 2017-12-05 18:11:45

因此,在@JTejedor的评论和建议之后,我意识到我的连接测试设置错误。对于任何有此问题的人,请注意,您需要确保同侪正在发送他们的注册冰候选人到对方。

在我的脱机版本中,这样做如下:

代码语言:javascript
复制
// Map Ice Candidate
remoteConnection.onicecandidate = evt => {
    if(evt.candidate) {
        localConnection.addIceCandidate(evt.candidate)
    }
}
localConnection.onicecandidate = evt => {
    if(evt.candidate) {
        remoteConnection.addIceCandidate(evt.candidate)
    }
}

如果使用套接字(或其他服务器端解决方案),请确保通过套接字将这些套接字发送给其他对等方。

下面是工作测试(总是通过):

代码语言:javascript
复制
// Ice Servers
const iceServers = [{
    urls: [
        "stun:stun.l.google.com:19302",
        "stun:stun1.l.google.com:19302",
        "stun:stun2.l.google.com:19302",
        "stun:stun3.l.google.com:19302",
        "stun:stun4.l.google.com:19302",
        "stun:stun.ekiga.net",
        "stun:stun.ideasip.com",
        "stun:stun.rixtelecom.se",
        "stun:stun.schlund.de",
        "stun:stun.stunprotocol.org:3478",
        "stun:stun.voiparound.com",
        "stun:stun.voipbuster.com",
        "stun:stun.voipstunt.com",
        "stun:stun.voxgratia.org"
    ]
}]

// Run the test once
async function initExample(){
    // Init Local Connections
    var localConnection = new RTCPeerConnection({iceServers});
    var remoteConnection = new RTCPeerConnection({iceServers});//this is really "remote" its simulated

    // Map Ice Candidate
    remoteConnection.onicecandidate = evt => {
        if(evt.candidate) {
            localConnection.addIceCandidate(evt.candidate)
        }
    }
    localConnection.onicecandidate = evt => {
        if(evt.candidate) {
            remoteConnection.addIceCandidate(evt.candidate)
        }
    }

    // Create Data Channels
    var remoteDataChannel;// this is added later
    var localDataChannel = localConnection.createDataChannel('DataChannel');

    // Tie end of promise to getting the datachannel
    return new Promise( async res => {
        // Catch DataChannel when it is sent from local to remote
        remoteConnection.ondatachannel = dataChannelEvent => {
            console.log('GOT CHANNEL', dataChannelEvent)
            remoteDataChannel = dataChannelEvent.channel
            res(remoteDataChannel);
        }

        // Create Offer
        var offer = await localConnection.createOffer();
        await localConnection.setLocalDescription(offer);
        await remoteConnection.setRemoteDescription(localConnection.localDescription);

        // Create Answer
        var answer = await remoteConnection.createAnswer();
        await remoteConnection.setLocalDescription(answer);
        await localConnection.setRemoteDescription(remoteConnection.localDescription);

    })
}

// A Promisified Timeout
const promisedTimeout = (timeout = 10000) => new Promise((resolve, reject) => {
  let wait = window.setTimeout(() => {
    window.clearTimeout(wait);
    resolve(false);
  }, timeout)
})

// Run the test N times
async function test(N=100){
    // Init variables
    const stats = {pass:0, fail: 0, N};
    const results = [];

    // Loop
    for (var i = 0; i <= N; i++) {
        // log
        console.log({...stats, i});

        // This will each example against a timeout, 
        // if the timeout finishes first, then this test will be marked as fail
        let pass = await Promise.race([
            initExample(),
            promisedTimeout()
        ]);
        if(pass) {
            stats.pass ++;
        }else{
            stats.fail ++;
        }
        results.push(pass);
    }
    return {results,stats};
}

// Run the test
test()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47657337

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档