How to detect when the WebSockets session is closed #4894
Stexxe
started this conversation in
Knowledge base
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
This guide shows you how to run code when a WebSockets session closes, on both the client and server sides.
Client-side
When using an
HttpClient
with theWebSockets
plugin:You have two main ways to detect a session close:
Using
try/finally
block: Code inside thefinally
block runs just before the session closes.Using
outgoing.invokeOnClose
: Register a handler to be called when the outgoing channel closes, which happens after the Close frame is sent.This
invokeOnClose
method also works when usingwebSocketSession
directly:Server-side
Similarly, there are two ways to detect a WebSockets session close on the server:
Using
try/finally
block: Thefinally
block's code is executed when the session closes.Using
coroutineContext[Job]?.invokeOnCompletion
: Register a handler that runs when the session's underlying Job completes, which occurs after the Close frame is sent.Beta Was this translation helpful? Give feedback.
All reactions