public abstract class BaseWebSocket extends Object implements MuWebSocket
A base class for server-side web sockets, that takes care of capturing the web socket session, responding to pings, and closure events.
This is an alternative to implementing the MuWebSocket
interface and is recommended so that any
additions to the interface are non-breaking to implementors.
Constructor and Description |
---|
BaseWebSocket() |
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.
|
protected MuWebSocketSession |
session()
Gets the websocket session
|
protected WebsocketSessionState |
state() |
protected WebsocketSessionState state()
public void onConnect(MuWebSocketSession session) throws Exception
MuWebSocket
onConnect
in interface MuWebSocket
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.public void onText(String message, DoneCallback onComplete) throws Exception
MuWebSocket
onText
in interface MuWebSocket
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.public void onBinary(ByteBuffer buffer, DoneCallback onComplete) throws Exception
MuWebSocket
onBinary
in interface MuWebSocket
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.public void onClientClosed(int statusCode, String reason) throws Exception
MuWebSocket
The connection should be closed on the server side when this is received. If overriding BaseWebSocket
this occurs automatically.
onClientClosed
in interface MuWebSocket
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.public void onPing(ByteBuffer payload, DoneCallback onComplete) throws Exception
MuWebSocket
When received, a websocket should send the data back in a pong
message. If overriding BaseWebSocket
this occurs automatically.
onPing
in interface MuWebSocket
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.public void onPong(ByteBuffer payload, DoneCallback onComplete) throws Exception
MuWebSocket
onPong
in interface MuWebSocket
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.public void onError(Throwable cause) throws Exception
MuWebSocket
ClientDisconnectedException
(note that if the client initiates a graceful shutdown,
then MuWebSocket.onClientClosed(int, String)
will be called instead)WebSocketHandlerBuilder.withIdleReadTimeout(long, TimeUnit)
,
in which case the cause will be a TimeoutException
MuWebSocket.onText(String, DoneCallback)
etc (but not onError itself).onError
in interface MuWebSocket
cause
- The cause of the errorException
- Any exceptions thrown will result in the connection being closed.protected MuWebSocketSession session()
IllegalStateException
- Thrown if the socket has not been connected yet.Copyright © 2017–2021. All rights reserved.