- All Superinterfaces:
AutoCloseable,Closeable
A client for Server-Sent Events (SSE). This class handles the setup of the SSE connection,
sending events and comments to the client, and managing the lifecycle of the connection. It
ensures proper headers are set and provides methods for sending various types of data.
This class implements Closeable to allow for proper resource management. The
connection is automatically closed if the client disconnects or if an error occurs during event
emission.
-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Close the SseClient and release keepAlive block if anyctx()Return the request Context.static ExchangeHandlerReturn an SseClient handler.voidBy blocking the SSE connection, you can share this client outside the handler to notify it from other sources.voidAdd a callback that will be called either when connection is closed throughclose(), or when theEmitteris detected as closed.voidsendComment(String comment) Attempt to send a comment.voidCallssendEvent(String, Object, String)with event set to "message"voidCallssendEvent(String, Object, String)with id set to nullvoidAttempt to send an event.booleanReturns true ifclose()has been called.
-
Method Details
-
handler
Return an SseClient handler. -
close
void close()Close the SseClient and release keepAlive block if any- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
ctx
Context ctx()Return the request Context.- Returns:
- the request
-
keepAlive
void keepAlive()By blocking the SSE connection, you can share this client outside the handler to notify it from other sources. Keep in mind that this function will block the handler until the SSE client is released by another thread. -
onClose
Add a callback that will be called either when connection is closed throughclose(), or when theEmitteris detected as closed.- Parameters:
task- task to run
-
sendComment
Attempt to send a comment. If theEmitterfails to emit (remote client has disconnected), theclose()function will be called instead. -
sendEvent
CallssendEvent(String, Object, String)with event set to "message" -
sendEvent
CallssendEvent(String, Object, String)with id set to null -
sendEvent
Attempt to send an event. If theEmitterfails to emit (remote client has disconnected), theclose()function will be called instead.- Parameters:
event- The name of the event.data- The data to send in the event. This can be a String, an InputStream, or any object that can be serialized to JSON using the configuredJsonService.id- The ID of the event.
-
terminated
boolean terminated()Returns true ifclose()has been called. This can either be by the user, or by Jex upon detecting that theEmitteris closed.
-