Class 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
    • 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
      • 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 milliseconds
        callback - 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 data
        event - The event name
        id - The event ID
        callback - A callback that is notified on Success or failure
      • getParameter

        public String getParameter​(String name)
      • setParameter

        public void setParameter​(String name,
                                 String value)
      • 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()
        Specified by:
        isOpen in interface Channel
      • getAttachment

        public <T> T getAttachment​(AttachmentKey<T> key)
        Description copied from interface: Attachable
        Get an attachment value. If no attachment exists for this key, null is returned.
        Specified by:
        getAttachment in interface Attachable
        Type Parameters:
        T - the value type
        Parameters:
        key - the attachment key
        Returns:
        the value, or null if there is none
      • getAttachmentList

        public <T> List<T> getAttachmentList​(AttachmentKey<? extends List<T>> key)
        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 interface Attachable
        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: Attachable
        Set an attachment value. If an attachment for this key was already set, return the original value. If the value being set is null, the attachment key is removed.
        Specified by:
        putAttachment in interface Attachable
        Type Parameters:
        T - the value type
        Parameters:
        key - the attachment key
        value - the new value
        Returns:
        the old value, or null if there was none
      • removeAttachment

        public <T> T removeAttachment​(AttachmentKey<T> key)
        Description copied from interface: Attachable
        Remove an attachment, returning its previous value.
        Specified by:
        removeAttachment in interface Attachable
        Type Parameters:
        T - the value type
        Parameters:
        key - the attachment key
        Returns:
        the old value, or null if there was none
      • addToAttachmentList

        public <T> void addToAttachmentList​(AttachmentKey<AttachmentList<T>> key,
                                            T value)
        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 interface Attachable
        Type Parameters:
        T - the list value type
        Parameters:
        key - the attachment key
        value - the value to add