-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
feat(websockets): add disconnect reason parameter to OnGatewayDisconnect #15451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,7 +140,9 @@ export class WebSocketsController { | |
|
||
const disconnectHook = adapter.bindClientDisconnect; | ||
disconnectHook && | ||
disconnectHook.call(adapter, client, () => disconnect.next(client)); | ||
disconnectHook.call(adapter, client, (reason?: string) => | ||
disconnect.next({ client, reason }), | ||
); | ||
}; | ||
} | ||
|
||
|
@@ -162,9 +164,15 @@ export class WebSocketsController { | |
|
||
public subscribeDisconnectEvent(instance: NestGateway, event: Subject<any>) { | ||
if (instance.handleDisconnect) { | ||
event | ||
.pipe(distinctUntilChanged()) | ||
.subscribe(instance.handleDisconnect.bind(instance)); | ||
event.pipe(distinctUntilChanged()).subscribe((data: any) => { | ||
|
||
// Handle both old format (just client) and new format ({ client, reason }) | ||
if (data && typeof data === 'object' && 'client' in data) { | ||
instance.handleDisconnect!(data.client, data.reason); | ||
} else { | ||
// Backward compatibility: if it's just the client | ||
instance.handleDisconnect!(data); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why(?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change prioritizes global exception filters (i.e., filters with empty exceptionMetatypes) over more specific ones. Previously, global filters that were intended to catch all exceptions could be skipped because they were processed after specific filters.
The updated logic ensures that if a filter is designed to handle all exception types (as indicated by an empty exceptionMetatypes array), it is invoked first, preserving the intended filter hierarchy and behavior.
Please let me know if I’ve misunderstood any part of this change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the expected behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the clarification! I've removed the exception filter changes
in commit 6cf7ad5 since the current behavior is indeed the intended
behavior.