public interface WebSocket extends Closeable
{ ws("/", (ws) -> { // connected ws.onMessage(message -> { System.out.println(message.value()); ws.send("Message Received"); }); ws.send("Connected"); }); }First thing you need to do is to register a new web socket in your App using the
Jooby.ws(String, WebSocket.Handler)
method.
You can specify a path to listen for web socket connection. The path can be a static path or
a path pattern (like routes).
On new connections the WebSocket.Handler.connect(WebSocket)
will be executed from there
you can listen on onMessage(Callback)
, onClose(Callback)
or
onError(ErrCallback)
events.
Inside a handler you can send text or binary message.
json
it is nice to define a consumes and produces types:
ws("/", (ws) -> { ws.onMessage(message -> { // read as json MyObject object = message.to(MyObject.class); }); MyObject object = new MyObject(); ws.send(object); // convert to text message using a json converter }) .consumes(MediaType.json) .produces(MediaType.json);
Modifier and Type | Interface and Description |
---|---|
static interface |
WebSocket.Callback<T>
Web socket callback.
|
static class |
WebSocket.CloseStatus
Hold a status code and optionally a reason message for
close() operations. |
static class |
WebSocket.Definition
Configure a web socket.
|
static interface |
WebSocket.ErrCallback
Web socket err callback.
|
static interface |
WebSocket.Handler
A web socket connect handler.
|
static interface |
WebSocket.SuccessCallback
Web socket success callback.
|
Modifier and Type | Field and Description |
---|---|
static WebSocket.CloseStatus |
BAD_DATA
"1007 indicates that an endpoint is terminating the connection because it has
received data within a message that was not consistent with the type of the message
(e.g., non-UTF-8 [RFC3629] data within a text message)."
|
static WebSocket.ErrCallback |
ERR
Default err callback.
|
static WebSocket.CloseStatus |
GOING_AWAY
"1001 indicates that an endpoint is "going away", such as a server going down or a
browser having navigated away from a page."
|
static WebSocket.CloseStatus |
NORMAL
"1000 indicates a normal closure, meaning that the purpose for which the connection
was established has been fulfilled."
|
static WebSocket.CloseStatus |
NOT_ACCEPTABLE
"1003 indicates that an endpoint is terminating the connection because it has
received a type of data it cannot accept (e.g., an endpoint that understands only
text data MAY send this if it receives a binary message)."
|
static WebSocket.CloseStatus |
POLICY_VIOLATION
"1008 indicates that an endpoint is terminating the connection because it has
received a message that violates its policy.
|
static WebSocket.CloseStatus |
PROTOCOL_ERROR
"1002 indicates that an endpoint is terminating the connection due to a protocol
error."
|
static WebSocket.CloseStatus |
REQUIRED_EXTENSION
"1010 indicates that an endpoint (client) is terminating the connection because it
has expected the server to negotiate one or more extension, but the server didn't
return them in the response message of the WebSocket handshake.
|
static WebSocket.CloseStatus |
SERVER_ERROR
"1011 indicates that a server is terminating the connection because it encountered
an unexpected condition that prevented it from fulfilling the request."
|
static WebSocket.CloseStatus |
SERVICE_OVERLOAD
"1013 indicates that the service is experiencing overload.
|
static WebSocket.CloseStatus |
SERVICE_RESTARTED
"1012 indicates that the service is restarted.
|
static WebSocket.SuccessCallback |
SUCCESS
Default success callback.
|
static WebSocket.CloseStatus |
TOO_BIG_TO_PROCESS
"1009 indicates that an endpoint is terminating the connection because it has
received a message that is too big for it to process."
|
Modifier and Type | Method and Description |
---|---|
default void |
close()
Gracefully closes the connection, after sending a description message
|
default void |
close(int code)
Gracefully closes the connection, after sending a description message
|
default void |
close(int code,
String reason)
Gracefully closes the connection, after sending a description message
|
void |
close(WebSocket.CloseStatus status)
Gracefully closes the connection, after sending a description message
|
MediaType |
consumes() |
void |
onClose(WebSocket.Callback<WebSocket.CloseStatus> callback)
Register an close callback to execute when client close the web socket.
|
void |
onError(WebSocket.ErrCallback callback)
Register an error callback to execute when an error is found.
|
void |
onMessage(WebSocket.Callback<Mutant> callback)
Register a callback to execute when a new message arrive.
|
String |
path() |
String |
pattern() |
void |
pause()
Pause the client stream.
|
MediaType |
produces() |
default <T> T |
require(Class<T> type)
Find and return a service using the provided type.
|
<T> T |
require(com.google.inject.Key<T> key)
Find and return a service using the provided key.
|
default <T> T |
require(com.google.inject.TypeLiteral<T> type)
Find and return a service using the provided type.
|
void |
resume()
Resume the client stream.
|
default void |
send(Object data)
Send data through the connection.
|
default void |
send(Object data,
WebSocket.ErrCallback err)
Send data through the connection.
|
default void |
send(Object data,
WebSocket.SuccessCallback success)
Send data through the connection.
|
void |
send(Object data,
WebSocket.SuccessCallback success,
WebSocket.ErrCallback err)
Send data through the connection.
|
void |
terminate()
Immediately shuts down the connection.
|
Map<Object,String> |
vars() |
static final WebSocket.SuccessCallback SUCCESS
static final WebSocket.ErrCallback ERR
static final WebSocket.CloseStatus NORMAL
static final WebSocket.CloseStatus GOING_AWAY
static final WebSocket.CloseStatus PROTOCOL_ERROR
static final WebSocket.CloseStatus NOT_ACCEPTABLE
static final WebSocket.CloseStatus BAD_DATA
static final WebSocket.CloseStatus POLICY_VIOLATION
static final WebSocket.CloseStatus TOO_BIG_TO_PROCESS
static final WebSocket.CloseStatus REQUIRED_EXTENSION
static final WebSocket.CloseStatus SERVER_ERROR
static final WebSocket.CloseStatus SERVICE_RESTARTED
static final WebSocket.CloseStatus SERVICE_OVERLOAD
String path()
String pattern()
MediaType consumes()
* / *
.MediaType produces()
* / *
.void onMessage(WebSocket.Callback<Mutant> callback) throws Exception
callback
- A callbackException
- If something goes wrong.void onError(WebSocket.ErrCallback callback)
callback
- A callbackvoid onClose(WebSocket.Callback<WebSocket.CloseStatus> callback) throws Exception
callback
- A callbackException
- If something goes wrong.default void close(int code, String reason)
code
- Close status code.reason
- Close reason.default void close(int code)
code
- Close status code.default void close()
close
in interface AutoCloseable
close
in interface Closeable
void close(WebSocket.CloseStatus status)
status
- Close status code.void resume()
void pause()
void terminate() throws Exception
Exception
- If something goes wrong.default void send(Object data) throws Exception
data
- Data to send.Exception
- If something goes wrong.default void send(Object data, WebSocket.SuccessCallback success) throws Exception
data
- Data to send.success
- A success callback.Exception
- If something goes wrong.default void send(Object data, WebSocket.ErrCallback err) throws Exception
data
- Data to send.err
- An err callback.Exception
- If something goes wrong.void send(Object data, WebSocket.SuccessCallback success, WebSocket.ErrCallback err) throws Exception
data
- Data to send.success
- A success callback.err
- An err callback.Exception
- If something goes wrong.default <T> T require(Class<T> type)
T
- Service type.type
- A service type.default <T> T require(com.google.inject.TypeLiteral<T> type)
T
- Service type.type
- A service type.<T> T require(com.google.inject.Key<T> key)
T
- Service type.key
- A key for a service.Copyright © 2015. All rights reserved.