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.Path[],
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
+
@Path("/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
+
`@OnMessage` handlers also support parsing messages into structured data, similar to MVC methods:
246
+
247
+
.Java
248
+
[source,java,role="primary"]
249
+
----
250
+
@Path("/chat/{room}")
251
+
public class ChatSocket {
252
+
253
+
record ChatMessage(String username, String message, String type) {}
254
+
255
+
@OnMessage
256
+
public Map<String, ChatMessage> onMessage(ChatMessage message) { // <1>
257
+
return Map.of("echo", message);
258
+
}
259
+
260
+
...
261
+
}
262
+
----
263
+
264
+
.Kotlin
265
+
[source,kotlin,role="secondary"]
266
+
----
267
+
@Path("/chat/{room}")
268
+
class ChatSocket {
269
+
270
+
data class ChatMessage(
271
+
val username: String,
272
+
val message: String,
273
+
val type: String
274
+
)
275
+
276
+
@OnMessage
277
+
fun onMessage(message: ChatMessage): Map<String, ChatMessage> { // <1>
278
+
return mapOf("echo" to message)
279
+
}
280
+
281
+
...
282
+
}
283
+
284
+
----
285
+
<1> WebSocket message is automatically decoded into `ChatMessage` structure.
286
+
171
287
==== Options
172
288
173
289
===== Connection Timeouts
@@ -192,3 +308,6 @@ websocket.maxSize = 128K
192
308
----
193
309
194
310
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