根据 NestJS 文档,我已经实现了 websockets 网关并在 AppModule
中提供它。服务器正常启动,我可以通过 http 成功提供静态 Assets 。但是我根本无法运行 websockets,ws 服务器在 ws://localhost:3333
不可用,而且 afterInit
函数根本没有执行。即使不是在我定义 @SubscribeMessage
时。
网关实现为
@WebSocketGateway()
export class SocketGateway implements OnGatewayInit {
afterInit() {
console.log('Gateway initialized');
}
}
AppModule 正确提供网关
@Module({
providers: [SocketGateway]
})
export class AppModule {}
这是 Bootstrap 的实现
export async function bootstrap() {
let app = await NestFactory.create(AppModule);
await app.listen(process.env.port || 3333, () => {
console.log(`Listening at http://localhost:${port}`);
});
}
bootstrap();
我的亲属是
"socket.io-client": "^2.2.0",
"@nestjs/common": "5.5.0",
"@nestjs/core": "5.5.0",
"@nestjs/platform-socket.io": "^6.1.0",
"@nestjs/websockets": "^6.1.0"
也许你直接看到了问题。感谢您的帮助,干杯!
最佳答案
在你的项目中,nest的主要版本v5和v6混在一起。不保证不同的主要版本可以正确互操作。将所有依赖项更新为 nest v6;你可以看看migration guide有关更新的更多信息。
运行 $ npm i @nestjs/core@latest @nestjs/common@latest
当您安装新的依赖项时,请注意来自 npm 的对等依赖项警告,如下所示:
npm WARN @nestjs/websockets@6.1.0 requires a peer of @nestjs/common@^6.0.0 but none is installed.
You must install peer dependencies yourself.
关于javascript - NestJS WebSocketGateway 未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55665343/