Skip to content

Commit 4c7758d

Browse files
declarative websockets docs
1 parent 515e319 commit 4c7758d

1 file changed

Lines changed: 78 additions & 0 deletions

File tree

docs/asciidoc/websocket.adoc

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,81 @@ import io.jooby.jackson.Jackson2Module
168168
}
169169
----
170170

171+
==== Declarative definition
172+
173+
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>
187+
return "welcome";
188+
}
189+
190+
@OnMessage
191+
public Map<String, String> onMessage(WebSocket ws, Context ctx, WebSocketMessage message) { // <3>
192+
return Map.of("echo", message.value());
193+
// ws.send(message.value()); // <4>
194+
}
195+
196+
@OnClose
197+
public void onClose(WebSocket ws, Context ctx, WebSocketCloseStatus status) {}
198+
199+
@OnError
200+
public void onError(WebSocket ws, Context ctx, Throwable cause) {}
201+
}
202+
203+
// Application startup:
204+
{
205+
ws(new ChatSocketWs_()); // <5>
206+
}
207+
----
208+
209+
.Kotlin
210+
[source,kotlin,role="secondary"]
211+
----
212+
@WebSocketRoute("/chat/{room}") // <1>
213+
class ChatSocket {
214+
215+
@OnConnect
216+
fun onConnect(ws: WebSocket, ctx: Context): String { // <2>
217+
return "welcome"
218+
}
219+
220+
@OnMessage
221+
fun onMessage(ws: WebSocket, ctx: Context, message: WebSocketMessage): Map<String, String> { // <3>
222+
return mapOf("echo" to message.value())
223+
// ws.send(message.value()) // <4>
224+
}
225+
226+
@OnClose
227+
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+
171246
==== Options
172247

173248
===== Connection Timeouts
@@ -192,3 +267,6 @@ websocket.maxSize = 128K
192267
----
193268

194269
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].
270+
271+
272+

0 commit comments

Comments
 (0)