You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can implement the same WebSocket as above using annotated classes in declarative style.
174
+
Ensure that `jooby-apt` is in the annotation processor path, annotate the class with javadoc:annotation.ws.WebSocketRoute[],
175
+
and mark methods with javadoc:annotation.ws.OnConnect[], javadoc:annotation.ws.OnMessage[], javadoc:annotation.ws.OnClose[], and javadoc:annotation.ws.OnError[]. Compile code to generate an extension javadoc:Extension[] and register it by calling javadoc:Jooby[ws, io.jooby.Extension].
176
+
177
+
When a lifecycle method **returns** a value, that value is written to the client automatically: plain text or binary for `String`, `byte[]`, and `ByteBuffer`, and structured values (for example JSON) using the same encoders as in <<Structured Data>>. Alternatively, use a **void** method and send with `ws.send(...)` on the javadoc:WebSocket[] argument.
178
+
179
+
.Java
180
+
[source,java,role="primary"]
181
+
----
182
+
@WebSocketRoute("/chat/{room}") // <1>
183
+
public class ChatSocket {
184
+
185
+
@OnConnect
186
+
public String onConnect(WebSocket ws, Context ctx) { // <2>
fun onClose(ws: WebSocket, ctx: Context, status: WebSocketCloseStatus) {}
228
+
229
+
@OnError
230
+
fun onError(ws: WebSocket, ctx: Context, cause: Throwable) {}
231
+
}
232
+
233
+
// Application startup:
234
+
{
235
+
ws(ChatSocketWs_()) // <5>
236
+
}
237
+
----
238
+
239
+
<1> WebSocket route patterns for this handler.
240
+
<2> Returning a value sends it to the client without calling `send`.
241
+
<3> Return a value for automatic encoding (see <<Structured Data>>)
242
+
<4> You still can use `ws.send(...)` if method return type is `void`.
243
+
<5> Register the generated extension with javadoc:Jooby[ws, io.jooby.Extension].
244
+
245
+
171
246
==== Options
172
247
173
248
===== Connection Timeouts
@@ -192,3 +267,6 @@ websocket.maxSize = 128K
192
267
----
193
268
194
269
See the Typesafe Config documentation for the supported https://github.com/lightbend/config/blob/master/HOCON.md#size-in-bytes-format[size in bytes format].
0 commit comments