public interface ServerSentEmitter
Server-Sent message emitter.
sse("/pattern", sse -> {
sse.send("Hello Server-Sent events");
})
- Since:
- 2.5.0
- Author:
- edgar
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interface
Server-Sent event handler.static class
Keep-alive task. -
Method Summary
Modifier and TypeMethodDescriptiondefault <T> T
Get an attribute by his key.default ServerSentEmitter
Set an application attribute.void
close()
Close the ServerSent and fir the close event.Context attributes (a.k.a request attributes).Originating HTTP context.getId()
Server-Sent ID.default String
Read theLast-Event-ID
header and retrieve it.boolean
isOpen()
True if connection is open.keepAlive
(long timeInMillis) Send a comment message to the client.default ServerSentEmitter
Send a comment message to the client.default <T> T
lastEventId
(Class<T> type) Read theLast-Event-ID
header and retrieve it.void
onClose
(SneakyThrows.Runnable task) Generated when client close the connection or when explicit calls toclose()
.default ServerSentEmitter
send
(byte[] data) Send a text message to client.send
(ServerSentMessage data) Send a message to the client.default ServerSentEmitter
Send a text message to client.default ServerSentEmitter
Send a text message to client.default ServerSentEmitter
Send a message to the client and set the event type attribute.Set Server-Sent ID.
-
Method Details
-
onClose
Generated when client close the connection or when explicit calls toclose()
.- Parameters:
task
- Cleanup task.
-
getContext
Originating HTTP context. Please note this is a read-only context, so you are not allowed to modify or produces a response from it.The context let give you access to originating request (then one that was upgrade it).
- Returns:
- Read-only originating HTTP request.
-
getAttributes
Context attributes (a.k.a request attributes).- Returns:
- Context attributes.
-
attribute
Get an attribute by his key. This is just an utility method aroundgetAttributes()
. This method look first in current context and fallback to application attributes.- Type Parameters:
T
- Attribute type.- Parameters:
key
- Attribute key.- Returns:
- Attribute value.
-
attribute
Set an application attribute.- Parameters:
key
- Attribute key.value
- Attribute value.- Returns:
- This ServerSent.
-
send
Send a text message to client.- Parameters:
data
- Text Message.- Returns:
- This ServerSent.
-
send
Send a text message to client.- Parameters:
data
- Text Message.- Returns:
- This ServerSent.
-
send
Send a text message to client.- Parameters:
data
- Text Message.- Returns:
- This ServerSent.
-
send
Send a message to the client and set the event type attribute.- Parameters:
event
- Event type.data
- Message.- Returns:
- This emitter.
-
send
Send a message to the client.- Parameters:
data
- Message.- Returns:
- This emitter.
-
keepAlive
Send a comment message to the client. The comment line can be used to prevent connections from timing out; a server can send a comment periodically to keep the connection alive.- Parameters:
time
- Period of time.unit
- Time unit.- Returns:
- This emitter.
-
keepAlive
Send a comment message to the client. The comment line can be used to prevent connections from timing out; a server can send a comment periodically to keep the connection alive.- Parameters:
timeInMillis
- Period of time in millis.- Returns:
- This emitter.
-
getLastEventId
Read theLast-Event-ID
header and retrieve it. Might be null.- Returns:
- Last event ID or
null
.
-
lastEventId
Read theLast-Event-ID
header and retrieve it. Might be null.- Type Parameters:
T
- Type to convert.- Parameters:
type
- Type to convert.- Returns:
- Last event ID or
null
.
-
getId
Server-Sent ID. Defaults to UUID.- Returns:
- Server-Sent ID. Defaults to UUID.
-
setId
Set Server-Sent ID.- Parameters:
id
- Set Server-Sent ID.- Returns:
- This emitter.
-
isOpen
boolean isOpen()True if connection is open.- Returns:
- True if connection is open.
-
close
void close()Close the ServerSent and fir the close event.
-