从套接字列表中获取特定套接字 - NestJs

问题描述 投票:0回答:1

我需要使用 Nests 从套接字列表中获取特定的套接字,这是我在下面的代码中使用的方式,但我不想将所有套接字拉入列表中,然后在其中搜索套接字,有什么办法吗?直接搜索并获取所需的套接字而不是使用“fetchSockets()”方法?

 @WebSocketServer()
  server: Server = new Server<ServerToClientEvents, ClientToServerEvents>();

@SubscribeMessage('event')
  async handelInitial(@MessageBody() data: any): Promise<void> {
 
    const array = await this.server.sockets.fetchSockets();
    const sockest = array.find((e)=>e.id==='required socket id')

}
node.js socket.io nestjs backend real-time
1个回答
0
投票

您可以按照此处所述使用

const sockets = await io.in(theSocketId).fetchSockets();
https://socket.io/docs/v4/server-instance/#fetchsockets

在您的 Nest 应用程序中,这看起来像这样:

await this.server.in('socket id').fetchSockets()

© www.soinside.com 2019 - 2024. All rights reserved.