Socket.IO和NodeJs behing Nginx(HTTPS),xhr轮询请求已取消

kknvjkwl  于 2023-08-03  发布在  Nginx
关注(0)|答案(1)|浏览(143)

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

但从Android/iOS应用程序不工作,我没有得到任何错误在nginx,或在nodejs应用程序.我看到的唯一错误是使用usb-debugging和chrome://inspect/#devices -> tab(networking)。在选项卡中,我看到:这个请求作为STATUS(挂起)持续半秒,之后它就(取消)了

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

字符串

已更新!!!!这是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

服务器(Socket.IO和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配置:

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"


我试着添加运输:['websocket','xhr-polling','polling'].

pinkon5k

pinkon5k1#

我的天啊,我用这个方法成功了:

ssl_certificate /etc/certificates/fullchain.pem;

字符串
而不是:

ssl_certificate /etc/certificates/cert.pem;

相关问题