|
3 | 3 | import javax.annotation.Nonnull; |
4 | 4 | import java.util.Map; |
5 | 5 |
|
| 6 | +/** |
| 7 | + * Websocket. Usage: |
| 8 | + * |
| 9 | + * <pre>{@code |
| 10 | + * |
| 11 | + * ws("/pattern", (ctx, configurer) -> { |
| 12 | + * configurer.onConnect(ws -> { |
| 13 | + * // Connect callback |
| 14 | + * }): |
| 15 | + * |
| 16 | + * configurer.onMessage((ws, message) -> { |
| 17 | + * ws.send("Got: " + message.value()); |
| 18 | + * }); |
| 19 | + * |
| 20 | + * configurer.onClose((ws, closeStatus) -> { |
| 21 | + * // Closing websocket |
| 22 | + * }); |
| 23 | + * |
| 24 | + * configurer.onError((ws, cause) -> { |
| 25 | + * |
| 26 | + * }); |
| 27 | + * }); |
| 28 | + * |
| 29 | + * }</pre> |
| 30 | + * |
| 31 | + * @author edgar |
| 32 | + * @since 2.2.0 |
| 33 | + */ |
6 | 34 | public interface WebSocket { |
7 | | - interface Handler { |
8 | | - void init(Context ctx, WebSocketConfigurer configurer); |
| 35 | + /** |
| 36 | + * Websocket initializer. Give you access to a read-only {@link Context} you are free to access |
| 37 | + * to request attributes, while attempt to modify a response results in exception. |
| 38 | + */ |
| 39 | + interface Initializer { |
| 40 | + /** |
| 41 | + * Callback with a readonly context and websocket configurer. |
| 42 | + * |
| 43 | + * @param ctx Readonly context. |
| 44 | + * @param configurer WebSocket configurer. |
| 45 | + */ |
| 46 | + void init(@Nonnull Context ctx, @Nonnull WebSocketConfigurer configurer); |
9 | 47 | } |
10 | 48 |
|
| 49 | + /** |
| 50 | + * On connect callback. |
| 51 | + */ |
11 | 52 | interface OnConnect { |
12 | | - void onConnect(WebSocket ws); |
| 53 | + /** |
| 54 | + * On connect callback with recently created web socket. |
| 55 | + * |
| 56 | + * @param ws WebSocket. |
| 57 | + */ |
| 58 | + void onConnect(@Nonnull WebSocket ws); |
13 | 59 | } |
14 | 60 |
|
| 61 | + /** |
| 62 | + * On message callback. When a Message is send by a client, this callback allow you to |
| 63 | + * handle/react to it. |
| 64 | + */ |
15 | 65 | interface OnMessage { |
16 | | - void onMessage(WebSocket ws, WebSocketMessage message); |
| 66 | + /** |
| 67 | + * Generated when a client send a message. |
| 68 | + * |
| 69 | + * @param ws WebSocket. |
| 70 | + * @param message Client message. |
| 71 | + */ |
| 72 | + void onMessage(@Nonnull WebSocket ws, @Nonnull WebSocketMessage message); |
17 | 73 | } |
18 | 74 |
|
19 | 75 | interface OnClose { |
|
0 commit comments