Class WebSocketEventStream
java.lang.Object
com.suse.salt.netapi.event.AbstractEventStream
com.suse.salt.netapi.event.WebSocketEventStream
- All Implemented Interfaces:
EventStream, AutoCloseable
Event stream implementation based on a
ClientEndpoint WebSocket.
It is used to connect the WebSocket to a ServerEndpoint
and receive messages from it.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final intDefault message buffer size in characters.private final intMaximum message length in charactersprivate final StringBuilderBuffer for partial messages.private javax.websocket.SessionThe WebSocketSession.private final javax.websocket.WebSocketContainerTheWebSocketContainerobject for a @ClientEndpoint implementation. -
Constructor Summary
ConstructorsConstructorDescriptionWebSocketEventStream(URI uri, Token token, long sessionIdleTimeout, long idleTimeout, int maxMsgSize, EventListener... listeners) Constructor used to create an event stream: open a websocket connection and start event processing. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this Stream; Events will not be posted to listeners after this call.private voidclose(javax.websocket.CloseReason closeReason) Close the WebSocketSessionwith a given close reason.private voidinitializeStream(URI uri, Token token, long sessionIdleTimeout, long idleTimeout) Connect the WebSocket to the server pointing to /ws/{token} to receive events.booleanHelper method to check if the stream is able to receive Events.voidonClose(javax.websocket.Session session, javax.websocket.CloseReason closeReason) On closing the websocket, refresh the session and notify all subscribed listeners.voidOn error, convertThrowableintoCloseReasonand close the session.voidNotify listeners on each event received on the websocket and buffer partial messages.voidonOpen(javax.websocket.Session session, javax.websocket.EndpointConfig config) On handshake completed, get the WebSocket Session and send a message to ServerEndpoint that WebSocket is ready.Methods inherited from class AbstractEventStream
addEventListener, clearListeners, getListenerCount, notifyListeners, removeEventListener
-
Field Details
-
defaultBufferSize
private final int defaultBufferSizeDefault message buffer size in characters.- See Also:
-
maxMessageLength
private final int maxMessageLengthMaximum message length in characters -
messageBuffer
Buffer for partial messages. -
websocketContainer
private final javax.websocket.WebSocketContainer websocketContainerTheWebSocketContainerobject for a @ClientEndpoint implementation. -
session
private javax.websocket.Session sessionThe WebSocketSession.
-
-
Constructor Details
-
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.- Parameters:
uri- salt api urltoken- salt session token to use for authenticationsessionIdleTimeout- session idle timeout to pass to the http client configidleTimeout- idle timeout to pass to the http client configmaxMsgSize- maximum event data size to acceptlisteners- event listeners to be added before stream initialization- Throws:
SaltException- in case of an error during stream initialization
-
-
Method Details
-
initializeStream
private void initializeStream(URI uri, Token token, long sessionIdleTimeout, long idleTimeout) throws SaltException Connect the WebSocket to the server pointing to /ws/{token} to receive events.- Throws:
SaltException- in case of an error during stream initialization
-
isEventStreamClosed
public boolean isEventStreamClosed()Helper method to check if the stream is able to receive Events.- Returns:
- A flag indicating the Stream state.
-
close
Closes this Stream; Events will not be posted to listeners after this call.- Throws:
IOException- in case of an error when closing the session
-
close
Close the WebSocketSessionwith a given close reason.- Parameters:
closeReason- the reason for the websocket closure- Throws:
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. http://docs.saltstack.com/en/latest/ref/netapi/all/salt.netapi.rest_cherrypy.html#ws- Parameters:
session- The just started WebSocketSession.config- TheEndpointConfigcontaining the handshake informations.- Throws:
IOException- if something goes wrong sending message to the remote peer
-
onMessage
Notify listeners on each event received on the websocket and buffer partial messages.- Parameters:
partialMessage- partial message received on this websocketlast- indicate the last part of a message- Throws:
MessageTooBigException- in case the message is longer than maxMessageLength
-
onError
On error, convertThrowableintoCloseReasonand close the session.- Parameters:
throwable- The Throwable object received on the current error.- Throws:
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.- Parameters:
session- the websocketSessioncloseReason- theCloseReasonfor the websocket closure
-