Class WebSocketEventStream

  • All Implemented Interfaces:
    EventStream, AutoCloseable

    public class WebSocketEventStream
    extends AbstractEventStream
    Event stream implementation based on a ClientEndpoint WebSocket. It is used to connect the WebSocket to a ServerEndpoint and receive messages from it.
    • Constructor Detail

      • WebSocketEventStream

        public WebSocketEventStream​(URI uri,
                                    Token token,
                                    long sessionIdleTimeout,
                                    long idleTimeout,
                                    int maxMsgSize,
                                    EventListener... listeners)
                             throws SaltException
        Constructor used to create an event stream: open a websocket connection and start event processing.
        uri - salt api url
        listeners - event listeners to be added before stream initialization
        idleTimeout - idle timeout to pass to the http client config
        maxMsgSize - maximum event data size to accept
        sessionIdleTimeout - session idle timeout to pass to the http client config
        token - salt session token to use for authentication
        SaltException - in case of an error during stream initialization
    • Method Detail

      • isEventStreamClosed

        public boolean isEventStreamClosed()
        Helper method to check if the stream is able to receive Events.
        A flag indicating the Stream state.
      • close

        public void close()
                   throws IOException
        Closes this Stream; Events will not be posted to listeners after this call.
        IOException - in case of an error when closing the session
      • onOpen

        public void onOpen​(javax.websocket.Session session,
                           javax.websocket.EndpointConfig config)
                    throws IOException
        On handshake completed, get the WebSocket Session and send a message to ServerEndpoint that WebSocket is ready.
        session - The just started WebSocket Session.
        config - The EndpointConfig containing the handshake informations.
        IOException - if something goes wrong sending message to the remote peer
      • onMessage

        public void onMessage​(String partialMessage,
                              boolean last)
                       throws MessageTooBigException
        Notify listeners on each event received on the websocket and buffer partial messages.
        partialMessage - partial message received on this websocket
        last - indicate the last part of a message
        MessageTooBigException - in case the message is longer than maxMessageLength
      • onError

        public void onError​(Throwable throwable)
                     throws IOException
        On error, convert Throwable into CloseReason and close the session.
        throwable - The Throwable object received on the current error.
        IOException - in case of an error when closing the session
      • onClose

        public void onClose​(javax.websocket.Session session,
                            javax.websocket.CloseReason closeReason)
        On closing the websocket, refresh the session and notify all subscribed listeners. Upon exit from this method, all subscribed listeners will be removed.
        session - the websocket Session
        closeReason - the CloseReason for the websocket closure