nginx Flutter中的Socket客户端内部计算方法无法在远程服务器上运行

oug3syen  于 2022-12-11  发布在  Nginx
关注(0)|答案(1)|浏览(114)

我正在尝试使用flutter的计算方法在后台实现套接字连接。带有套接字的计算方法在Node.js本地服务器上工作,并成功地在后台发出所有事件。由于我托管了我的Node.js套接字服务器,客户端无法发出或侦听任何事件。我可以看到它已连接的日志,也可以记录套接字ID。我不确定这里出了什么问题。
BG方法调用

compute(DummyBG,DummyArgs(message:widget.arguments.message,sender_id:widget.arguments.user_id));

虚拟BG

Future forwardMessageBG(ForwardArgs args) async {
 SocketUtils _socketUtils = SocketUtils();
  
 await _socketUtils.initSocket(args.sender_id, args.user.user_id);
 await _socketUtils.connect();

 _socketUtils.socket.emit("message", [message.toJson()]);
}

SokcetUtils工具

class {
  var user_id;
  var recid;
  initSocket(String user_id, String reciever_id) async {
    user_id = user_id;
    recid = reciever_id;
    this._fromUser = user_id;
    await _init();
  }

 _init() async {     
   socket = IO.io(
    deployment ? _serverIP : SERVER_CHAT,
    IO.OptionBuilder()
        .enableForceNew()
        .setTransports([
          'websocket'
        ]) // for Flutter or Dart VM// disable auto-connection//
        .setQuery({
          "info": _fromUser,
        })
        .setPath(deployment ? "/chat" : "/socket.io")
        .disableAutoConnect() 
        .build());
  }
}

nginx配置如下。

server {
        location / {
                proxy_pass http://localhost:3000/;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade' ;
                proxy_set_header Host Shost;
                proxy_cache_bypass $http_upgrade;
        }
        location /chat/ {
                proxy_pass http://localhost:3001/socket.io/;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade' ;
                proxy_set_header Host Shost;
                proxy_cache_bypass $http_upgrade;
        }
    server_name api.axn.in; # managed by Certbot
    proxy_read_timeout 600s; # added for user content upload
    client_max_body_size 200m; # added for user content upload
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/api.axn.in/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/api.axn.in/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
    if ($host = api.axn.in) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
    server_name api.anx.in;
    listen 80;
    return 404; # managed by Certbot
}

当我与本地套接字服务器连接时,同样的代码工作得很好。

nzkunb0c

nzkunb0c1#

你好,

upstream socket_nodes {
 ip_hash;
 server 127.0.0.1:8001;
}

server {
  server_name yourDomain.eu www.yourDomain.eu;
  access_log  /var/log/nginx/localhost.access.log;
  root /var/www/yourDomain;
  index  index.html index.htm;
  location / {
            try_files $uri $uri/ =404;
             }
  location /socket.io {
  proxy_pass http://socket_nodes;
  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection 'upgrade';
  proxy_cache_bypass $http_upgrade;
                      }

listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/yourDomain/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/yourDomain/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

不要忘记允许端口

相关问题