|
| 1 | +== Web Sockets |
| 2 | + |
| 3 | +Jooby supports https://developer.mozilla.org/es/docs/Web/API/WebSockets_API[WebSockets]. |
| 4 | +A javadoc:WebSocket[] is registered like any other handler: |
| 5 | + |
| 6 | +.WebSocket |
| 7 | +[source,java,role="primary"] |
| 8 | +---- |
| 9 | +{ |
| 10 | + ws("/ws", (ctx, configurer) -> { // <1> |
| 11 | + configurer.onConnect(ws -> { |
| 12 | + ws.send("Connected"); // <2> |
| 13 | + }); |
| 14 | + |
| 15 | + configurer.onMessage((ws, message) -> { |
| 16 | + ws.send("Got " + message.value()); // <3> |
| 17 | + }); |
| 18 | + |
| 19 | + configurer.onClose((ws, statusCode) -> { |
| 20 | + // <4> |
| 21 | + }); |
| 22 | + |
| 23 | + configurer.onError((ws, cause) -> { |
| 24 | + // 5 |
| 25 | + }); |
| 26 | + }); |
| 27 | +} |
| 28 | +---- |
| 29 | + |
| 30 | +.Kotlin |
| 31 | +[source,kotlin,role="secondary"] |
| 32 | +---- |
| 33 | +{ |
| 34 | + ws("/ws") { // <1> |
| 35 | + configurer.onConnect { ws -> |
| 36 | + ws.send("Connected") // <2> |
| 37 | + } |
| 38 | + |
| 39 | + configurer.onMessage { ws, message -> |
| 40 | + ws.send("Got " + message.value()) // <3> |
| 41 | + } |
| 42 | + |
| 43 | + configurer.onClose { ws, statusCode -> |
| 44 | + // <4> |
| 45 | + } |
| 46 | +
|
| 47 | + configurer.onError { ws, cause -> |
| 48 | + // <5> |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | +---- |
| 53 | + |
| 54 | +<1> Add a WebSocket handler. Useful to initialize resources |
| 55 | +<2> On WebSocket connect/open send a message back to client. Useful to initialize resources |
| 56 | +<3> On new message send back to client |
| 57 | +<4> WebSocket is about to close, you must free/release any acquire resources |
| 58 | +<5> WebSocket found a exception. Useful to log the error and provide an alternative response is |
| 59 | +the WebSocket is still open |
| 60 | + |
| 61 | +You are free to access to HTTP context from WebSocket configurer or callback, but it is disallowed |
| 62 | +to modify the HTTP context or produces a response from it: |
| 63 | + |
| 64 | +.Context |
| 65 | +[source,java,role="primary"] |
| 66 | +---- |
| 67 | +{ |
| 68 | + ws("/ws/{key}", (ctx, configurer) -> { |
| 69 | + String key = ctx.path("key").value(); // <1> |
| 70 | + String foo = ctx.session().get("foo").value(); // <2> |
| 71 | + ... |
| 72 | + }); |
| 73 | +} |
| 74 | +---- |
| 75 | + |
| 76 | +.Kotlin |
| 77 | +[source,kotlin,role="secondary"] |
| 78 | +---- |
| 79 | +{ |
| 80 | + ws("/ws/{key}") { ctx, configurer -> |
| 81 | + val key = ctx.path("key").value() // <1> |
| 82 | + val foo = ctx.session().get("foo").value() // <2> |
| 83 | + ... |
| 84 | + } |
| 85 | +} |
| 86 | +---- |
| 87 | + |
| 88 | +<1> Access to path variable: `key` |
| 89 | +<2> Access to session variable: `foo` |
| 90 | + |
| 91 | +=== Structured data |
| 92 | + |
| 93 | +Structure data is supported using the Value API and the javadoc:WebSocket[render] method: |
| 94 | + |
| 95 | +.JSON example: |
| 96 | + |
| 97 | +.JSON example |
| 98 | +[source,java,role="primary"] |
| 99 | +---- |
| 100 | +import io.jooby.json.JacksonModule; |
| 101 | +
|
| 102 | +{ |
| 103 | + install(new JackonModule()); // <1> |
| 104 | +
|
| 105 | + ws("/ws", (ctx, configurer) -> { |
| 106 | + configurer.onMessage((ws, message) -> { |
| 107 | + MyObject myobject = message.to(MyObject.class); // <2> |
| 108 | + ws.render(myobject); // <3> |
| 109 | + }) |
| 110 | + }); |
| 111 | +} |
| 112 | +---- |
| 113 | + |
| 114 | +.Kotlin |
| 115 | +[source,kotlin,role="secondary"] |
| 116 | +---- |
| 117 | +{ |
| 118 | + install(JacksonModule()) // <1> |
| 119 | +
|
| 120 | + ws("/ws/{key}") { ctx, configurer -> |
| 121 | + configurer.onMessage { ws, message -> |
| 122 | + val myobject = message.to<MyObject>() // <2> |
| 123 | + ws.render(myobject) // <3> |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | +---- |
| 128 | + |
| 129 | +<1> Install Jackson module (required for JSON decoding/encoding) |
| 130 | +<2> Parse/decode message to `MyObject` |
| 131 | +<3> Encode myobject as JSON and send to client |
| 132 | + |
| 133 | +Alternative you explicit tells with decoder/encoder to use using consumes/produces attributes: |
| 134 | + |
| 135 | +.Context |
| 136 | +[source,java,role="primary"] |
| 137 | +---- |
| 138 | +import io.jooby.json.JacksonModule; |
| 139 | +
|
| 140 | +{ |
| 141 | + install(new JackonModule()); // <1> |
| 142 | +
|
| 143 | + ws("/ws", (ctx, configurer) -> { |
| 144 | + configurer.onMessage((ws, message) -> { |
| 145 | + MyObject myobject = message.to(MyObject.class); // <2> |
| 146 | + ws.render(myobject); // <3> |
| 147 | + }) |
| 148 | + }) |
| 149 | + .consumes(MediaType.json) |
| 150 | + .produces(MediaType.json); |
| 151 | +} |
| 152 | +---- |
| 153 | + |
| 154 | +.Kotlin |
| 155 | +[source,kotlin,role="secondary"] |
| 156 | +---- |
| 157 | +{ |
| 158 | + install(JacksonModule()) // <1> |
| 159 | +
|
| 160 | + ws("/ws/{key}") { ctx, configurer -> |
| 161 | + configurer.onMessage { ws, message -> |
| 162 | + val myobject = message.to<MyObject>() // <2> |
| 163 | + ws.render(myobject) // <3> |
| 164 | + } |
| 165 | + }.consumes(MediaType.json) |
| 166 | + .produces(MediaType.json) |
| 167 | +} |
| 168 | +---- |
| 169 | + |
| 170 | +Structure messages depends/requires a javadoc:MessageDecoder[] and jadoc:MessageEncoder[]. In this |
| 171 | +example both are provided by the JacksonModule. |
| 172 | + |
| 173 | +=== Connection Timeouts |
| 174 | +By default Jooby will timeout idle connections that have no activity after 5 minutes. You can |
| 175 | +control this behaviour by setting the `websocket.idleTimeout` property: |
| 176 | + |
| 177 | +.application.conf |
| 178 | +[source, properties] |
| 179 | +---- |
| 180 | +websocket.idleTimeout = 1h |
| 181 | +---- |
| 182 | + |
| 183 | +See https://github.com/lightbend/config/blob/master/HOCON.md#duration-format[duration format] |
0 commit comments