File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments