以下代码是使用webRTC连接的客户端和服务器端代码。它在localhost中运行良好。当我部署到linux共享托管服务器时,出现错误。
这是我的服务器端代码。
var http = require('http');
var express = require('express');
var ExpressPeerServer = require('peer').ExpressPeerServer;
var app = express();
var server = http.createServer(app);
var options = {
debug: true,
key: 'peerjs',
allow_discovery: true,
ssl: {
key: '',
cert: ''
},
proxied: true
};
var expressPeerServer = ExpressPeerServer(server, options);
app.use('/api', expressPeerServer);
app.use('/', express.static('.'));
app.use('/list', function (req, res) {
return res.json(Object.keys(expressPeerServer._clients.peerjs));
});
var port = process.env.PORT || 8080;
server.listen(port, function () {
console.log('Basic-ss live at', port);
});
expressPeerServer.on('connection', function (id) {
console.log('Peer connected with id:', id);
});
expressPeerServer.on('disconnect', function (id) {
console.log('Peer %s disconnected', id);
});
以下是连接对等方的客户端代码。
initPeer(peerId) {
this.peer = new Peer(peerId, {
host: 'localhost',
port: 8080,
path: '/api',
key: 'peerjs',
debug: 3,
config: {
'iceServers': [
{ url: 'stun:stun.l.google.com:19302' },
]
}
});
setTimeout(() => {
this.currentPeerId = this.peer.id;
if (this.currentPeerId !== null) {
isPeerConnected = true;
} else {
isPeerConnected = false;
}
console.log('Current Peer ID', this.currentPeerId);
}, 4000);
}
此代码在localhost中可以正常运行。 这是我在服务器中上传的代码。
this.peer = new Peer(peerId, {
host: '', ===> my domain name here.
port: 8080,
path: '/api',
key: 'mebstelemedclinic',
debug: 3,
config: {
'iceServers': [
{ url: 'stun:stun.l.google.com:19302' },
{
url: 'turn:192.158.29.39:3478?transport=udp',
credential: 'JZEOEt2V3Qb0y27GRntt2u2PAYA=',
username: '28224511:1379330808'
}
]
}
});
如果使用相同的服务器代码和上面的客户端代码,则会出现连接错误。
WebSocket connection to 'wss://mydomain.in:8080/api/peerjs?key=peerjs&id=LY42201PH-yKy04f-ygqZJhqrBmezG6&token=n42tatqqn9e' failed: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT
我从很久以前就被打了。帮助我如何克服这个错误。谢谢。
答案 0 :(得分:0)
您应将以下代码添加到/etc/nginx/sites-available
中的路径nano site_name中:
location {
proxy_pass Ip address; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host $host; }