public interface WebSocket
Websocket. Usage:
ws("/pattern", (ctx, configurer) -> {
configurer.onConnect(ws -> {
// Connect callback
}):
configurer.onMessage((ws, message) -> {
ws.send("Got: " + message.value());
});
configurer.onClose((ws, closeStatus) -> {
// Closing websocket
});
configurer.onError((ws, cause) -> {
});
});
- Since:
- 2.2.0
- Author:
- edgar
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic interface
Websocket initializer.static interface
On close callback.static interface
On connect callback.static interface
On error callback.static interface
On message callback.static interface
Callback for sending messages. -
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Max message size for websocket (128K). -
Method Summary
Modifier and TypeMethodDescriptiondefault <T> T
Get an attribute by his key.default WebSocket
Set an application attribute.default WebSocket
close()
Close the web socket and send aWebSocketCloseStatus.NORMAL
code to client.close
(WebSocketCloseStatus closeStatus) Close the web socket and send a close status code to client.void
forEach
(SneakyThrows.Consumer<WebSocket> callback) For each of live sessions (including this) do something with it.Context attributes (a.k.a request attributes).Originating HTTP context.Web sockets connected to the same path.boolean
isOpen()
True if websocket is open.default WebSocket
Encode a value and send a text message to client.render
(Object value, WebSocket.WriteCallback callback) Encode a value and send a text message to client.default WebSocket
renderBinary
(Object value) Encode a value and send a binary message to client.renderBinary
(Object value, WebSocket.WriteCallback callback) Encode a value and send a binary message to client.default WebSocket
send
(byte[] message) Send a text message to client.default WebSocket
send
(byte[] message, WebSocket.WriteCallback callback) Send a text message to client.default WebSocket
send
(DataBuffer message) send
(DataBuffer message, WebSocket.WriteCallback callback) default WebSocket
Send a text message to client.send
(String message, WebSocket.WriteCallback callback) Send a text message to client.default WebSocket
send
(ByteBuffer message) send
(ByteBuffer message, WebSocket.WriteCallback callback) default WebSocket
sendBinary
(byte[] message) Send a binary message to client.default WebSocket
sendBinary
(byte[] message, WebSocket.WriteCallback callback) Send a binary message to client.default WebSocket
sendBinary
(DataBuffer message) sendBinary
(DataBuffer message, WebSocket.WriteCallback callback) default WebSocket
sendBinary
(String message) Send a binary message to client.sendBinary
(String message, WebSocket.WriteCallback callback) Send a binary message to client.default WebSocket
sendBinary
(ByteBuffer message) sendBinary
(ByteBuffer message, WebSocket.WriteCallback callback)
-
Field Details
-
MAX_BUFFER_SIZE
static final int MAX_BUFFER_SIZEMax message size for websocket (128K).- See Also:
-
-
Method Details
-
getContext
Originating HTTP context. Please note this is a read-only context, so you are not allowed to modify or produces a response from it.The context let give you access to originating request (then one that was upgrade it).
- Returns:
- Read-only originating HTTP request.
-
getAttributes
Context attributes (a.k.a request attributes).- Returns:
- Context attributes.
-
attribute
Get an attribute by his key. This is just an utility method aroundgetAttributes()
. This method look first in current context and fallback to application attributes.- Type Parameters:
T
- Attribute type.- Parameters:
key
- Attribute key.- Returns:
- Attribute value.
-
attribute
Set an application attribute.- Parameters:
key
- Attribute key.value
- Attribute value.- Returns:
- This router.
-
getSessions
Web sockets connected to the same path. This method doesn't include the current websocket.- Returns:
- Web sockets or empty list.
-
isOpen
boolean isOpen()True if websocket is open.- Returns:
- True when open.
-
forEach
For each of live sessions (including this) do something with it.Broadcast example: ws.forEach(session -> { session.send("Message"); });
- Parameters:
callback
- Callback.
-
send
Send a text message to client.- Parameters:
message
- Text Message.- Returns:
- This websocket.
-
send
Send a text message to client.- Parameters:
message
- Text Message.callback
- Write callback.- Returns:
- This websocket.
-
send
Send a text message to client.- Parameters:
message
- Text Message.- Returns:
- This websocket.
-
send
Send a text message to client.- Parameters:
message
- Text Message.callback
- Write callback.- Returns:
- This websocket.
-
send
-
send
-
send
-
send
-
sendBinary
Send a binary message to client.- Parameters:
message
- Binary Message.- Returns:
- This websocket.
-
sendBinary
Send a binary message to client.- Parameters:
message
- Binary Message.callback
- Write callback.- Returns:
- This websocket.
-
sendBinary
Send a binary message to client.- Parameters:
message
- Binary Message.- Returns:
- This websocket.
-
sendBinary
@NonNull default WebSocket sendBinary(@NonNull byte[] message, @NonNull WebSocket.WriteCallback callback) Send a binary message to client.- Parameters:
message
- Binary Message.callback
- Write callback.- Returns:
- This websocket.
-
sendBinary
-
sendBinary
@NonNull WebSocket sendBinary(@NonNull ByteBuffer message, @NonNull WebSocket.WriteCallback callback) -
sendBinary
-
sendBinary
@NonNull WebSocket sendBinary(@NonNull DataBuffer message, @NonNull WebSocket.WriteCallback callback) -
render
Encode a value and send a text message to client.- Parameters:
value
- Value to send.- Returns:
- This websocket.
-
render
Encode a value and send a text message to client.- Parameters:
value
- Value to send.callback
- Write callback.- Returns:
- This websocket.
-
renderBinary
Encode a value and send a binary message to client.- Parameters:
value
- Value to send.- Returns:
- This websocket.
-
renderBinary
Encode a value and send a binary message to client.- Parameters:
value
- Value to send.callback
- Write callback.- Returns:
- This websocket.
-
close
Close the web socket and send aWebSocketCloseStatus.NORMAL
code to client.This method fires a
WebSocket.OnClose.onClose(WebSocket, WebSocketCloseStatus)
callback.- Returns:
- This websocket.
-
close
Close the web socket and send a close status code to client.This method fires a
WebSocket.OnClose.onClose(WebSocket, WebSocketCloseStatus)
callback.- Parameters:
closeStatus
- Close status.- Returns:
- This websocket.
-