使用WebRTC建立对等连接时发信号错误

时间:2018-11-12 03:35:43

标签: javascript webrtc

我一直在尝试使对等连接适用于我的应用程序,如下图所示:

console log

如您所见,它表明对等连接已经发生,但是存在一些不兼容错误,这可能是由于下图所示的 adapter.js 造成的。

error log

如果有人知道如何解决此感谢,将不胜感激!

我还通过pastebin添加了我正在使用的代码以供查看。 https://pastebin.com/WHCugjig

'use strict';

var localStream;
var remoteStream;
var pc;
var isInitiator;

var pcConfig = {
  iceServers: [
    {
      urls: 'stun:stun.l.google.com:19302'
    }
  ]
};

// Define action buttons.
const callButton = document.getElementById('callButton');
const hangupButton = document.getElementById('hangupButton');

/////////////////////////////////////////////

var socket = io.connect();

var room = prompt('Enter room name:');

socket.emit('create or join', room);

socket.on('created', function(room) {
  console.log('Created room ' + room);
  isInitiator = true;
  startVideo();
});

socket.on('created', function(room) {
  isInitiator = false;
  startVideo();
  createPeerConnection(isInitiator);
});

////////////////////////////////////////////////

function sendMessage(message) {
  socket.emit('message', message);
}

// This client receives a message
socket.on('message', function(message) {
  if (message.type === 'offer') {
    pc.setRemoteDescription(
      new RTCSessionDescription(message),
      function() {},
      onCreateSessionDescriptionError
    );
    pc.createAnswer(setLocalAndSendMessage, onCreateSessionDescriptionError);
  } else if (message.type === 'answer') {
    pc.setRemoteDescription(
      new RTCSessionDescription(message),
      function() {},
      onCreateSessionDescriptionError
    );
  } else if (message.type === 'candidate') {
    pc.addIceCandidate(
      new RTCIceCandidate({
        candidate: message.candidate
      })
    );
  }
});

////////////////////////////////////////////////////

var localVideo = document.querySelector('#localVideo');
var remoteVideo = document.querySelector('#remoteVideo');

// Set up initial action buttons status: disable call and hangup.
callButton.disabled = true;
hangupButton.disabled = true;

// Add click event handlers for buttons.
callButton.addEventListener('click', callStart);
hangupButton.addEventListener('click', hangupCall);

function startVideo() {
  navigator.mediaDevices
    .getUserMedia({
      audio: true,
      video: true
    })
    .then(gotStream)
    .catch(function(e) {
      alert('getUserMedia() error: ' + e.name);
    });
}

function gotStream(stream) {
  try {
    localVideo.srcObject = stream;
  } catch (error) {
    localVideo.src = window.URL.createObjectURL(stream);
  }
  localStream = stream;
  callButton.disabled = false;
}

function callStart() {
  createPeerConnection();
  pc.addStream(localStream);
  callButton.disabled = true;
  hangupButton.disabled = false;
  pc.createOffer(setLocalAndSendMessage, handleCreateOfferError);
}

/////////////////////////////////////////////////////////

function createPeerConnection() {
  try {
    pc = new RTCPeerConnection(null);
    pc.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.');
      }
    };
    pc.ontrack = handleRemoteStreamAdded;
    console.log('Created RTCPeerConnnection');
  } catch (e) {
    console.log('Failed to create PeerConnection, exception: ' + e.message);
    alert('Cannot create RTCPeerConnection object.');
    return;
  }
}

function handleRemoteStreamAdded(event) {
  console.log('Remote stream added.');
  try {
    remoteVideo.srcObject = event.stream;
  } catch (error) {
    remoteVideo.src = window.URL.createObjectURL(event.stream);
  }
  remoteStream = event.stream;
}

function setLocalAndSendMessage(sessionDescription) {
  pc.setLocalDescription(sessionDescription);
  console.log('setLocalAndSendMessage sending message', sessionDescription);
  sendMessage(sessionDescription);
}

function handleCreateOfferError(event) {
  console.log('createOffer() error: ', event);
}

function onCreateSessionDescriptionError(error) {
  console.log('Failed to create session description: ' + error.toString());
}

function hangupCall() {
  pc.close();
  pc = null;
}

1 个答案:

答案 0 :(得分:0)

如果没有运行的信令服务器版本,则很难调试它。反正...

您正在根据调试日志调用setLocalAndSendMessage。 然后,您两次收到“无法创建会话描述”错误,这表明调用了用于接收type = offer消息的代码。 现在,为什么发生这种情况取决于您的信令服务器。