Skip to content

Commit a3dbdf5

Browse files
upd documentation
1 parent 042234c commit a3dbdf5

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

docs/asciidoc/websocket.adoc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,47 @@ class ChatSocket {
242242
<4> You still can use `ws.send(...)` if method return type is `void`.
243243
<5> Register the generated extension with javadoc:Jooby[ws, io.jooby.Extension].
244244

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.
245286

246287
==== Options
247288

0 commit comments

Comments
 (0)