简体   繁体   English

Node.js Socket.io HTTPS-传输轮询

[英]Node.js Socket.io HTTPS - transport polling

I am using nginx with my ssl certificates and it's giving me the https version of my site which is great. 我正在将Nginx与ssl证书一起使用,这给了我网站的https版本,很棒。 The problem is my socket.io communications don't work unless i use a regular http connection. 问题是除非我使用常规的http连接,否则我的socket.io通信不起作用。 I apologize if this is a bit long, but i'm not sure what i'm doing here and wanted to make sure you guys had everything you might need to know. 如果这有点长,我深表歉意,但是我不确定我在这里做什么,并想确保你们拥有您可能需要知道的一切。 I have tried the solutions of various different sites some of which were on here, but none of them worked. 我尝试了各种不同站点的解决方案,其中一些站点位于此处,但没有一个起作用。

I tried manually creating the https server instead of letting express do it but that resulted in nothing loading at all, with the current implementation i can at least see the site. 我尝试手动创建https服务器,而不是让express这样做,但这导致根本不加载任何内容,使用当前的实现,我至少可以看到该站点。

upstream project {
   server example.org:4000;
}

server {
   listen 80;
   return https://$host$request_uri;
}

server {
   listen 443 ssl default_server;
   listen [::]:443 ssl default_server;
   ssl_certificate /home/adam/SSL/public.crt;
   ssl_certificate_key /home/adam/SSL/example.org.key;
   ssl_prefer_server_ciphers on;

   location / {
     proxy_pass http://example.org;
   }
}

That seems to be working as when i go to my site, it automatically takes me to the https version and the page is loaded. 这似乎在起作用,因为当我转到网站时,它会自动将我带到https版本并加载页面。 The issue is when is when the client side tries to connect i keep getting: 问题是何时客户端尝试连接时我不断得到:

" https://MY_SERVER_IP:4040/socket.io/?EIO=3&transport=polling&t=M0CPjUDnet::ERR_CONNECTION_CLOSED " https:// MY_SERVER_IP:4040 / socket.io /?EIO = 3&transport = polling&t = M0CPjUDnet :: ERR_CONNECTION_CLOSED

printed to the console 打印到控制台

Here's my client and server code: 这是我的客户端和服务器代码:

var IPaddress = 'https://MY_SERVER_IP:4040';
var socket = io.connect(IPaddress,{secure:true});
socket.on('connect', function (socket) {
    console.log('Connected!');
});

server code: 服务器代码:

var express = require('express');
var app = express();
var privateKey = fs.readFileSync(__dirname+'/SSL/example.com.key','utf8');
var certificate = fs.readFileSync(__dirname+'/SSL/public.crt','utf8');
var intermediate = fs.readFileSync(__dirname+'/SSL/intermediate.crt','utf8');
var options = {key:privateKey,cert:certificate,ca:intermediate};

var io = require('socket.io').listen(4040,options);
//var io = require('socket.io').listen(4040);
io.sockets.on('connection', function (socket) {
    socket.on('disconnect',function(){
       console.log("A client has left us :(");
    });
});
app.listen(4000);

Update - 02/12/2017 更新-02/12/2017

In my code i have this line: 在我的代码中,我有这行:

require('./routes.js')(app);

which contains: 其中包含:

module.exports = function(app) {

    app.get('/main',function(req,res){
        if (req.session.user == null){
            // if user is not logged-in redirect back to login page //
            res.redirect('/');
        }   else{
            res.sendFile(path.join(__dirname + '/FrontEnd/main.html'));
        }
    });

    // viewed at http://localhost:8080
    app.get('/', function(req, res) {
        if(req.cookies.user == undefined || req.cookies.pass == undefined){
            res.sendFile(path.join(__dirname + '/FrontEnd/login.html'));
        }else {
            //attempt automatic login
            AM.autoLogin(req.cookies.user,req.cookies.pass,function(o){
               if(o !=null){
                   req.session.user = o;
                   res.sendFile(path.join(__dirname + '/FrontEnd/home.html'));
               }else{
                   res.sendFile(path.join(__dirname + '/FrontEnd/login.html'));
               }
            });

        }
    });

...... ......

Could this be causing the 502 bad gateway error? 这可能会导致502错误的网关错误吗?

Expanding on my comment, in your current server code, both express and socket.io are only accepting regular HTTP connections. 扩展我的评论,在您当前的服务器代码中,express和socket.io都仅接受常规的HTTP连接。 The socket.io listen(port[, options]) function does not accept HTTPS connections. socket.io listen(port[, options])函数不接受HTTPS连接。

You started on the right approach with manually creating an HTTPS server. 您从正确的方法开始,手动创建了HTTPS服务器。 You then need to attach express and socket.io to that server. 然后,您需要将express和socket.io附加到该服务器。

var fs = require('fs');
var express = require('express');
var app = express();
var privateKey = fs.readFileSync(__dirname + '/SSL/example.com.key', 'utf8');
var certificate = fs.readFileSync(__dirname + '/SSL/public.crt', 'utf8');
var intermediate = fs.readFileSync(__dirname+'/SSL/intermediate.crt', 'utf8');
var options = { key: privateKey, cert: certificate, ca: intermediate };

// Create our HTTPS server here and attach express to it
var server = require('https').createServer(options, app);

// Attach socket.io to our existing HTTPS server
var io = require('socket.io')(server); 

io.sockets.on('connection', function (socket) {
    socket.on('disconnect', function() {
       console.log("A client has left us :(");
    });
});
server.listen(4000);

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

相关问题 Phaser.js中的Node.js和Socket.io未连接socket.io/?EIO=3&transport=polling - Node.js and Socket.io in Phaser.js not connecting socket.io/?EIO=3&transport=polling node.js表达4.11.2&socket.io出现错误?EIO = 3&transport = polling&t = - node.js express 4.11.2 & socket.io getting error ?EIO=3&transport=polling&t= Node.js socket.io/?EIO=3&transport=polling&t=LcuVjT6 404(未找到)反向代理 - Node.js socket.io/?EIO=3&transport=polling&t=LcuVjT6 404 (Not Found) reverse proxy 在3000端口上带有https的Node.js socket.io - Node.js socket.io with https on 3000 port Node.js、socket.io https 连接 - Node.js, socket.io https connection 将HTTPS分配给node.js socket.io - assign HTTPS to node.js socket.io Flutter socket.io 客户端未通过 https 与 node.js socket.io 服务器连接 - Flutter socket.io client not connecting with node.js socket.io server via https 使用Socket.Io传输轮询错误 - Transport polling error with Socket.Io 带有Socket.io的Node.js-长轮询失败,并抛出“代码”:1,“消息”:“会话ID未知”响应 - Node.js with Socket.io - Long Polling fails and throws “code”:1,“message”:“Session ID unknown” response 带有Socket.io的Node.js-长轮询失败,并抛出“代码”:1,“消息”:“会话ID未知”响应 - Node.js with Socket.io - Long Polling fails and throws “code”:1,“message”:“Session ID unknown” response
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM