Record Class SseEvent

java.lang.Object
java.lang.Record
cloud.opencode.base.web.sse.SseEvent
Record Components:
id - the event ID (optional) - 事件 ID(可选)
event - the event type (default: "message") - 事件类型(默认:"message")
data - the event data - 事件数据
retry - the reconnection time in milliseconds (optional) - 重连时间(毫秒,可选)

public record SseEvent(String id, String event, String data, Long retry) extends Record
SSE Event - Represents a Server-Sent Event SSE 事件 - 表示服务器发送的事件

Server-Sent Events (SSE) is a technology for pushing events from server to client over HTTP.

服务器发送事件(SSE)是一种通过 HTTP 从服务器向客户端推送事件的技术。

Features | 主要功能:

  • Immutable SSE event record - 不可变SSE事件记录
  • Builder pattern for event construction - 构建器模式
  • Optional fields (id, retry) - 可选字段(id、retry)

Usage Examples | 使用示例:

SseEvent event = SseEvent.of("Hello, World!");
SseEvent typed = SseEvent.of("update", jsonData);
SseEvent full = SseEvent.builder()
    .id("123").event("update").data(json).retry(3000).build();

Security | 安全性:

  • Thread-safe: Yes (immutable record) - 是(不可变记录)
  • Null-safe: Partial (data can be null) - 部分(数据可以为null)
Since:
JDK 25, opencode-base-web V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Field Details

  • Constructor Details

    • SseEvent

      public SseEvent(String id, String event, String data, Long retry)
      Compact constructor: validates that id and event do not contain newline characters to prevent SSE event injection. 紧凑构造器:校验 id 和 event 不包含换行符以防止 SSE 事件注入。
  • Method Details

    • of

      public static SseEvent of(String data)
    • of

      public static SseEvent of(String event, String data)
    • of

      public static SseEvent of(String id, String event, String data)
    • builder

      public static SseEvent.Builder builder()
    • eventOrDefault

      public String eventOrDefault()
    • getId

      public Optional<String> getId()
    • getRetry

      public Optional<Long> getRetry()
    • isMessage

      public boolean isMessage()
    • hasData

      public boolean hasData()
    • hasId

      public boolean hasId()
    • toDebugString

      public String toDebugString()
    • toString

      public String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • id

      public String id()
      Returns the value of the id record component.
      Returns:
      the value of the id record component
    • event

      public String event()
      Returns the value of the event record component.
      Returns:
      the value of the event record component
    • data

      public String data()
      Returns the value of the data record component.
      Returns:
      the value of the data record component
    • retry

      public Long retry()
      Returns the value of the retry record component.
      Returns:
      the value of the retry record component