Class ServerSentEvent
- All Implemented Interfaces:
AutoCloseable
Parse server-sent event text into event name, data, ID, and retry fields.
Chilkat.ServerSentEvent is a small helper class for parsing
Server-Sent Events formatted text. Given a multi-line event block, it
extracts the event name, data payload,
id value, and retry interval into simple
read-only properties. It is useful when an application receives event-stream
text and needs to work with the structured event fields instead of manually
parsing the raw lines.
Load event text
Use LoadEvent to parse a complete multi-line
Server-Sent Event block.
Read event name
Inspect EventName to determine the event type supplied by
the server, if an event: field is present.
Read event data
Use Data to obtain the event payload, such as JSON text from
a streaming API or real-time service.
Track event IDs
Read LastEventId when the event includes an
id: field that identifies the event or stream position.
Handle retry hints
Use Retry to read the server-provided retry interval when a
retry: field is included.
Diagnostics
Check LastErrorText if event text cannot be parsed or if a
parsing result is unexpected.
Relationship to Chilkat.WebSocket:
ServerSentEvent does not manage a WebSocket connection or parse
WebSocket frames. Instead, it can be used after Chilkat.WebSocket
receives a text message whose payload is formatted as a Server-Sent Event.
Read the WebSocket text frame, pass the event text to
LoadEvent, then use EventName, Data,
LastEventId, and Retry to process the event.
Common pattern:
Receive or otherwise obtain a complete Server-Sent Event text block, call
LoadEvent, then inspect EventName to route the
event and Data to process the payload. Use
LastEventId and Retry when the stream provides
reconnection or event-position information.
</section>
Instances hold a native Chilkat object: close them with ChilkatObject.close() (try-with-resources) when
done; an unclosed instance is freed when it becomes unreachable.
- See Also:
- Online reference
-
Constructor Summary
Constructors -
Method Summary
Methods inherited from class com.chilkatsoft.chilkat2.ChilkatObject
close, getDebugLogFilePath, getLastErrorHtml, getLastErrorText, getLastErrorXml, getLastMethodSuccess, getVerboseLogging, getVersion, isClosed, setDebugLogFilePath, setLastMethodSuccess, setVerboseLogging, toString
-
Constructor Details
-
ServerSentEvent
public ServerSentEvent()Creates a newServerSentEventobject.
-
-
Method Details
-
getData
The data for the server-side event. (If thedatafield was empty, then this will be empty.)- Returns:
- the value of the
Dataproperty (read-only)
-
getEventName
The name of the server-side event. (If theeventfield was not present, then this will be empty.)- Returns:
- the value of the
EventNameproperty (read-only)
-
getLastEventId
The content of theidfield, if present.- Returns:
- the value of the
LastEventIdproperty (read-only)
-
getRetry
public int getRetry()The integer value of theretryfield, if it exists; otherwise, 0.- Returns:
- the value of the
Retryproperty (read-only)
-
loadEvent
Loads the multi-line event text into this object. For example, theeventTextfor a Firebase event might look like this:event: put data: {"path": "/c", "data": {"foo": true, "bar": false}}- Parameters:
eventText- theeventTextargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-