serverconnection.onmessage =在createOffer之后从未调用过gotMessageFromServer

时间:2016-03-08 13:45:15

标签: webrtc peer-connection

我正在尝试使用WebRTC和websockets连接到浏览器进行视频聊天。

我能够createoffer获得冰候选者但功能(我期望远程流响应)serverconnection.onmessage永远不会被调用。

我的index.html 中的

function pageReady() {
   localVideo = document.getElementById("localVideo");
   remoteVideo = document.getElementById("remoteVideo");

   serverConnection = new WebSocket('ws://localhost:3434');
   serverConnection.onmessage = gotMessageFromServer;

   var constraints = {
     video: true,
     audio: true,
   };

   if (navigator.getUserMedia) {
     navigator.getUserMedia(constraints, getUserMediaSuccess,  getUserMediaError);
   } else {
     alert('Your browser does not support getUserMedia API');
   }
  }

  function start(isCaller) {
   peerConnection = new RTCPeerConnection(peerConnectionConfig, optional);
   console.log('the peer connection is', peerConnection);
   peerConnection.onicecandidate = gotIceCandidate;
   peerConnection.onaddstream = gotRemoteStream;
   peerConnection.addStream(localStream);
   if (isCaller)
     peerConnection.createOffer(gotDescription, createOfferError);
  }

  function gotMessageFromServer(message) {
   console.log('message is', json);
   if (!peerConnection) start(false);

   var signal = JSON.parse(message.data);
   if (signal.sdp) {
     console.log('message is signal.sdp', signal.sdp);
     peerConnection.setRemoteDescription(new RTCSessionDescription(signal.sdp), function () {
       peerConnection.createAnswer(gotDescription, createAnswerError);
     });
   } else if (signal.ice) {
     console.log('message is signal.ice', signal.ice);
     peerConnection.addIceCandidate(new RTCIceCandidate(signal.ice));
   }
  }

server.js

 var WebSocketServer = require('ws').Server;
 var wss = new WebSocketServer({ noServer: true });

 var http = require('http'),
 fs = require('fs');

 fs.readFile('../index.html', function (err, html) {
   if (err) {
    throw err;
  }
  var server = http.createServer(function (request, response) {
   response.writeHeader(200, { "Content-Type": "text/html" });
   response.write(html);
   response.end();
 })

  server.listen(3434);
  server.on('upgrade', function (req, socket, head) {
   wss.handleUpgrade(req, socket, head, function (client) {
   });
 });
  wss.on('connection', function connection(ws) {
    ws.on('message', function incoming(message) {
      console.log('received: %s', message);
    });

    ws.send(message);
  });
});

0 个答案:

没有答案