简体   繁体   English

在指定套接字空间/命名空间的同时使用NGINX + Socket.io

[英]Using NGINX + Socket.io while specifying socket room/namespace

I just updated a node+socket app to use https. 我刚刚更新了node + socket应用以使用https。 It took a while, but I now have a working nginx, node.js, socket.io configuration here. 花费了一段时间,但是我现在在这里可以使用nginx,node.js,socket.io配置。 It works great, but my question is, is it possible to configure the server side so that the client doesn't need to submit the path parameter, or at least include it as part of the url? 它很好用,但是我的问题是,是否可以配置服务器端,以便客户端不需要提交path参数,或者至少将其包含在url中? I'd like to be able to give users just a url to use, for example, https://mywebsite.com/apiPath/myNamespace . 我希望能够仅向用户提供要使用的网址,例如https://mywebsite.com/apiPath/myNamespace

Currently: 目前:

NGINX CONFIG: NGINX配置:

upstream websocket1 {
    server 127.0.0.1:3000;
} 
server {
    listen 80;
    listen [::]:80;
    server_name mywebsite.com;
    return 301 https://mywebsite.com$request_uri;
}
server {
        listen 443 ssl;
        server_name         mywebsite.com;
        ssl_certificate     this.crt;
        ssl_certificate_key this.pem;
        location /apiPath/ {
                proxy_pass http://websocket1/socket.io/;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "Upgrade";
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
}

SERVER: 服务器:

var express = require('express');
var app = express();

var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 3000;
http.listen(port);

var nsp = io.of('/myNamespace');

nsp.on('connection', function(socket) {
  console.log('connected!');
});

CLIENT: 客户:

var socket = require('socket.io-client')(https://mywebsite.com/myNamespace, {path: '/apiPath'});

Thanks in advance! 提前致谢!

You can opt to not use the path parameter, but the path is the namespace. 您可以选择不使用path参数,但是路径是名称空间。 If you don't supply one, it just uses a namespace of / . 如果不提供,则仅使用/的命名空间。 If you only need the one namespace, that should be fine. 如果只需要一个名称空间,那应该没问题。 Instead of specifying .of() on your io calls, you can simply do io.to() and it will use the default namespace 除了指定的.of()在你的io电话,你可以简单地做io.to()它会使用默认的命名空间

Well, I found a solution to my issue here . 好吧,我在这里找到了解决我问题的方法。 With this new setup, I don't need to pass a path parameter and can use namespaces as part of my url. 使用此新设置,我不需要传递path参数,并且可以将名称空间用作我的url的一部分。 My revised setup looks like this: 我修改后的设置如下所示:

NGINX CONFIG: NGINX配置:

upstream websocket1 {
    server 127.0.0.1:3000;
} 
server {
    listen 80;
    listen [::]:80;
    server_name mywebsite.com;
    return 301 https://mywebsite.com$request_uri;
}
server {
        listen 443 ssl;
        server_name         mywebsite.com;
        ssl_certificate     this.crt;
        ssl_certificate_key this.pem;
        location ~(myNamespace|socket\.io).*$ {
                proxy_pass http://websocket1;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "Upgrade";
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
}

SERVER: 服务器:

var express = require('express');
var app = express();

var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 3000;
http.listen(port);

var nsp = io.of('/myNamespace');

nsp.on('connection', function(socket) {
  console.log('connected!');
});

CLIENT: 客户:

var socket = require('socket.io-client')(https://mywebsite.com/myNamespace);

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

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