我使用 peerjs-server 为 webRTC 编写了一个简单的 Node Express 服务器和简单的客户端使用 peerjs 。在 localhost 上一切正常,但是当我在 vps 上尝试时,出现错误:
Firefox can't connect with server ws://my.vps/peerjs/peerjs?key=peerjs&id=hj3hpekwaa38fr00&token=ymtfvhagiw
PeerJS: Socket closed.
PeerJS: ERROR Error: Lost connection to server.
Error: "Lost connection to server."
emitError https://cdnjs.cloudflare.com/ajax/libs/peerjs/0.3.16/peer.min.js:1:16426
_initializeServerConnection https://cdnjs.cloudflare.com/ajax/libs/peerjs/0.3.16/peer.min.js:1:12260
emit https://cdnjs.cloudflare.com/ajax/libs/peerjs/0.3.16/peer.min.js:1:25516
onclose https://cdnjs.cloudflare.com/ajax/libs/peerjs/0.3.16/peer.min.js:1:19350
服务器:
const express = require('express');
enter code here`const app = express();
const ExpressPeerServer = require('peer').ExpressPeerServer;
app.use(express.static('./public'));
const server = app.listen(80, () => { // 3000 on localhost
console.log('Express server listen on port ' + 80);
});
const options = { debug: true };
const peerserver = ExpressPeerServer(server, options);
app.use('/peerjs', peerserver);
app.use('/*', express.static('./public/index.html'));
客户:
var peer = new Peer('', {
host: location.hostname,
port: location.port || (location.protocol === 'https:' ? 443 : 80),
path: '/peerjs',
debug: 3
});
peer.on('open', function (id) {
console.log(id);
});
任何帮助表示赞赏。
最佳答案
看起来您正在与服务器 ws://my.vps/
连接,它是到 http://my.vps/ 的服务器的 Web 套接字。这似乎不存在。
它可能也应该使用 https(或 wss)
关于node.js - 如何解决在 vps 上设置 WebRTC 服务器的问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53635070/