插座io nestjs+飞镖+ Linux系统连接故障
我目前使用 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-alpine
或 node: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题不是在Docker或插座本身。它来自nginx配置。
我将此代码添加到我的nginx conf,
然后错误消失了。
The problem was not at docker or socket itself. It was from nginx configure.
I added this code to my nginx conf
then error gone.