-
-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathWebSocket.java
More file actions
482 lines (434 loc) · 11.3 KB
/
WebSocket.java
File metadata and controls
482 lines (434 loc) · 11.3 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Map;
import org.jspecify.annotations.Nullable;
import io.jooby.output.Output;
/**
* Websocket. Usage:
*
* <pre>{@code
* ws("/pattern", (ctx, configurer) -> {
* configurer.onConnect(ws -> {
* // Connect callback
* }):
*
* configurer.onMessage((ws, message) -> {
* ws.send("Got: " + message.value());
* });
*
* configurer.onClose((ws, closeStatus) -> {
* // Closing websocket
* });
*
* configurer.onError((ws, cause) -> {
*
* });
* });
*
* }</pre>
*
* @author edgar
* @since 2.2.0
*/
public interface WebSocket {
/**
* Websocket initializer. Give you access to a read-only {@link Context} you are free to access to
* request attributes, while attempt to modify a response results in exception.
*/
interface Initializer {
/**
* Callback with a readonly context and websocket configurer.
*
* @param ctx Readonly context.
* @param configurer WebSocket configurer.
*/
void init(Context ctx, WebSocketConfigurer configurer);
}
/** Web socket route handler. */
interface Handler extends Route.Handler {
/**
* Web socket initializer.
*
* @return Web socket initializer.
*/
Initializer getInitializer();
}
/** On connect callback. */
interface OnConnect {
/**
* On connect callback with recently created web socket.
*
* @param ws WebSocket.
*/
void onConnect(WebSocket ws);
}
/**
* On message callback. When a Message is send by a client, this callback allow you to
* handle/react to it.
*/
interface OnMessage {
/**
* Generated when a client send a message.
*
* @param ws WebSocket.
* @param message Client message.
*/
void onMessage(WebSocket ws, WebSocketMessage message);
}
/**
* On close callback. Generated when client close the connection or when explicit calls to {@link
* #close(WebSocketCloseStatus)}.
*/
interface OnClose {
/**
* Generated when client close the connection or when explicit calls to {@link
* #close(WebSocketCloseStatus)}.
*
* @param ws WebSocket.
* @param closeStatus Close status.
*/
void onClose(WebSocket ws, WebSocketCloseStatus closeStatus);
}
/** On error callback. Generated when unexpected error occurs. */
interface OnError {
/**
* Error callback, let you listen for exception. Websocket might or might not be open.
*
* @param ws Websocket.
* @param cause Cause.
*/
void onError(WebSocket ws, Throwable cause);
}
/** Callback for sending messages. */
interface WriteCallback {
/** NOOP callback. */
WriteCallback NOOP = (ws, cause) -> {};
/**
* Notify about message sent to client.
*
* @param ws Websocket.
* @param cause Error or <code>null</code> for success messages.
*/
void operationComplete(WebSocket ws, @Nullable Throwable cause);
}
/** Max message size for websocket (128K). */
int MAX_BUFFER_SIZE = 131072;
/**
* Originating HTTP context. Please note this is a read-only context, so you are not allowed to
* modify or produces a response from it.
*
* <p>The context let give you access to originating request (then one that was upgrade it).
*
* @return Read-only originating HTTP request.
*/
Context getContext();
/**
* Context attributes (a.k.a request attributes).
*
* @return Context attributes.
*/
default Map<String, Object> getAttributes() {
return getContext().getAttributes();
}
/**
* Get an attribute by his key. This is just an utility method around {@link #getAttributes()}.
* This method look first in current context and fallback to application attributes.
*
* @param key Attribute key.
* @param <T> Attribute type.
* @return Attribute value.
*/
default <T> T attribute(String key) {
return getContext().getAttribute(key);
}
/**
* Set an application attribute.
*
* @param key Attribute key.
* @param value Attribute value.
* @return This router.
*/
default WebSocket attribute(String key, Object value) {
getContext().setAttribute(key, value);
return this;
}
/**
* Web sockets connected to the same path. This method doesn't include the current websocket.
*
* @return Web sockets or empty list.
*/
List<WebSocket> getSessions();
/**
* True if websocket is open.
*
* @return True when open.
*/
boolean isOpen();
/**
* For each of live sessions (including this) do something with it.
*
* <pre>{@code
* Broadcast example:
*
* ws.forEach(session -> {
* session.send("Message");
* });
*
* }</pre>
*
* @param callback Callback.
*/
void forEach(SneakyThrows.Consumer<WebSocket> callback);
/**
* Send a ping message to client.
*
* @param message Text Message.
* @return This websocket.
*/
default WebSocket sendPing(String message) {
return sendPing(message, WriteCallback.NOOP);
}
/**
* Send a ping message to client.
*
* @param message Text Message.
* @param callback Write callback.
* @return This websocket.
*/
WebSocket sendPing(String message, WriteCallback callback);
/**
* Send a ping message to client.
*
* @param message Text Message.
* @return This websocket.
*/
default WebSocket sendPing(byte[] message) {
return sendPing(message, WriteCallback.NOOP);
}
/**
* Send a ping message to client.
*
* @param message Text Message.
* @param callback Write callback.
* @return This websocket.
*/
default WebSocket sendPing(byte[] message, WriteCallback callback) {
return sendPing(ByteBuffer.wrap(message), callback);
}
/**
* Send a ping message to client.
*
* @param message Text message.
* @return This instance.
*/
default WebSocket sendPing(ByteBuffer message) {
return sendPing(message, WriteCallback.NOOP);
}
/**
* Send a ping message to client.
*
* @param message Text message.
* @param callback Write callback.
* @return This instance.
*/
WebSocket sendPing(ByteBuffer message, WriteCallback callback);
/**
* Send a text message to client.
*
* @param message Text Message.
* @return This websocket.
*/
default WebSocket send(String message) {
return send(message, WriteCallback.NOOP);
}
/**
* Send a text message to client.
*
* @param message Text Message.
* @param callback Write callback.
* @return This websocket.
*/
WebSocket send(String message, WriteCallback callback);
/**
* Send a text message to client.
*
* @param message Text Message.
* @return This websocket.
*/
default WebSocket send(byte[] message) {
return send(message, WriteCallback.NOOP);
}
/**
* Send a text message to client.
*
* @param message Text Message.
* @param callback Write callback.
* @return This websocket.
*/
default WebSocket send(byte[] message, WriteCallback callback) {
return send(ByteBuffer.wrap(message), callback);
}
/**
* Send a text message to client.
*
* @param message Text message.
* @return This instance.
*/
default WebSocket send(ByteBuffer message) {
return send(message, WriteCallback.NOOP);
}
/**
* Send a text message to client.
*
* @param message Text message.
* @param callback Write callback.
* @return This instance.
*/
WebSocket send(ByteBuffer message, WriteCallback callback);
/**
* Send a text message to client.
*
* @param message Text message.
* @return This instance.
*/
default WebSocket send(Output message) {
return send(message, WriteCallback.NOOP);
}
/**
* Send a text message to client.
*
* @param message Text message.
* @param callback Write callback.
* @return This instance.
*/
WebSocket send(Output message, WriteCallback callback);
/**
* Send a binary message to client.
*
* @param message Binary Message.
* @return This websocket.
*/
default WebSocket sendBinary(String message) {
return sendBinary(message, WriteCallback.NOOP);
}
/**
* Send a binary message to client.
*
* @param message Binary Message.
* @param callback Write callback.
* @return This websocket.
*/
WebSocket sendBinary(String message, WriteCallback callback);
/**
* Send a binary message to client.
*
* @param message Binary Message.
* @return This websocket.
*/
default WebSocket sendBinary(byte[] message) {
return sendBinary(message, WriteCallback.NOOP);
}
/**
* Send a binary message to client.
*
* @param message Binary Message.
* @param callback Write callback.
* @return This websocket.
*/
default WebSocket sendBinary(byte[] message, WriteCallback callback) {
return sendBinary(ByteBuffer.wrap(message), callback);
}
/**
* Send a binary message to client.
*
* @param message Binary message.
* @return This instance.
*/
default WebSocket sendBinary(ByteBuffer message) {
return sendBinary(message, WriteCallback.NOOP);
}
/**
* Send a binary message to client.
*
* @param message Binary message.
* @param callback Write callback.
* @return This instance.
*/
WebSocket sendBinary(ByteBuffer message, WriteCallback callback);
/**
* Send a binary message to client.
*
* @param message Binary message.
* @return This instance.
*/
default WebSocket sendBinary(Output message) {
return sendBinary(message, WriteCallback.NOOP);
}
/**
* Send a binary message to client.
*
* @param message Binary message.
* @param callback Write callback.
* @return This instance.
*/
WebSocket sendBinary(Output message, WriteCallback callback);
/**
* Encode a value and send a text message to client.
*
* @param value Value to send.
* @return This websocket.
*/
default WebSocket render(Object value) {
return render(value, WriteCallback.NOOP);
}
/**
* Encode a value and send a text message to client.
*
* @param value Value to send.
* @param callback Write callback.
* @return This websocket.
*/
WebSocket render(Object value, WriteCallback callback);
/**
* Encode a value and send a binary message to client.
*
* @param value Value to send.
* @return This websocket.
*/
default WebSocket renderBinary(Object value) {
return renderBinary(value, WriteCallback.NOOP);
}
/**
* Encode a value and send a binary message to client.
*
* @param value Value to send.
* @param callback Write callback.
* @return This websocket.
*/
WebSocket renderBinary(Object value, WriteCallback callback);
/**
* Close the web socket and send a {@link WebSocketCloseStatus#NORMAL} code to client.
*
* <p>This method fires a {@link OnClose#onClose(WebSocket, WebSocketCloseStatus)} callback.
*
* @return This websocket.
*/
default WebSocket close() {
return close(WebSocketCloseStatus.NORMAL);
}
/**
* Close the web socket and send a close status code to client.
*
* <p>This method fires a {@link OnClose#onClose(WebSocket, WebSocketCloseStatus)} callback.
*
* @param closeStatus Close status.
* @return This websocket.
*/
WebSocket close(WebSocketCloseStatus closeStatus);
}