public class WebSocketEventStream extends AbstractEventStream
ClientEndpoint
WebSocket.
It is used to connect the WebSocket to a ServerEndpoint
and receive messages from it.Modifier and Type | Field and Description |
---|---|
private int |
defaultBufferSize
Default message buffer size in characters.
|
private int |
maxMessageLength
Maximum message length in characters
|
private java.lang.StringBuilder |
messageBuffer
Buffer for partial messages.
|
private javax.websocket.Session |
session
The WebSocket
Session . |
private javax.websocket.WebSocketContainer |
websocketContainer
The
WebSocketContainer object for a @ClientEndpoint implementation. |
Constructor and Description |
---|
WebSocketEventStream(java.net.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.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes this Stream; Events will not be posted to listeners after this call.
|
private void |
close(javax.websocket.CloseReason closeReason)
Close the WebSocket
Session with a given close reason. |
private void |
initializeStream(java.net.URI uri,
Token token,
long sessionIdleTimeout,
long idleTimeout)
Connect the WebSocket to the server pointing to /ws/{token} to receive events.
|
boolean |
isEventStreamClosed()
Helper method to check if the stream is able to receive Events.
|
void |
onClose(javax.websocket.Session session,
javax.websocket.CloseReason closeReason)
On closing the websocket, refresh the session and notify all subscribed listeners.
|
void |
onError(java.lang.Throwable throwable)
On error, convert
Throwable into CloseReason and close the session. |
void |
onMessage(java.lang.String partialMessage,
boolean last)
Notify listeners on each event received on the websocket and buffer partial messages.
|
void |
onOpen(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.
|
addEventListener, clearListeners, getListenerCount, notifyListeners, removeEventListener
private final int defaultBufferSize
private final int maxMessageLength
private final java.lang.StringBuilder messageBuffer
private final javax.websocket.WebSocketContainer websocketContainer
WebSocketContainer
object for a @ClientEndpoint implementation.private javax.websocket.Session session
Session
.public WebSocketEventStream(java.net.URI uri, Token token, long sessionIdleTimeout, long idleTimeout, int maxMsgSize, EventListener... listeners) throws SaltException
uri
- salt api urllisteners
- event listeners to be added before stream initializationidleTimeout
- idle timeout to pass to the http client configmaxMsgSize
- maximum event data size to acceptsessionIdleTimeout
- session idle timeout to pass to the http client configtoken
- salt session token to use for authenticationSaltException
- in case of an error during stream initializationprivate void initializeStream(java.net.URI uri, Token token, long sessionIdleTimeout, long idleTimeout) throws SaltException
SaltException
- in case of an error during stream initializationpublic boolean isEventStreamClosed()
public void close() throws java.io.IOException
java.io.IOException
- in case of an error when closing the sessionprivate void close(javax.websocket.CloseReason closeReason) throws java.io.IOException
Session
with a given close reason.closeReason
- the reason for the websocket closurejava.io.IOException
- in case of an error when closing the sessionpublic void onOpen(javax.websocket.Session session, javax.websocket.EndpointConfig config) throws java.io.IOException
session
- The just started WebSocket Session
.config
- The EndpointConfig
containing the handshake informations.java.io.IOException
- if something goes wrong sending message to the remote peerpublic void onMessage(java.lang.String partialMessage, boolean last) throws MessageTooBigException
partialMessage
- partial message received on this websocketlast
- indicate the last part of a messageMessageTooBigException
- in case the message is longer than maxMessageLengthpublic void onError(java.lang.Throwable throwable) throws java.io.IOException
Throwable
into CloseReason
and close the session.throwable
- The Throwable object received on the current error.java.io.IOException
- in case of an error when closing the sessionpublic void onClose(javax.websocket.Session session, javax.websocket.CloseReason closeReason)
session
- the websocket Session
closeReason
- the CloseReason
for the websocket closure