Package io.undertow.server.handlers.sse
Class ServerSentEventConnection
- java.lang.Object
-
- io.undertow.server.handlers.sse.ServerSentEventConnection
-
- All Implemented Interfaces:
Attachable,Closeable,AutoCloseable,Channel
public class ServerSentEventConnection extends Object implements Channel, Attachable
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 Modifier and Type Class Description static interfaceServerSentEventConnection.EventCallback
-
Constructor Summary
Constructors Constructor Description ServerSentEventConnection(HttpServerExchange exchange, org.xnio.channels.StreamSinkChannel sink)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddCloseTask(org.xnio.ChannelListener<ServerSentEventConnection> listener)Adds a listener that will be invoked when the channel is closed<T> voidaddToAttachmentList(AttachmentKey<AttachmentList<T>> key, T value)Add a value to a list-typed attachment key.voidclose()AccountgetAccount()<T> TgetAttachment(AttachmentKey<T> key)Get an attachment value.<T> List<T>getAttachmentList(AttachmentKey<? extends List<T>> key)Gets a list attachment value.longgetKeepAliveTime()StringgetParameter(String name)PrincipalgetPrincipal()Map<String,Object>getProperties()Map<String,Deque<String>>getQueryParameters()StringgetQueryString()HeaderMapgetRequestHeaders()StringgetRequestURI()HeaderMapgetResponseHeaders()booleanisOpen()<T> TputAttachment(AttachmentKey<T> key, T value)Set an attachment value.<T> TremoveAttachment(AttachmentKey<T> key)Remove an attachment, returning its previous value.voidsend(String data)Sends an event to the remote clientvoidsend(String data, ServerSentEventConnection.EventCallback callback)Sends an event to the remote clientvoidsend(String data, String event, String id, ServerSentEventConnection.EventCallback callback)Sends an event to the remote clientvoidsendRetry(long retry)Sends the 'retry' message to the client, instructing it how long to wait before attempting a reconnect.voidsendRetry(long retry, ServerSentEventConnection.EventCallback callback)Sends the 'retry' message to the client, instructing it how long to wait before attempting a reconnect.voidsetKeepAliveTime(long keepAliveTime)Sets the keep alive time in milliseconds.voidsetParameter(String name, String value)voidshutdown()execute a graceful shutdown once all data has been sent
-
-
-
Constructor Detail
-
ServerSentEventConnection
public ServerSentEventConnection(HttpServerExchange exchange, org.xnio.channels.StreamSinkChannel sink)
-
-
Method Detail
-
addCloseTask
public void addCloseTask(org.xnio.ChannelListener<ServerSentEventConnection> listener)
Adds a listener that will be invoked when the channel is closed- Parameters:
listener- The listener to invoke
-
getPrincipal
public Principal getPrincipal()
- Returns:
- The principal that was associated with the SSE request
-
getAccount
public Account getAccount()
- Returns:
- The account that was associated with the SSE request
-
getRequestHeaders
public HeaderMap getRequestHeaders()
- Returns:
- The request headers from the initial request that opened this connection
-
getResponseHeaders
public HeaderMap getResponseHeaders()
- Returns:
- The response headers from the initial request that opened this connection
-
getRequestURI
public String getRequestURI()
- Returns:
- The request URI from the initial request that opened this connection
-
getQueryParameters
public Map<String,Deque<String>> getQueryParameters()
- Returns:
- the query parameters
-
getQueryString
public String getQueryString()
- Returns:
- the query string
-
send
public void send(String data)
Sends an event to the remote client- Parameters:
data- The event data
-
send
public void send(String data, ServerSentEventConnection.EventCallback callback)
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
public void sendRetry(long retry, ServerSentEventConnection.EventCallback callback)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
-
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
-
close
public void close() throws IOException- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceChannel- Specified by:
closein interfaceCloseable- Throws:
IOException
-
getAttachment
public <T> T getAttachment(AttachmentKey<T> key)
Description copied from interface:AttachableGet an attachment value. If no attachment exists for this key,nullis returned.- Specified by:
getAttachmentin interfaceAttachable- Type Parameters:
T- the value type- Parameters:
key- the attachment key- Returns:
- the value, or
nullif there is none
-
getAttachmentList
public <T> List<T> getAttachmentList(AttachmentKey<? extends List<T>> key)
Description copied from interface:AttachableGets a list attachment value. If not attachment exists for this key an empty list is returned- Specified by:
getAttachmentListin interfaceAttachable- Type Parameters:
T- the value type- Parameters:
key- the attachment key- Returns:
- the value, or an empty list if there is none
-
putAttachment
public <T> T putAttachment(AttachmentKey<T> key, T value)
Description copied from interface:AttachableSet 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:
putAttachmentin interfaceAttachable- Type Parameters:
T- the value type- Parameters:
key- the attachment keyvalue- the new value- Returns:
- the old value, or
nullif there was none
-
removeAttachment
public <T> T removeAttachment(AttachmentKey<T> key)
Description copied from interface:AttachableRemove an attachment, returning its previous value.- Specified by:
removeAttachmentin interfaceAttachable- Type Parameters:
T- the value type- Parameters:
key- the attachment key- Returns:
- the old value, or
nullif there was none
-
addToAttachmentList
public <T> void addToAttachmentList(AttachmentKey<AttachmentList<T>> key, T value)
Description copied from interface:AttachableAdd a value to a list-typed attachment key. If the key is not mapped, add such a mapping.- Specified by:
addToAttachmentListin interfaceAttachable- Type Parameters:
T- the list value type- Parameters:
key- the attachment keyvalue- the value to add
-
-