1

我正在尝试使用 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));
   }
  }

服务器.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);
  });
});
4

0 回答 0