Package io.undertow.server.handlers.sse
Class ServerSentEventConnection
java.lang.Object
io.undertow.server.handlers.sse.ServerSentEventConnection
- All Implemented Interfaces:
Attachable
,Closeable
,AutoCloseable
,Channel
Represents the server side of a Server Sent Events connection.
The class implements Attachable, which provides access to the underlying exchanges attachments.
- Author:
- Stuart Douglas
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsConstructorDescriptionServerSentEventConnection
(HttpServerExchange exchange, org.xnio.channels.StreamSinkChannel sink) -
Method Summary
Modifier and TypeMethodDescriptionvoid
addCloseTask
(org.xnio.ChannelListener<ServerSentEventConnection> listener) Adds a listener that will be invoked when the channel is closed<T> void
addToAttachmentList
(AttachmentKey<AttachmentList<T>> key, T value) Add a value to a list-typed attachment key.void
close()
<T> T
getAttachment
(AttachmentKey<T> key) Get an attachment value.<T> List<T>
getAttachmentList
(AttachmentKey<? extends List<T>> key) Gets a list attachment value.long
getParameter
(String name) boolean
isOpen()
<T> T
putAttachment
(AttachmentKey<T> key, T value) Set an attachment value.<T> T
removeAttachment
(AttachmentKey<T> key) Remove an attachment, returning its previous value.void
Sends an event to the remote clientvoid
send
(String data, ServerSentEventConnection.EventCallback callback) Sends an event to the remote clientvoid
send
(String data, String event, String id, ServerSentEventConnection.EventCallback callback) Sends an event to the remote clientvoid
sendRetry
(long retry) Sends the 'retry' message to the client, instructing it how long to wait before attempting a reconnect.void
sendRetry
(long retry, ServerSentEventConnection.EventCallback callback) Sends the 'retry' message to the client, instructing it how long to wait before attempting a reconnect.void
setKeepAliveTime
(long keepAliveTime) Sets the keep alive time in milliseconds.void
setParameter
(String name, String value) void
shutdown()
execute a graceful shutdown once all data has been sent
-
Constructor Details
-
ServerSentEventConnection
public ServerSentEventConnection(HttpServerExchange exchange, org.xnio.channels.StreamSinkChannel sink)
-
-
Method Details
-
addCloseTask
Adds a listener that will be invoked when the channel is closed- Parameters:
listener
- The listener to invoke
-
getPrincipal
- Returns:
- The principal that was associated with the SSE request
-
getAccount
- Returns:
- The account that was associated with the SSE request
-
getRequestHeaders
- Returns:
- The request headers from the initial request that opened this connection
-
getResponseHeaders
- Returns:
- The response headers from the initial request that opened this connection
-
getRequestURI
- Returns:
- The request URI from the initial request that opened this connection
-
getQueryParameters
- Returns:
- the query parameters
-
getQueryString
- Returns:
- the query string
-
send
Sends an event to the remote client- Parameters:
data
- The event data
-
send
Sends an event to the remote client- Parameters:
data
- The event datacallback
- A callback that is notified on Success or failure
-
sendRetry
public void sendRetry(long retry) Sends the 'retry' message to the client, instructing it how long to wait before attempting a reconnect.- Parameters:
retry
- The retry time in milliseconds
-
sendRetry
Sends the 'retry' message to the client, instructing it how long to wait before attempting a reconnect.- Parameters:
retry
- The retry time in millisecondscallback
- The callback that is notified on success or failure
-
send
public void send(String data, String event, String id, ServerSentEventConnection.EventCallback callback) Sends an event to the remote client- Parameters:
data
- The event dataevent
- The event nameid
- The event IDcallback
- A callback that is notified on Success or failure
-
getParameter
-
setParameter
-
getProperties
-
getKeepAliveTime
public long getKeepAliveTime()- Returns:
- The keep alive time
-
setKeepAliveTime
public void setKeepAliveTime(long keepAliveTime) Sets the keep alive time in milliseconds. If this is larger than zero a ':' message will be sent this often (assuming there is no activity) to keep the connection alive. The spec recommends a value of 15000 (15 seconds).- Parameters:
keepAliveTime
- The time in milliseconds between keep alive messaged
-
shutdown
public void shutdown()execute a graceful shutdown once all data has been sent -
isOpen
public boolean isOpen() -
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceChannel
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
getAttachment
Description copied from interface:Attachable
Get an attachment value. If no attachment exists for this key,null
is returned.- Specified by:
getAttachment
in interfaceAttachable
- Type Parameters:
T
- the value type- Parameters:
key
- the attachment key- Returns:
- the value, or
null
if there is none
-
getAttachmentList
Description copied from interface:Attachable
Gets a list attachment value. If not attachment exists for this key an empty list is returned- Specified by:
getAttachmentList
in interfaceAttachable
- Type Parameters:
T
- the value type- Parameters:
key
- the attachment key- Returns:
- the value, or an empty list if there is none
-
putAttachment
Description copied from interface:Attachable
Set an attachment value. If an attachment for this key was already set, return the original value. If the value being set isnull
, the attachment key is removed.- Specified by:
putAttachment
in interfaceAttachable
- Type Parameters:
T
- the value type- Parameters:
key
- the attachment keyvalue
- the new value- Returns:
- the old value, or
null
if there was none
-
removeAttachment
Description copied from interface:Attachable
Remove an attachment, returning its previous value.- Specified by:
removeAttachment
in interfaceAttachable
- Type Parameters:
T
- the value type- Parameters:
key
- the attachment key- Returns:
- the old value, or
null
if there was none
-
addToAttachmentList
Description copied from interface:Attachable
Add a value to a list-typed attachment key. If the key is not mapped, add such a mapping.- Specified by:
addToAttachmentList
in interfaceAttachable
- Type Parameters:
T
- the list value type- Parameters:
key
- the attachment keyvalue
- the value to add
-