Interface ServerSentEventHandler
public interface ServerSentEventHandler
-
Method Summary
Modifier and TypeMethodDescriptionstatic ServerSentEventHandlercreate()static ServerSentEventHandlercreate(Consumer<ServerSentEventHandler> onActive) static ServerSentEventHandlercreate(Consumer<ServerSentEventHandler> onActive, Consumer<ServerSentEventHandler> onInactive) booleanReturns true if there are active connections to the SSE endpoint.voidonActive()Called when the first client connects to the SSE endpoint.voidCalled when the client connects to the SSE endpoint.voidonDisconnect(Request request) Called when the client disconnects from the SSE endpoint.voidCalled when the last client disconnects from the SSE endpoint.default voidsendMessage(String message) Sends a message to all clients, with the message appended to the `data` field of the Server-Sent Event.voidsendMessage(Function<Request, ServerSentEventMessage> messageGenerator) Sends a message to all clients, with the message being generated by the provided function.default voidsendMessage(ServerSentEventMessage message) Sends a message to all clients, with each field of theServerSentEventMessagebeing set in the message, as long as the field is not null.
-
Method Details
-
create
-
create
-
create
static ServerSentEventHandler create(Consumer<ServerSentEventHandler> onActive, Consumer<ServerSentEventHandler> onInactive) -
onConnect
Called when the client connects to the SSE endpoint.- Parameters:
request- The initial request from the client.
-
onDisconnect
Called when the client disconnects from the SSE endpoint.- Parameters:
request- The initial request from the client.
-
sendMessage
Sends a message to all clients, with the message appended to the `data` field of the Server-Sent Event.- Parameters:
message- The message to send.
-
sendMessage
Sends a message to all clients, with each field of theServerSentEventMessagebeing set in the message, as long as the field is not null.- Parameters:
message- The message to send.
-
sendMessage
Sends a message to all clients, with the message being generated by the provided function. This allows for the message to be tailored per client, if required (e.g. by using path parameters or query parameters).- Parameters:
messageGenerator- A function that takes aServerSentEventRequestand returns aServerSentEventMessage, that will then be sent to the client represented by the given request.
-
hasActiveConnections
boolean hasActiveConnections()Returns true if there are active connections to the SSE endpoint.- Returns:
- true if there are active connections to the SSE endpoint.
-
onActive
void onActive()Called when the first client connects to the SSE endpoint. This is a good point to start any background threads that will generate data, which can then be sent by calling the sendMessage methods. -
onInactive
void onInactive()Called when the last client disconnects from the SSE endpoint. This is a good point to stop any background threads that are generating data.
-