javascript - NestJS WebSocketGateway 未初始化

标签 javascript node.js typescript websocket nestjs

根据 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/

相关文章:

reactjs - 如何设置react-router-dom的类型?

typescript 通用特化

javascript - 订阅 BehaviorSubject 时,如何避免在可观察对象中执行两次 promise ?

javascript - 根据值将数组拆分为更多数组

javascript - 过渡排序网页上显示的内容

javascript - 如何将 JSON 数据传递给 AngularJS 指令

javascript - 在 Windows 上运行 Grunt 时出错 : Fatal error: Unable to find local grunt

javascript - 无法使最基本的 Backbone.js "Hello World"示例正常工作

node.js - 简单的 Express 3.x socket.io 聊天

javascript - Soundcloud 嵌入流 URL( Node 、JSON)