Package com.oorian

Class OorianSseConnection

java.lang.Object
com.oorian.OorianSseConnection
All Implemented Interfaces:
SseConnection

public class OorianSseConnection extends Object implements 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

    Constructors
    Constructor
    Description
    OorianSseConnection(jakarta.servlet.AsyncContext asyncContext, HtmlPage page)
    Constructs a new SSE connection.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes the SSE connection.
    Gets the HtmlPage associated with this connection.
    boolean
    Checks if the SSE connection is still open.
    void
    Sends an SSE comment (heartbeat/keep-alive).
    void
    Sends a data-only SSE event (no event type, uses default "message" event).
    void
    sendEvent(String eventType, String data)
    Sends an SSE event with the specified event type and data.
    void
    Sends a heartbeat comment to keep the connection alive.
    void
    sendJsonResponse(String jsonResponse)
    Sends a JSON response message using the "update" event type, which the client-side JavaScript will process using the existing response handlers.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • OorianSseConnection

      public OorianSseConnection(jakarta.servlet.AsyncContext asyncContext, HtmlPage page)
      Constructs a new SSE connection.
      Parameters:
      asyncContext - The async context for this connection.
      page - The HtmlPage associated with this connection.
  • Method Details

    • sendEvent

      public void sendEvent(String eventType, String data)
      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

      public void sendData(String data)
      Sends a data-only SSE event (no event type, uses default "message" event).
      Parameters:
      data - The data to send.
    • sendComment

      public void sendComment(String comment)
      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

      public void sendJsonResponse(String jsonResponse)
      Sends a JSON response message using the "update" event type, which the client-side JavaScript will process using the existing response handlers.
      Specified by:
      sendJsonResponse in interface SseConnection
      Parameters:
      jsonResponse - The JSON response content.
    • isOpen

      public boolean isOpen()
      Checks if the SSE connection is still open.
      Specified by:
      isOpen in interface SseConnection
      Returns:
      true if the connection is open, false otherwise.
    • getPage

      public HtmlPage 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:
      close in interface SseConnection