【问题标题】:Retreive socket id from ngx-socket-io从 ngx-socket-io 检索套接字 id
【发布时间】:2022-09-25 02:05:11
【问题描述】:

我最近开始使用ngx-socket-io,我遇到了一些困难,因为我想检索套接字 ID.有谁知道如何做到这一点? (我使用的是服务初始化而不是应用程序模块中的那个) 先感谢您。

    标签: angular ionic-framework socket.io ngx-socket-io


    【解决方案1】:

    您可以在套接字实例中找到它:socket.io Socket.id。
    但是,在使用它之前,您必须等待套接字初始化。

    @Injectable({ providedIn: 'root' })
    export class SocketService {
      socketId;
    
      constructor(private socket: Socket) {
        socket.on('connect', () => this.socketId = this.socket.ioSocket.id);
      }
    }

    【讨论】: