根据NestJS文档,我已经实现了websockets网关并在AppModule
中提供它。服务器正常启动,我可以通过http成功提供静态资产。但是我根本无法运行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 {}
这是引导实现
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"
也许你直接看到问题。谢谢你的帮助,欢呼!
在您的项目中,嵌套的主要版本v5和v6是混合的。不保证不同的主要版本能够正确地进行互操作。将所有依赖项更新为nest v6;您可以查看migration guide以获取有关更新的其他信息。
运行$ npm i @nestjs/core@latest @nestjs/common@latest
当您安装新的依赖项时,请注意来自npm的对等依赖项警告,如下所示:
npm WARN @nestjs/[email protected] requires a peer of @nestjs/common@^6.0.0 but none is installed.
You must install peer dependencies yourself.