public interface WebSocket
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) -> {
});
});
NOTE: Websocket API ONLY handles text messages (not binary message).Modifier and Type | Interface and Description |
---|---|
static interface |
WebSocket.Initializer
Websocket initializer.
|
static interface |
WebSocket.OnClose
On close callback.
|
static interface |
WebSocket.OnConnect
On connect callback.
|
static interface |
WebSocket.OnError
On error callback.
|
static interface |
WebSocket.OnMessage
On message callback.
|
Modifier and Type | Field and Description |
---|---|
static int |
MAX_BUFFER_SIZE
Max message size for websocket (13k).
|
Modifier and Type | Method and Description |
---|---|
default <T> T |
attribute(String key)
Get an attribute by his key.
|
default WebSocket |
attribute(String key,
Object value)
Set an application attribute.
|
default WebSocket |
close()
Close the web socket and send a
WebSocketCloseStatus.NORMAL code to client. |
WebSocket |
close(WebSocketCloseStatus closeStatus)
Close the web socket and send a close status code to client.
|
default Map<String,Object> |
getAttributes()
Context attributes (a.k.a request attributes).
|
Context |
getContext()
Originating HTTP context.
|
List<WebSocket> |
getSessions()
Web sockets connected to the same path.
|
boolean |
isOpen()
True if websocket is open.
|
default WebSocket |
render(Object value)
Encode a value and send a text message to client.
|
WebSocket |
render(Object value,
boolean broadcast)
Encode a value and send a text message to current client (broadcast = false) or to ALL
connected clients under the websocket path (broadcast = true).
|
default WebSocket |
send(byte[] message)
Send a text message to client.
|
WebSocket |
send(byte[] message,
boolean broadcast)
Send a text message to current client (broadcast = false) or to ALL connected clients under the
websocket path (broadcast = true).
|
default WebSocket |
send(String message)
Send a text message to client.
|
WebSocket |
send(String message,
boolean broadcast)
Send a text message to current client (broadcast = false) or to ALL connected clients under the
websocket path (broadcast = true).
|
static final int MAX_BUFFER_SIZE
@Nonnull Context getContext()
@Nonnull default Map<String,Object> getAttributes()
@Nonnull default <T> T attribute(@Nonnull String key)
getAttributes()
.
This method look first in current context and fallback to application attributes.T
- Attribute type.key
- Attribute key.@Nonnull default WebSocket attribute(@Nonnull String key, Object value)
key
- Attribute key.value
- Attribute value.@Nonnull default WebSocket send(@Nonnull String message)
message
- Text Message.@Nonnull List<WebSocket> getSessions()
boolean isOpen()
@Nonnull WebSocket send(@Nonnull String message, boolean broadcast)
message
- Text Message.broadcast
- True to send to all connected clients.@Nonnull default WebSocket send(@Nonnull byte[] message)
message
- Text Message.@Nonnull WebSocket send(@Nonnull byte[] message, boolean broadcast)
message
- Text Message.broadcast
- True to send to all connected clients.@Nonnull default WebSocket render(@Nonnull Object value)
value
- Value to send.@Nonnull WebSocket render(@Nonnull Object value, boolean broadcast)
value
- Value to send.broadcast
- True to send to all connected clients.@Nonnull default WebSocket close()
WebSocketCloseStatus.NORMAL
code to client.
This method fires a WebSocket.OnClose.onClose(WebSocket, WebSocketCloseStatus)
callback.@Nonnull WebSocket close(@Nonnull WebSocketCloseStatus closeStatus)
WebSocket.OnClose.onClose(WebSocket, WebSocketCloseStatus)
callback.closeStatus
- Close status.Copyright © 2020. All rights reserved.