Socket.IO 和 NodeJs 在 Nginx 后面(HTTPS). xhr 轮询请求被取消

huangapple go评论74阅读模式
英文:

Socket.IO and NodeJs behing Nginx ( HTTPS ). xhr polling requests canceled

问题

如果我的解释不好,请写下评论!
我遇到了一个奇怪的问题,我有一个使用NodeJs Express 5.0的socket.io服务器。
我有socket.io客户端从Windows/MacOS/Linux连接,使用任何Web浏览器都可以建立socket.io连接,没有任何问题。

但是从Android/Ios应用程序不起作用,我在nginx中没有收到任何错误,也没有在nodejs应用程序中收到任何错误。
我唯一看到的错误是在使用USB调试和chrome://inspect/#devices -> 标签(networking)中。
在标签中我看到:这个请求状态为(挂起)半秒钟,然后变为(已取消)

URL: mydomain.com/socket.io/?EIO=4&transport=polling&t=Oc7fPq6 
STATUS: canceled
TYPE: xhr
SIZE: 0 B

更新!!!! 这是来自Android应用程序的错误日志

2023-07-25 14:37:30.426 9286-9482/com.myandroidapp.app E/chromium: [ERROR:ssl_client_socket_impl.cc(978)] handshake failed; returned -1, SSL error code 1, net_error -202

服务器(Socket.IO和Express代码):

let SocketServer = require('./core/socket_server.js');
const bodyparser = require('body-parser');

const cors = require('cors');
const express = require('express');
const app = express();

const server = require('http').createServer(app);

const io = require('socket.io')(server, {
  cors: {
    origin: "*",
    methods: ["GET", "POST"],
    transports: ['websocket', 'xhr-polling'],
    credentials: false
  },
  allowEIO3: true
});

let SockServer = new SocketServer(io);

app.set('view engine', 'ejs');
app.use(bodyparser.json());

app.use('/call/:room_id/:user_id/:video_state', express.static('public'));

app.get('/call/:room_id/:user_id/:video_state', async (request, response) => {

  await response.render('video_chat', {
      room_id: request.params.room_id,
      user_id: request.params.user_id,
      video_state: Number(request.params.video_state)
      });
});

server.listen(5001, "0.0.0.0");
SockServer.ServerInitialize();

app.use(cors({
  origin: "*"
}));

Nginx配置:

events {
    worker_connections 1024;
}

http {

    server {
        listen 443 ssl;
        server_name mydomain.com www.mydomain.com;

        resolver 127.0.0.11;

        ssl_certificate /etc/certificates/cert.pem;
        ssl_certificate_key /etc/certificates/privkey.pem;

        location /socket.io/ {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            
            proxy_pass http://nodejs_container:5001;
            
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";

        }

        location /videochat {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            
            proxy_pass http://nodejs_container:5001;
            
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }

        location /users {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            
            proxy_pass http://nodejs_container:5001;
            
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }

        location ~ ^/call/([^/]+)/([^/]+)/([^/]+) {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            
            proxy_pass http://nodejs_container:5001;
            
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
    }
}

Docker-compose:

version: '3'
services:
  nodejs_container:
    build:
      context: app
      dockerfile: ../docker/NodeJs/DockerFile
    networks:
      - backend
    environment:
      - NODE_ENV=production
    volumes:
      - ./app:/home/app
    command: sh -c "npm install && npm run start:prod"

  coturn_container:
    image: coturn/coturn
    ports:
      - "3478:3478/udp"
      - "5349:5349/udp"
      - "3478:3478/tcp"
      - "5349:5349/tcp"
    depends_on:
      - nodejs_container
    volumes:
      - ./coturn/turnserver/dhp.pem:/etc/turnserver/dhp.pem
      - ./coturn/turnserver.conf:/etc/turnserver.conf
      - ./coturn/certificates:/etc/coturn/certificates
    networks:
      - backend

  nginx_container:
    image: nginx
    ports:
      - 443:443
    depends_on:
      - nodejs_container
      - coturn_container
    networks:
      - backend
    volumes:
      - ./coturn/certificates:/etc/coturn/certificates
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./certificates:/etc/certificates
    command: [nginx, '-g', 'daemon off;']

networks:
  backend:
    driver: bridge

SocketServer类构造函数的一部分。

class SocketServer{
  constructor(main_socket){
    this.ServerConnections = {
      database: undefined,
      socket_server: main_socket,
      Server_NSP: {
        Users: main_socket.of(ServerNamespaces.USERS),
        VideoChat: main_socket.of(ServerNamespaces.VIDEOCHAT)
      }
    };
  }
// ServerNamespaces.USERS = "/users"
// ServerNamespaces.VIDEOCHAT = "/videochat"

我尝试添加transports: ['websocket', 'xhr-polling', 'polling']

英文:

If my explanation is bad, please write a comment !
I encounter a strange problem, i have a socket.io Server using NodeJs Express 5.0.
I have socket.io clients that connects from Windows/MacOS/Linux using any WebBrowser, and connection of socket.io is established without any problems.

But from Android/Ios Application is not working, i don't get any error in nginx, or in nodejs app.
The only error i see is using usb-debugging and chrome://inspect/#devices -> tab (networking).
In the Tab i see: This requests as STATUS (pending) for half of a second, and after that it's (canceled)

URL: mydomain.com/socket.io/?EIO=4&transport=polling&t=Oc7fPq6 
STATUS: canceled
TYPE: xhr
SIZE: 0 B

UPDATED !!!! This is error log from Android App

2023-07-25 14:37:30.426 9286-9482/com.myandroidapp.app E/chromium: [ERROR:ssl_client_socket_impl.cc(978)] handshake failed; returned -1, SSL error code 1, net_error -202

Server (Socket.IO and Express Code):


let SocketServer = require('./core/socket_server.js');
const bodyparser = require('body-parser');

const cors = require('cors');
const express = require('express');
const app = express();

const server = require('http').createServer(app);

const io = require('socket.io')(server, {
  cors: {
    origin: "*",
    methods: ["GET", "POST"],
    transports: ['websocket', 'xhr-polling'],
    credentials: false
  },
  allowEIO3: true
});

let SockServer = new SocketServer(io);

app.set('view engine', 'ejs');
app.use(bodyparser.json());

app.use('/call/:room_id/:user_id/:video_state', express.static('public'));

app.get('/call/:room_id/:user_id/:video_state', async (request, response) => {

  await response.render('video_chat', {
      room_id: request.params.room_id,
      user_id: request.params.user_id,
      video_state: Number(request.params.video_state)
      });
});

server.listen(5001, "0.0.0.0");
SockServer.ServerInitialize();

app.use(cors({
  origin: "*"
}));

Nginx Configuration:


events {
    worker_connections 1024;
}

http {

    server {
        listen 443 ssl;
        server_name mydomain.com www.mydomain.com;

        resolver 127.0.0.11;

        ssl_certificate /etc/certificates/cert.pem;
        ssl_certificate_key /etc/certificates/privkey.pem;

        location /socket.io/ {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            
            proxy_pass http://nodejs_container:5001;
            
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";

        }

        location /videochat {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            
            proxy_pass http://nodejs_container:5001;
            
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }

        location /users {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            
            proxy_pass http://nodejs_container:5001;
            
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }

        location ~ ^/call/([^/]+)/([^/]+)/([^/]+) {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            
            proxy_pass http://nodejs_container:5001;
            
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
    }
}

Docker-compose:


version: '3'
services:
  nodejs_container:
    build:
      context: app
      dockerfile: ../docker/NodeJs/DockerFile
    networks:
      - backend
    environment:
      - NODE_ENV=production
    volumes:
      - ./app:/home/app
    command: sh -c "npm install && npm run start:prod"

  coturn_container:
    image: coturn/coturn
    ports:
      - "3478:3478/udp"
      - "5349:5349/udp"
      - "3478:3478/tcp"
      - "5349:5349/tcp"
    depends_on:
      - nodejs_container
    volumes:
      - ./coturn/turnserver/dhp.pem:/etc/turnserver/dhp.pem
      - ./coturn/turnserver.conf:/etc/turnserver.conf
      - ./coturn/certificates:/etc/coturn/certificates
    networks:
      - backend

  nginx_container:
    image: nginx
    ports:
      - 443:443
    depends_on:
      - nodejs_container
      - coturn_container
    networks:
      - backend
    volumes:
      - ./coturn/certificates:/etc/coturn/certificates
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./certificates:/etc/certificates
    command: [nginx, '-g', 'daemon off;']

networks:
  backend:
    driver: bridge

A part of SocketServer Class constructor.

class SocketServer{
  constructor(main_socket){
    this.ServerConnections = {
      database: undefined,
      socket_server: main_socket,
      Server_NSP: {
        Users: main_socket.of(ServerNamespaces.USERS),
        VideoChat: main_socket.of(ServerNamespaces.VIDEOCHAT)
      }
    };
  }
// ServerNamespaces.USERS = "/users"
// ServerNamespaces.VIDEOCHAT = "/videochat"

I tried to add transports: ['websocket', 'xhr-polling', 'polling'].

答案1

得分: 0

Oh my god,
使用以下方式成功解决了:

ssl_certificate /etc/certificates/fullchain.pem;

而不是:

ssl_certificate /etc/certificates/cert.pem;

英文:

Oh my god,
Got it worked by using this:

ssl_certificate /etc/certificates/fullchain.pem;

Instead of:

ssl_certificate /etc/certificates/cert.pem;

huangapple
  • 本文由 发表于 2023年7月24日 19:59:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76754282.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定