Package com.oorian
Class OorianSseConnection
java.lang.Object
com.oorian.OorianSseConnection
- All Implemented Interfaces:
SseConnection
Manages a Server-Sent Events (SSE) connection to a client browser.
OorianSseConnection wraps an AsyncContext and provides methods for sending SSE-formatted events to the client. It handles the SSE protocol details including event formatting, data encoding, and connection management.
SSE Message Format:
event: eventType data: message content
Key Features:
- Thread-safe message sending
- Automatic data line splitting for multi-line content
- Heartbeat support via comments
- Weak reference to page to prevent memory leaks
- Connection state tracking
- Since:
- 2024
- Version:
- 1.0
- Author:
- Marvin P. Warble Jr.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionOorianSseConnection(jakarta.servlet.AsyncContext asyncContext, HtmlPage page) Constructs a new SSE connection. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the SSE connection.getPage()Gets the HtmlPage associated with this connection.booleanisOpen()Checks if the SSE connection is still open.voidsendComment(String comment) Sends an SSE comment (heartbeat/keep-alive).voidSends a data-only SSE event (no event type, uses default "message" event).voidSends an SSE event with the specified event type and data.voidSends a heartbeat comment to keep the connection alive.voidsendJsonResponse(String jsonResponse) Sends a JSON response message using the "update" event type, which the client-side JavaScript will process using the existing response handlers.
-
Constructor Details
-
OorianSseConnection
Constructs a new SSE connection.- Parameters:
asyncContext- The async context for this connection.page- The HtmlPage associated with this connection.
-
-
Method Details
-
sendEvent
Sends an SSE event with the specified event type and data.The data is formatted according to the SSE specification. Multi-line data is split into multiple data: lines.
- Parameters:
eventType- The event type (appears in "event:" field). May be null for default message event.data- The data to send (appears in "data:" field).
-
sendData
Sends a data-only SSE event (no event type, uses default "message" event).- Parameters:
data- The data to send.
-
sendComment
Sends an SSE comment (heartbeat/keep-alive).Comments are lines starting with a colon and are ignored by the EventSource API, but they keep the connection alive through proxies that might otherwise timeout.
- Parameters:
comment- The comment text.
-
sendHeartbeat
public void sendHeartbeat()Sends a heartbeat comment to keep the connection alive. -
sendJsonResponse
Sends a JSON response message using the "update" event type, which the client-side JavaScript will process using the existing response handlers.- Specified by:
sendJsonResponsein interfaceSseConnection- Parameters:
jsonResponse- The JSON response content.
-
isOpen
public boolean isOpen()Checks if the SSE connection is still open.- Specified by:
isOpenin interfaceSseConnection- Returns:
- true if the connection is open, false otherwise.
-
getPage
Gets the HtmlPage associated with this connection.- Returns:
- The associated HtmlPage, or null if it has been garbage collected.
-
close
public void close()Closes the SSE connection.Marks the connection as closed and completes the async context.
- Specified by:
closein interfaceSseConnection
-