#WsBefore
O app.wsBefore adiciona um handler que executa antes de um WebSocket handler. Você pode ter vários before-handlers como um WebSocket endpoint, e todos os eventos são suportados.
- Java
- Kotlin
1 2 3 4 5 6 7 |
<span class="n">app</span><span class="o">.</span><span class="na">wsBefore</span><span class="o">(</span><span class="n">ws</span> <span class="o">-></span> <span class="o">{</span> <span class="c1">// runs before all WebSocket requests</span> <span class="o">});</span> <span class="n">app</span><span class="o">.</span><span class="na">wsBefore</span><span class="o">(</span><span class="s">"/path/*"</span><span class="o">,</span> <span class="n">ws</span> <span class="o">-></span> <span class="o">{</span> <span class="c1">// runs before websocket requests to /path/*</span> <span class="o">});</span> |
1 2 3 4 5 6 7 |
<span class="n">app</span><span class="p">.</span><span class="nf">wsBefore</span> <span class="p">{</span> <span class="n">ws</span> <span class="p">-></span> <span class="c1">// runs before all WebSocket requests</span> <span class="p">}</span> <span class="n">app</span><span class="p">.</span><span class="nf">wsBefore</span><span class="p">(</span><span class="s">"/path/*"</span><span class="p">)</span> <span class="p">{</span> <span class="n">ws</span> <span class="p">-></span> <span class="c1">// runs before websocket requests to /path/*</span> <span class="p">}</span> |
#WsEndpoint
Um WebSocket endpoint é declarado com app.ws(path, handler). WebSocket handlers requerem um único paths.
- Java
- Kotlin
1 2 3 4 5 6 7 8 9 10 11 |
<span class="n">app</span><span class="o">.</span><span class="na">ws</span><span class="o">(</span><span class="s">"/websocket/:path"</span><span class="o">,</span> <span class="n">ws</span> <span class="o">-></span> <span class="o">{</span> <span class="n">ws</span><span class="o">.</span><span class="na">onConnect</span><span class="o">(</span><span class="n">ctx</span> <span class="o">-></span> <span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">"Connected"</span><span class="o">));</span> <span class="n">ws</span><span class="o">.</span><span class="na">onMessage</span><span class="o">(</span><span class="n">ctx</span> <span class="o">-></span> <span class="o">{</span> <span class="nc">User</span> <span class="n">user</span> <span class="o">=</span> <span class="n">ctx</span><span class="o">.</span><span class="na">message</span><span class="o">(</span><span class="nc">User</span><span class="o">.</span><span class="na">class</span><span class="o">);</span> <span class="c1">// convert from json</span> <span class="n">ctx</span><span class="o">.</span><span class="na">send</span><span class="o">(</span><span class="n">user</span><span class="o">);</span> <span class="c1">// convert to json and send back</span> <span class="o">});</span> <span class="n">ws</span><span class="o">.</span><span class="na">onBinaryMessage</span><span class="o">(</span><span class="n">ctx</span> <span class="o">-></span> <span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">"Message"</span><span class="o">))</span> <span class="n">ws</span><span class="o">.</span><span class="na">onClose</span><span class="o">(</span><span class="n">ctx</span> <span class="o">-></span> <span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">"Closed"</span><span class="o">));</span> <span class="n">ws</span><span class="o">.</span><span class="na">onError</span><span class="o">(</span><span class="n">ctx</span> <span class="o">-></span> <span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">"Errored"</span><span class="o">));</span> <span class="o">});</span> |
1 2 3 4 5 6 7 8 9 10 11 |
<span class="n">app</span><span class="p">.</span><span class="nf">ws</span><span class="p">(</span><span class="s">"/websocket/:path"</span><span class="p">)</span> <span class="p">{</span> <span class="n">ws</span> <span class="p">-></span> <span class="n">ws</span><span class="p">.</span><span class="nf">onConnect</span> <span class="p">{</span> <span class="n">ctx</span> <span class="p">-></span> <span class="nf">println</span><span class="p">(</span><span class="s">"Connected"</span><span class="p">)</span> <span class="p">}</span> <span class="n">ws</span><span class="p">.</span><span class="nf">onMessage</span> <span class="p">{</span> <span class="n">ctx</span> <span class="p">-></span> <span class="kd">val</span> <span class="py">user</span> <span class="p">=</span> <span class="n">ctx</span><span class="p">.</span><span class="n">message</span><span class="p"><</span><span class="nc">User</span><span class="p">>();</span> <span class="c1">// convert from json</span> <span class="n">ctx</span><span class="p">.</span><span class="nf">send</span><span class="p">(</span><span class="n">user</span><span class="p">);</span> <span class="c1">// convert to json and send back</span> <span class="p">}</span> <span class="n">ws</span><span class="p">.</span><span class="nf">onBinaryMessage</span> <span class="p">{</span> <span class="n">ctx</span> <span class="p">-></span> <span class="nf">println</span><span class="p">(</span><span class="s">"Message"</span><span class="p">)</span> <span class="p">}</span> <span class="n">ws</span><span class="p">.</span><span class="nf">onClose</span> <span class="p">{</span> <span class="n">ctx</span> <span class="p">-></span> <span class="nf">println</span><span class="p">(</span><span class="s">"Closed"</span><span class="p">)</span> <span class="p">}</span> <span class="n">ws</span><span class="p">.</span><span class="nf">onError</span> <span class="p">{</span> <span class="n">ctx</span> <span class="p">-></span> <span class="nf">println</span><span class="p">(</span><span class="s">"Errored"</span><span class="p">)</span> <span class="p">}</span> <span class="p">}</span> |
#WsAfter
O app.wsAfter adiciona um handler que executa depois de um WebSocket handler. Você pode ter vários after-handlers como um WebSocket endpoint, e todos os eventos são suportados.
- Java
- Kotlin
1 2 3 4 5 6 7 |
<span class="n">app</span><span class="o">.</span><span class="na">wsAfter</span><span class="o">(</span><span class="n">ws</span> <span class="o">-></span> <span class="o">{</span> <span class="c1">// runs after all WebSocket requests</span> <span class="o">});</span> <span class="n">app</span><span class="o">.</span><span class="na">wsAfter</span><span class="o">(</span><span class="s">"/path/*"</span><span class="o">,</span> <span class="n">ws</span> <span class="o">-></span> <span class="o">{</span> <span class="c1">// runs after websocket requests to /path/*</span> <span class="o">});</span> |
1 2 3 4 5 6 7 |
<span class="n">app</span><span class="p">.</span><span class="nf">wsAfter</span> <span class="p">{</span> <span class="n">ws</span> <span class="p">-></span> <span class="c1">// runs after all WebSocket requests</span> <span class="p">}</span> <span class="n">app</span><span class="p">.</span><span class="nf">wsAfter</span><span class="p">(</span><span class="s">"/path/:path"</span><span class="p">)</span> <span class="p">{</span> <span class="n">ws</span> <span class="p">-></span> <span class="c1">// runs after websocket requests to /path/*</span> <span class="p">}</span> |
#WsContext
O objeto wsContext fornece para você tudo o que você precisa para manipular um websocket-request. Ele contém o básico do websocket session e servlet-request, e métodos convenientes para mandar mensagens para o cliente.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<span class="n">ctx</span><span class="o">.</span><span class="na">matchedPath</span><span class="o">()</span> <span class="c1">// get the path used to match this request, ex "/path/:param"</span> <span class="n">ctx</span><span class="o">.</span><span class="na">send</span><span class="o">(</span><span class="n">object</span><span class="o">)</span> <span class="c1">// send an object as JSON (string message)</span> <span class="n">ctx</span><span class="o">.</span><span class="na">send</span><span class="o">(</span><span class="n">string</span><span class="o">)</span> <span class="c1">// send a string message</span> <span class="n">ctx</span><span class="o">.</span><span class="na">send</span><span class="o">(</span><span class="n">byteBuffer</span><span class="o">)</span> <span class="c1">// send a bytebuffer message</span> <span class="n">ctx</span><span class="o">.</span><span class="na">queryString</span><span class="o">()</span> <span class="c1">// get the query string</span> <span class="n">ctx</span><span class="o">.</span><span class="na">queryParamMap</span><span class="o">()</span> <span class="c1">// get a map of the query parameters</span> <span class="n">ctx</span><span class="o">.</span><span class="na">queryParams</span><span class="o">(</span><span class="n">key</span><span class="o">)</span> <span class="c1">// get query parameters by key</span> <span class="n">ctx</span><span class="o">.</span><span class="na">queryParam</span><span class="o">(</span><span class="n">key</span><span class="o">)</span> <span class="c1">// get query parameter by key</span> <span class="n">ctx</span><span class="o">.</span><span class="na">queryParam</span><span class="o">(</span><span class="n">key</span><span class="o">,</span> <span class="k">default</span><span class="o">)</span> <span class="c1">// get query parameter (or default value)</span> <span class="n">ctx</span><span class="o">.</span><span class="na">queryParam</span><span class="o">(</span><span class="n">key</span><span class="o">,</span> <span class="kd">class</span><span class="err">)</span> <span class="err">//</span> <span class="nc">get</span> <span class="n">query</span> <span class="n">parameter</span> <span class="n">as</span> <span class="kd">class</span> <span class="nc">ctx</span><span class="o">.</span><span class="na">pathParamMap</span><span class="o">()</span> <span class="c1">// get path parameter map</span> <span class="n">ctx</span><span class="o">.</span><span class="na">pathParam</span><span class="o">(</span><span class="n">key</span><span class="o">)</span> <span class="c1">// get path parameter</span> <span class="n">ctx</span><span class="o">.</span><span class="na">pathParam</span><span class="o">(</span><span class="n">key</span><span class="o">,</span> <span class="kd">class</span><span class="err">)</span> <span class="err">//</span> <span class="nc">get</span> <span class="n">path</span> <span class="n">parameter</span> <span class="n">as</span> <span class="kd">class</span> <span class="nc">ctx</span><span class="o">.</span><span class="na">host</span><span class="o">()</span> <span class="c1">// get the host</span> <span class="n">ctx</span><span class="o">.</span><span class="na">header</span><span class="o">(</span><span class="n">key</span><span class="o">)</span> <span class="c1">// get request header</span> <span class="n">ctx</span><span class="o">.</span><span class="na">headerMap</span><span class="o">()</span> <span class="c1">// get a map of the request headers</span> <span class="n">ctx</span><span class="o">.</span><span class="na">cookie</span><span class="o">(</span><span class="n">key</span><span class="o">)</span> <span class="c1">// get request cookie</span> <span class="n">ctx</span><span class="o">.</span><span class="na">cookieMap</span><span class="o">()</span> <span class="c1">// get a map of all request cookies</span> <span class="n">ctx</span><span class="o">.</span><span class="na">attribute</span><span class="o">(</span><span class="n">key</span><span class="o">,</span> <span class="n">value</span><span class="o">)</span> <span class="c1">// set request attribute</span> <span class="n">ctx</span><span class="o">.</span><span class="na">attribute</span><span class="o">(</span><span class="n">key</span><span class="o">)</span> <span class="c1">// get request attribute</span> <span class="n">ctx</span><span class="o">.</span><span class="na">attributeMap</span><span class="o">()</span> <span class="c1">// get a map of request attributes</span> <span class="n">ctx</span><span class="o">.</span><span class="na">sessionAttribute</span><span class="o">(</span><span class="n">key</span><span class="o">)</span> <span class="c1">// get request session attribute (from when WebSocket upgrade was performed)</span> <span class="n">ctx</span><span class="o">.</span><span class="na">sessionAttributeMap</span><span class="o">()</span> <span class="c1">// get a map of session attributes (from when WebSocket upgrade was performed)</span> |
Deixe um comentário