socket-controllers icon indicating copy to clipboard operation
socket-controllers copied to clipboard

feature: Support for @OnDisconnecting decorator

Open patrickshox opened this issue 1 year ago • 0 comments

Description

I need to be able to get the socket's rooms when the user disconnects. However, the "disconnect" event fires after disconnect so we can't get the rooms from the socket. For this purpose, socket.io now supports the "disconnecting" event which fires before disconnection. However, socket-controllers doesn't have a decorator for @OnDisconnecting, so I have to just use regular socket.io code...

io.on("connection", (socket) => {
    socket.on("disconnecting", (reason) => {
      for (const room of socket.rooms) {
        ...
      }
    });
  }); 

Proposed solution

I'd like to be able to write something like:

@OnDisconnecting()
  public async removeSocketFromRooms(@SocketIO() io: Server,  @ConnectedSocket() socket: Socket) {
    for (const room of socket.rooms) {
      ...
    }
  }

patrickshox avatar Aug 21 '22 22:08 patrickshox