public interface MuWebSocket
An interface defining the callbacks received on a websocket which is returned by MuWebSocketFactory.create(MuRequest, Headers)
.
In order to listen to events, implement this interface and store the reference to the MuWebSocketSession
when
onConnect(MuWebSocketSession)
is called.
Important: The callbacks are called within on an NIO event thread, therefore there should be no
blocking calls in the callbacks (any blocking IO should be passed to another thread). The methods that receive a
ByteBuffer
in this interface provide a DoneCallback
parameter which should be called when
the buffer is no longer needed. If this is not called, then no more messages will be received.
Note: Rather than implementing this, you may wish to extend the BaseWebSocket
class which
handles ping events and captures the socket session, exposing it via the BaseWebSocket.session()
method.
Modifier and Type | Method and Description |
---|---|
void |
onBinary(ByteBuffer buffer,
DoneCallback onComplete)
Called when a message is received from the client.
|
void |
onClientClosed(int statusCode,
String reason)
Called when the client has closed the connection.
|
void |
onConnect(MuWebSocketSession session)
Called when the websocket is connected.
|
void |
onError(Throwable cause)
Called when an unexpected error occurs.
|
void |
onPing(ByteBuffer payload,
DoneCallback onComplete)
Called when a ping message is sent from a client.
|
void |
onPong(ByteBuffer payload,
DoneCallback onComplete)
Called when a pong message is sent from the client.
|
void |
onText(String message,
DoneCallback onComplete)
Called when a message is received from the client.
|
void onConnect(MuWebSocketSession session) throws Exception
session
- The websocket session, which can be used to send messages, pings, and close the connection.Exception
- Any exceptions thrown will result in the onError method being called with the thrown exception being used as the cause
parameter.void onText(String message, DoneCallback onComplete) throws Exception
message
- The message as a string.onComplete
- A callback that must be run with onComplete.run()
when the byte buffer is no longer needed.Exception
- Any exceptions thrown will result in the onError method being called with the thrown exception being used as the cause
parameter.void onBinary(ByteBuffer buffer, DoneCallback onComplete) throws Exception
buffer
- The message as a byte buffer.onComplete
- A callback that must be run with onComplete.run()
when the byte buffer is no longer needed. Failure to call this will result in memory leaks.Exception
- Any exceptions thrown will result in the onError method being called with the thrown exception being used as the cause
parameter.void onClientClosed(int statusCode, String reason) throws Exception
statusCode
- The closure code. See https://tools.ietf.org/html/rfc6455#section-7.4reason
- An optional reason for the closure.Exception
- Any exceptions thrown will result in the onError method being called with the thrown exception being used as the cause
parameter.void onPing(ByteBuffer payload, DoneCallback onComplete) throws Exception
payload
- The ping payload.onComplete
- A callback that must be run with onComplete.run()
when the byte buffer is no longer needed. Failure to call this will result in memory leaks.Exception
- Any exceptions thrown will result in the onError method being called with the thrown exception being used as the cause
parameter.void onPong(ByteBuffer payload, DoneCallback onComplete) throws Exception
payload
- The pong payloadonComplete
- A callback that must be run with onComplete.run()
when the byte buffer is no longer needed. Failure to call this will result in memory leaks.Exception
- Any exceptions thrown will result in the onError method being called with the thrown exception being used as the cause
parameter.void onError(Throwable cause) throws Exception
ClientDisconnectedException
(note that if the client initiates a graceful shutdown,
then onClientClosed(int, String)
will be called instead)WebSocketHandlerBuilder.withIdleReadTimeout(long, TimeUnit)
,
in which case the cause will be a TimeoutException
onText(String, DoneCallback)
etc (but not onError itself).WebSocketProtocolException
cause
- The cause of the errorException
- Any exceptions thrown will result in the connection being closed.Copyright © 2017–2021. All rights reserved.