【发布时间】:2021-02-18 21:50:41
【问题描述】:
我在 nodejs 中成功运行 websockets - 作为使用 wshttps://www.npmjs.com/package/ws 的命令行 websockets 服务器
我无法让 nestjs websockets 网关正常工作。我努力了: https://docs.nestjs.com/websockets/gateways - 以及 github 上的工作示例; https://github.com/nestjs/nest/tree/master/sample/02-gateways 。它运行 - 但我无法连接到它。
如果我在 nodejs 中使用 ws 客户端进行连接,我会得到:
Error: socket hang up
我也试过https://chrome.google.com/webstore/detail/simple-websocket-client/pfdhoblngboilpfeibdedpjgfnlcodoo?hl=en 连接。我可以用它连接到其他 websocket 服务器。
我也试过这个https://techformist.com/websockets-app-nestjs/,它说
[Nest] 29036 - 06/11/2020, 6:42:02 pm [NestFactory] Starting Nest application...
[Nest] 29036 - 06/11/2020, 6:42:02 pm [InstanceLoader] AppModule dependencies initialized +11ms
[Nest] 29036 - 06/11/2020, 6:42:03 pm [AppGateway] Initialized
[Nest] 29036 - 06/11/2020, 6:42:03 pm [NestApplication] Nest application successfully started +3ms
看起来它正在工作 - 但我仍然无法连接。
我做错了什么?
更新: 下面是我的网关代码(来自https://techformist.com/websockets-app-nestjs/)
import {
SubscribeMessage,
WebSocketGateway,
OnGatewayInit,
} from "@nestjs/websockets";
import { Logger } from "@nestjs/common";
@WebSocketGateway(3026)
export class AppGateway implements OnGatewayInit {
private logger: Logger = new Logger("AppGateway");
afterInit (server: any) {
// throw new Error('Method not implemented.'); - comment this
this.logger.log("Initialized");
}
// export class AppGateway {
@SubscribeMessage("message")
handleMessage (client: any, payload: any): string {
return "Hello world!";
}
}
【问题讨论】:
-
显示你的 Nest js ws 网关代码...
-
@ZainUrRehman 我已经更新了.. 我只是使用其中一个示例。然后我尝试连接到:ws://localhost:3026
标签: nestjs