javascript - WebSocket 连接失败:连接建立错误
问题描述
以下代码是使用 webRTC 连接的客户端和服务器端代码。它在本地主机中完美运行。当我部署到 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
很长一段时间以来,我都被打动了。帮助我如何克服这个错误。谢谢你。
解决方案
您应该将以下代码添加到您的路径 nano site_name 中/etc/nginx/sites-available
:
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; }
推荐阅读
- python - 如果每列每行有多个值,如何在 pandas 数据框中的两列之间创建字典?
- php - Google 服务帐户权限/找不到文件错误
- javascript - Javascript - HTML2canvas 生成的 PDF 模糊不清
- typescript - 在最近的 eslint 中使用 Enums 时如何避免阴影变量警告?
- c++ - 如何以 3 位小数精度在单独的行上排序和打印数字?
- android - 如何向我的应用添加欢迎图片?
- python - 删除包含子列表的列表
- aws-lambda - 如何使用异步函数在 AWS lambda 中调用 pinpoint.getUserEndpoints
- python - 对绘图中的拟合设置限制
- c - 格式字符串的转换规范中的引号是什么意思?