Peerjs不断失去连接,用户标识丢失

时间:2018-11-09 04:34:05

标签: webrtc peer peerjs

我已经按照使用peerjs的教程制作了一个应用程序。除了当我使用peerjs进行视频通话时,其他一切似乎都正常。我已经制作了自己的peerjs服务器,该服务器正在localhost上运行(现在正在测试)。这是对等服务器的代码:

const express = require('express');
const path = require('path');
const http = require('http');
const cors = require('cors');
const errorhandler = require('errorhandler');
var ExpressPeerServer = require('peer').ExpressPeerServer;

var options = {
  debug: true,
  key: 'copycat'
};

var app = express();
var server = http.createServer(app);

var port = process.env.PORT || '3001';

app.set('port', port);
app.use(cors());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/peerjs', ExpressPeerServer(server, options));
app.use(errorhandler());

process.on('uncaughtException', function(exc) {
  console.error(exc);
});

server.listen(port);

如您所见,我正在端口3001上运行该应用程序。现在,以下是视频通话的peerjs连接脚本:

// PeerJS
    // Compatibility shim
    navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
    // PeerJS object
    var peer = new Peer(username + roomId, {
        host: 'localhost',
        path: '/peerjs',
        port: 443,
        secure: true,
        key: 'copycat',
        debug: true
    });

    peer.on('open', function () {
        $('#my-id').text(peer.id);
    });

    // Receiving a call
    peer.on('call', function (call) {
        // Answer the call automatically (instead of prompting user) for demo purposes
        call.answer(window.localStream);
        step3(call);
    });

    peer.on('error', function (err) {
        alert(err.message);
        // Return to step 2 if error occurs
        step2();
    });

    // Click handlers setup
    $(function () {
        $('#make-call').click(function () {
            // Initiate a call!
            var call = peer.call($('#callto-id').val(), window.localStream);
            step3(call);
        });
        $('#end-call').click(function () {
            window.existingCall.close();
            step2();
        });
        step1();
    });
    function step1() {
        // Get audio/video stream
        navigator.getUserMedia({ audio: true, video: true }, function (stream) {
            // Set your video displays
            $('#my-video').prop('src', URL.createObjectURL(stream));
            window.localStream = stream;
            step2();
        }, function () { $('#step1-error').show(); });
    }

    function step2() {
        $('#step1, #step3').hide();
        $('#step2').show();
    }

    function step3(call) {
        // Hang up on an existing call if present
        if (window.existingCall) {
            window.existingCall.close();
        }
        // Wait for stream on the call, then set peer video display
        call.on('stream', function (stream) {
            $('#second-video').prop('src', URL.createObjectURL(stream));
        });
        // UI stuff
        window.existingCall = call;
        $('#second-id').text(call.peer);
        call.on('close', step2);
        $('#step1, #step2').hide();
        $('#step3').show();
    }

这几乎是github上peerjs示例文件中的示例代码。我感到困惑的是端口值。在上述脚本的选项内部,其端口443。当我尝试进行视频通话时,在chrome中出现以下错误:

peer.js:1492 WebSocket connection to 'wss://localhost/peerjs/peerjs?key=peerjs&id=User80925be509c6c606fa21409858f5&token=zz69b3ccyk' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
Socket._startWebSocket @ peer.js:1492
Socket.start @ peer.js:1481
Peer._initialize @ peer.js:1058
Peer @ peer.js:962
(anonymous) @ 5be509c6c606fa21409858f5:183
peer.js:1741 PeerJS:  Socket closed.
peer.js:1741 PeerJS:  ERROR Error: Lost connection to server.
peer.js:1555 POST https://localhost/peerjs/peerjs/User80925be509c6c606fa21409858f5/zz69b3ccyk/id?i=0 net::ERR_CONNECTION_REFUSED
Socket._startXhrStream @ peer.js:1555
Socket.start @ peer.js:1480
Peer._initialize @ peer.js:1058
Peer @ peer.js:962
(anonymous) @ 5be509c6c606fa21409858f5:183
peer.js:1741 PeerJS:  ERROR Error: Lost connection to server.

请告知我我在做什么错??

1 个答案:

答案 0 :(得分:0)

如果在本地使用,则使用本地端口,即3001,否则使用443 制作这样的物体

var peer = new Peer(undefined, {

    host: 'localhost',
    path: '/peerjs',
    port: 3001,
    secure: true,
    key: 'copycat',
    debug: true
});