繁体   English   中英

WebSocket 连接失败:连接建立错误

[英]WebSocket Connection failed: error in connection establishment

以下代码是使用 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);
});

以下是connect peer的客户端代码。

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);
    }

这段代码在本地主机上运行良好。 这是我在服务器中上传的代码。

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

很长一段时间以来,我在这一点上受到了打击。 帮助我如何克服这个错误。 谢谢你。

您应该将以下代码添加到/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;     } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM