WebRTC dataChannel永远不会打开

时间:2018-02-07 10:06:07

标签: javascript webrtc

我已经在Codepen上创建了一个演示程序,它基于https://codelabs.developers.google.com/codelabs/webrtc-web/#0文章。

我有两个按钮first被执行来监听(如文章中的join事件),第二个是创建连接(如创建跟随就绪事件)。 主要功能如下:

function createPeerConnection(isInitiator, config) {
  console.log('Creating Peer connection as initiator?', isInitiator, 'config:',
              config);
  peerConn = new RTCPeerConnection(config);

  // send any ice candidates to the other peer
  peerConn.onicecandidate = function(event) {
    console.log('icecandidate event:', event);
    if (event.candidate) {
      sendMessage({
        type: 'candidate',
        label: event.candidate.sdpMLineIndex,
        id: event.candidate.sdpMid,
        candidate: event.candidate.candidate
      });
    } else {
      console.log('End of candidates.');
    }
  };

  if (isInitiator) {
    console.log('Creating Data Channel');
    dataChannel = peerConn.createDataChannel('photos');
    onDataChannelCreated(dataChannel);

    console.log('Creating an offer');
    peerConn.createOffer(onLocalSessionCreated, logError);
  } else {
    peerConn.ondatachannel = function(event) {
      console.log('ondatachannel:', event.channel);
      dataChannel = event.channel;
      onDataChannelCreated(dataChannel);
    };
  }
}

但数据通道上的onopen事件从未执行且通道处于连接状态。当我在Chromium中的同一台计算机(隐身模式中)打开它时,代码正常工作。

我在互联网上找到的Turn和Stun服务器。有没有办法测试它们是否正常?

我正在使用连接到同一路由器的两台计算机测试此代码,但其中一台使用VPN。

1 个答案:

答案 0 :(得分:1)

1。)您可以使用webRTC-tester测试您的网络是否正确设置了呼叫: https://test.webrtc.org/

2。)您可以使用以下方法测试Turn and Stun Server是否正常工作: https://webrtc.github.io/samples/src/content/peerconnection/trickle-ice/