Skip to content

Commit

Permalink
feat(crux): add unsubscribe all to wsclient
Browse files Browse the repository at this point in the history
  • Loading branch information
robot9706 committed Sep 15, 2023
1 parent 6112c29 commit 8834e98
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
7 changes: 5 additions & 2 deletions web/crux/src/app/token/jwt-auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default class JwtAuthGuard extends AuthGuard('jwt') {
return await this.canActivateHttp(context, context.switchToHttp().getRequest(), strategy)
}
if (type === 'ws') {
return this.canActivateWs(context)
return await this.canActivateWs(context)
}

this.logger.error(`Invalid context ${type}`)
Expand Down Expand Up @@ -107,7 +107,7 @@ export default class JwtAuthGuard extends AuthGuard('jwt') {
req.headers.authorization = `Bearer ${token}`
}

private canActivateWs(context: ExecutionContext): boolean {
private async canActivateWs(context: ExecutionContext): Promise<boolean> {
const client: WsClient = context.switchToWs().getClient()
if (client.disconnecting) {
// NOTE(@robot9706): When a client is disconnecting disallow any handlers
Expand All @@ -123,6 +123,9 @@ export default class JwtAuthGuard extends AuthGuard('jwt') {

if (!sessionExpiresAt || sessionExpiresAt <= now) {
this.logger.debug('WebSocket session expired.')

await client.unsubscribeAll()

client.close()
throw new CruxUnauthorizedException()
}
Expand Down
1 change: 1 addition & 0 deletions web/crux/src/websockets/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export type WsClient = WebSocket & {
connectionRequest: AuthorizedHttpRequest
sendWsMessage: WsSendClientMessage
subscriptions: Map<string, WsSubscription>
unsubscribeAll: () => Promise<void>

// NOTE(@robot9706): Used by jwt-auth.guard WS strategy to be able to call
// unsubscribe methods when the session is invalid (for cleanup)
Expand Down
7 changes: 6 additions & 1 deletion web/crux/src/websockets/dyo.ws.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ export default class DyoWsAdapter extends AbstractWsAdapter {
client.send(JSON.stringify(msg))
}
client.on(CLOSE_EVENT, () => this.onClientDisconnect(client))
client.unsubscribeAll = () => this.onClientDisconnect(client)

client.setup = new WsClientSetup(client, client.token, () => this.bindClientMessageHandlers(client))
client.setup.start()
Expand All @@ -299,7 +300,11 @@ export default class DyoWsAdapter extends AbstractWsAdapter {
this.logger.log(`Connected ${client.token} clients: ${this.server?.clients?.size}`)
}

private async onClientDisconnect(client: WsClient) {
private async onClientDisconnect(client: WsClient): Promise<void> {
if (client.disconnecting) {
return
}

client.disconnecting = true

this.logger.log(`Disconnected ${client.token} clients: ${this.server?.clients?.size}`)
Expand Down

0 comments on commit 8834e98

Please sign in to comment.