|
28 | 28 | * |
29 | 29 | * }</pre> |
30 | 30 | * |
| 31 | + * NOTE: Websocket API ONLY handles text messages (not binary message). |
| 32 | + * |
31 | 33 | * @author edgar |
32 | 34 | * @since 2.2.0 |
33 | 35 | */ |
@@ -72,15 +74,43 @@ interface OnMessage { |
72 | 74 | void onMessage(@Nonnull WebSocket ws, @Nonnull WebSocketMessage message); |
73 | 75 | } |
74 | 76 |
|
| 77 | + /** |
| 78 | + * On close callback. Generated when client close the connection or when explicit calls to |
| 79 | + * {@link #close(WebSocketCloseStatus)} or {@link #disconnect()}. |
| 80 | + */ |
75 | 81 | interface OnClose { |
76 | | - void onClose(WebSocket ws, WebSocketCloseStatus closeStatus); |
| 82 | + /** |
| 83 | + * Generated when client close the connection or when explicit calls to |
| 84 | + * {@link #close(WebSocketCloseStatus)} or {@link #disconnect()}. |
| 85 | + * |
| 86 | + * @param ws WebSocket. |
| 87 | + * @param closeStatus Close status. |
| 88 | + */ |
| 89 | + void onClose(@Nonnull WebSocket ws, @Nonnull WebSocketCloseStatus closeStatus); |
77 | 90 | } |
78 | 91 |
|
| 92 | + /** |
| 93 | + * On error callback. Generated when unexpected error occurs. |
| 94 | + */ |
79 | 95 | interface OnError { |
80 | | - void onError(WebSocket ws, Throwable cause); |
| 96 | + /** |
| 97 | + * Error callback, let you listen for exception. Websocket might or might not be open. |
| 98 | + * |
| 99 | + * @param ws Websocket. |
| 100 | + * @param cause Cause. |
| 101 | + */ |
| 102 | + void onError(@Nonnull WebSocket ws, @Nonnull Throwable cause); |
81 | 103 | } |
82 | 104 |
|
83 | | - Context getContext(); |
| 105 | + /** |
| 106 | + * Originating HTTP context. Please note this is a read-only context, so you are not allowed |
| 107 | + * to modify or produces a response from it. |
| 108 | + * |
| 109 | + * The context let give you access to originating request (then one that was upgrade it). |
| 110 | + * |
| 111 | + * @return Read-only originating HTTP request. |
| 112 | + */ |
| 113 | + @Nonnull Context getContext(); |
84 | 114 |
|
85 | 115 | /** |
86 | 116 | * Context attributes (a.k.a request attributes). |
@@ -115,22 +145,84 @@ interface OnError { |
115 | 145 | return this; |
116 | 146 | } |
117 | 147 |
|
118 | | - default WebSocket send(String message) { |
| 148 | + /** |
| 149 | + * Send a text message to client. |
| 150 | + * |
| 151 | + * @param message Text Message. |
| 152 | + * @return This websocket. |
| 153 | + */ |
| 154 | + default @Nonnull WebSocket send(@Nonnull String message) { |
119 | 155 | return send(message, false); |
120 | 156 | } |
121 | 157 |
|
122 | | - WebSocket send(String message, boolean broadcast); |
| 158 | + /** |
| 159 | + * Send a text message to current client (broadcast = false) or to ALL connected clients under the |
| 160 | + * websocket path (broadcast = true). |
| 161 | + * |
| 162 | + * @param message Text Message. |
| 163 | + * @param broadcast True to send to all connected clients. |
| 164 | + * @return This websocket. |
| 165 | + */ |
| 166 | + @Nonnull WebSocket send(@Nonnull String message, boolean broadcast); |
123 | 167 |
|
124 | | - default WebSocket send(byte[] bytes) { |
125 | | - return send(bytes, false); |
| 168 | + /** |
| 169 | + * Send a text message to client. |
| 170 | + * |
| 171 | + * @param message Text Message. |
| 172 | + * @return This websocket. |
| 173 | + */ |
| 174 | + default @Nonnull WebSocket send(@Nonnull byte[] message) { |
| 175 | + return send(message, false); |
126 | 176 | } |
127 | 177 |
|
128 | | - WebSocket send(byte[] bytes, boolean broadcast); |
| 178 | + /** |
| 179 | + * Send a text message to current client (broadcast = false) or to ALL connected clients under the |
| 180 | + * websocket path (broadcast = true). |
| 181 | + * |
| 182 | + * @param message Text Message. |
| 183 | + * @param broadcast True to send to all connected clients. |
| 184 | + * @return This websocket. |
| 185 | + */ |
| 186 | + @Nonnull WebSocket send(@Nonnull byte[] message, boolean broadcast); |
129 | 187 |
|
130 | | - default WebSocket render(Object message) { |
131 | | - return render(message, false); |
| 188 | + /** |
| 189 | + * Encode a value and send a text message to client. |
| 190 | + * |
| 191 | + * @param value Value to send. |
| 192 | + * @return This websocket. |
| 193 | + */ |
| 194 | + default @Nonnull WebSocket render(@Nonnull Object value) { |
| 195 | + return render(value, false); |
132 | 196 | } |
133 | 197 |
|
134 | | - WebSocket render(Object message, boolean broadcast); |
| 198 | + /** |
| 199 | + * Encode a value and send a text message to current client (broadcast = false) or to ALL |
| 200 | + * connected clients under the websocket path (broadcast = true). |
| 201 | + * |
| 202 | + * @param value Value to send. |
| 203 | + * @param broadcast True to send to all connected clients. |
| 204 | + * @return This websocket. |
| 205 | + */ |
| 206 | + @Nonnull WebSocket render(@Nonnull Object value, boolean broadcast); |
| 207 | + |
| 208 | + /** |
| 209 | + * Close the web socket and send a {@link WebSocketCloseStatus#NORMAL} code to client. |
| 210 | + * |
| 211 | + * This method fires a {@link OnClose#onClose(WebSocket, WebSocketCloseStatus)} callback. |
| 212 | + * |
| 213 | + * @return This websocket. |
| 214 | + */ |
| 215 | + default @Nonnull WebSocket close() { |
| 216 | + return close(WebSocketCloseStatus.NORMAL); |
| 217 | + } |
135 | 218 |
|
| 219 | + /** |
| 220 | + * Close the web socket and send a close status code to client. |
| 221 | + * |
| 222 | + * This method fires a {@link OnClose#onClose(WebSocket, WebSocketCloseStatus)} callback. |
| 223 | + * |
| 224 | + * @param closeStatus Close status. |
| 225 | + * @return This websocket. |
| 226 | + */ |
| 227 | + @Nonnull WebSocket close(@Nonnull WebSocketCloseStatus closeStatus); |
136 | 228 | } |
0 commit comments