为什么我的PeerJS音频聊天应用无效?

时间:2016-02-19 16:46:23

标签: javascript node.js webrtc audio-streaming peerjs

我有一个运行php的apache服务器和我的前端peerjs代码:

<script>
    $(function(){

        //returns element value of GET key (e.g '&test=2'returns 2)
        function getParameterByName(name){
            name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
            var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
                results = regex.exec(location.search);
            return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
        }

        function getAudio(successCallback, errorCallback){
            navigator.getUserMedia({
                audio: true,
                video: false
            }, successCallback, errorCallback);
        }

        //request call
        var from = getParameterByName('from');
        var to = getParameterByName('to');

        $('#start-call').click(function(){

            console.log('starting call...');

            getAudio(
                function(MediaStream){

                    console.log('now calling ' + to);
                    var call = peer.call(to, MediaStream);
                    call.on('stream', onReceiveStream);
                },
                function(err){
                    console.log('an error occured while getting the audio');
                    console.log(err);
                }
            );

        });
        //recieve call
        function onReceiveCall(call){

            console.log('peer is calling...');
            console.log(call);

            getAudio(
                function(MediaStream){
                    call.answer(MediaStream);
                    console.log('answering call started...');
                },
                function(err){
                    console.log('an error occured while getting the audio');
                    console.log(err);
                }
            );

            call.on('stream', onReceiveStream);
        }

        function onReceiveStream(stream){
            var audio = document.querySelector('audio');
            audio.src = window.URL.createObjectURL(stream);
            audio.onloadedmetadata = function(e){
                console.log('now playing the audio');
                audio.play();
            }
        }

        var peer = new Peer({host: 'www.example.com', port:9000, path:''});
        peer.on('open', function(id) {
          console.log('My peer ID is: ' + id);
        });

        peer.on('call', onReceiveCall);

    });
</script>
<div id = "start-call"> Start Call </div>
<audio controls></audio>

然后我有一个节点服务器在端口9000上运行对等服务器:

var fs = require('fs');
var PeerServer = require('peer').PeerServer;

var server = PeerServer({
  port: 9000,
  ssl: {
    key: fs.readFileSync('/xxx/xxx/privkey.pem'),
    cert: fs.readFileSync('/xxx/xxx/cert.pem')
  },
  proxied: true
});

server.on('connection', function(id) {
  console.log("connecion made:" + id);
});

使用chrome,前端控制台中不会出现任何错误,我会收到提示"Connection made: 35hy2o4ydfqw"。 因此,当我单击开始调用时,控制台会输出"now calling xxx..."但是在另一侧根本没有收到任何内容,所以我认为它是

peer.on('call', onReceiveCall);

未收到来电。关于接收器端出了什么问题的提示非常少,绝对没有任何反应。

感谢您提供任何帮助或建议。

2 个答案:

答案 0 :(得分:0)

您是在localhost还是远程网站工作?如果您在远程网站上工作,请确保您拥有安全(HTTS)站点。 Navigator.getUserMedia仅适用于SSL网站。

答案 1 :(得分:0)

您应该修改此行var peer = new Peer(from, {host: 'www.example.com', port:9000, path:''});

from - 这是来电

的ID