public class EventStream extends Object implements AutoCloseable
ClientEndpoint
WebSocket.
It is used to connect the WebSocket to a ServerEndpoint
and receive messages from it; for each message a bunch of EventListener
will be recalled and notified with it.Constructor and Description |
---|
EventStream(ClientConfig config,
EventListener... listeners)
Constructor used to create an event stream: open a websocket connection and start
event processing.
|
Modifier and Type | Method and Description |
---|---|
void |
addEventListener(EventListener listener)
Implementation of
addEventListener(EventListener) |
void |
close()
Close the WebSocket
Session . |
void |
close(javax.websocket.CloseReason closeReason)
Close the WebSocket
Session with a given close reason. |
int |
getListenerCount()
Helper method that returns the current number of subscribed listeners.
|
boolean |
isEventStreamClosed()
Helper method to check if the WebSocket Session exists and is open.
|
void |
onClose(javax.websocket.Session session,
javax.websocket.CloseReason closeReason)
On closing the websocket, refresh the session and notify all subscribed listeners.
|
void |
onError(Throwable throwable)
On error, convert
Throwable into CloseReason and close the session. |
void |
onMessage(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.
|
void |
removeEventListener(EventListener listener)
Implementation of
removeEventListener(EventListener) . |
public EventStream(ClientConfig config, EventListener... listeners) throws SaltException
config
- client configuration containing the URL, token, timeouts, etc.listeners
- event listeners to be added before stream initializationSaltException
- in case of an error during stream initializationpublic void addEventListener(EventListener listener)
addEventListener(EventListener)
listener
- Reference to the class that implements EventListener
.public void removeEventListener(EventListener listener)
removeEventListener(EventListener)
.listener
- Reference to the class that implements EventListener
.public int getListenerCount()
public boolean isEventStreamClosed()
EventStream
WebSocket Session
state.public void close() throws IOException
Session
.close
in interface AutoCloseable
IOException
- in case of an error when closing the sessionpublic void close(javax.websocket.CloseReason closeReason) throws IOException
Session
with a given close reason.closeReason
- the reason for the websocket closureIOException
- in case of an error when closing the sessionpublic void onOpen(javax.websocket.Session session, javax.websocket.EndpointConfig config) throws IOException
session
- The just started WebSocket Session
.config
- The EndpointConfig
containing the handshake informations.IOException
- if something goes wrong sending message to the remote peerpublic void onMessage(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(Throwable throwable) throws IOException
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 sessionpublic void onClose(javax.websocket.Session session, javax.websocket.CloseReason closeReason)
session
- the websocket Session
closeReason
- the CloseReason
for the websocket closureCopyright © 2016. All rights reserved.