插座io nestjs+飞镖+ Linux系统连接故障

发布于 2025-01-20 06:41:55 字数 2015 浏览 1 评论 0原文

我目前使用 NestJS 作为 SocketIO 服务器,使用 Flutter 作为客户端。

在本地 mac 机器上开发 NestJS 服务器,它与 dart socket-io 客户端配合良好,但每当我尝试部署我的 NestJS 服务器代码(docker 或本机 ubuntu)时,连接总是失败。 (我也厌倦了多次删除node_modules并重新安装......)

我想知道为什么会发生这种情况以及如何解决这个问题。首先我认为这是套接字版本问题的问题,但在这种情况下可能不是。


服务器 NestJs 版本 8.2.5,本地计算机 NestJS 版本 8.2.5

部署服务器:18.04.6 LTS,部署 Docker 映像:node:16-alpinenode:14-alpine

服务器端 package.json

    "@nestjs/platform-express": "^8.4.4",
    "@nestjs/platform-socket.io": "^8.4.4",
    "@nestjs/platform-ws": "^8.4.4",
    "socket.io-redis": "^5.4.0",

客户端 Dart pubspec

  socket_io_client: ^2.0.0-beta.4-nullsafety.0

客户端套接字部分

    socket = io('http://192.168.0.26:9000', <String, dynamic>{
      'transports': ['websocket'],
      'autoConnect': true,
    });
    socket.connect();
    socket.on('connect', (_) => print('connect: ${socket.id}'));

服务器端 Redis 适配器

import { IoAdapter } from '@nestjs/platform-socket.io';
import { ServerOptions } from 'socket.io';
import * as redisIoAdapter from 'socket.io-redis';

export class RedisIoAdapter extends IoAdapter {
  createIOServer(port: number, options?: ServerOptions): any {
    const server = super.createIOServer(port, options);
    const redisAdapter = redisIoAdapter({
      host: '127.0.0.1',  // host.docker.internal
      port: 6379,
    });
    server.adapter(redisAdapter);
    return server;
  }
}


async function bootstrap(port: number) {
  const app = await NestFactory.create(AppModule);
  app.useWebSocketAdapter(new RedisIoAdapter(app));
  app.use(bodyParser.json({ limit: '50mb' }));
  app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
  await app.listen(port);
}

for (const single of [9000, 9001]) {
  bootstrap(single).then();
}

...

I currently using NestJS for SocketIO server and Flutter for client.

Developing NestJS Server on local mac machine, it works well with dart socket-io client but whenever I tried to deploy my NestJS server code(docker or native ubuntu), connection always fails. (also I tired many times removing node_modules and reinstall...)

I wonder why this happens and how to solve this problem. first I thought it was a problem whit socket's version problem but it might not in this case.


Server NestJs version 8.2.5, Local Machine NestJS version 8.2.5

Deployment Server: 18.04.6 LTS, Deployment Docker Image: node:16-alpine or node:14-alpine

Server side package.json

    "@nestjs/platform-express": "^8.4.4",
    "@nestjs/platform-socket.io": "^8.4.4",
    "@nestjs/platform-ws": "^8.4.4",
    "socket.io-redis": "^5.4.0",

Client Dart pubspec

  socket_io_client: ^2.0.0-beta.4-nullsafety.0

Client Socket Part

    socket = io('http://192.168.0.26:9000', <String, dynamic>{
      'transports': ['websocket'],
      'autoConnect': true,
    });
    socket.connect();
    socket.on('connect', (_) => print('connect: ${socket.id}'));

Server Side Redis Adapter

import { IoAdapter } from '@nestjs/platform-socket.io';
import { ServerOptions } from 'socket.io';
import * as redisIoAdapter from 'socket.io-redis';

export class RedisIoAdapter extends IoAdapter {
  createIOServer(port: number, options?: ServerOptions): any {
    const server = super.createIOServer(port, options);
    const redisAdapter = redisIoAdapter({
      host: '127.0.0.1',  // host.docker.internal
      port: 6379,
    });
    server.adapter(redisAdapter);
    return server;
  }
}


async function bootstrap(port: number) {
  const app = await NestFactory.create(AppModule);
  app.useWebSocketAdapter(new RedisIoAdapter(app));
  app.use(bodyParser.json({ limit: '50mb' }));
  app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
  await app.listen(port);
}

for (const single of [9000, 9001]) {
  bootstrap(single).then();
}

...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江湖彼岸 2025-01-27 06:41:55

问题不是在Docker或插座本身。它来自nginx配置。

我将此代码添加到我的nginx conf,

location /socket.io {
        proxy_pass http://backend;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        add_header  Front-End-Https   on;
}

然后错误消失了。

The problem was not at docker or socket itself. It was from nginx configure.

I added this code to my nginx conf

location /socket.io {
        proxy_pass http://backend;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        add_header  Front-End-Https   on;
}

then error gone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文