Skip to content

Commit 55d3923

Browse files
committed
WebSocket API: document public API
1 parent 93e971e commit 55d3923

File tree

7 files changed

+74
-11
lines changed

7 files changed

+74
-11
lines changed

jooby/src/main/java/io/jooby/Context.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ public interface Context extends Registry {
666666
*/
667667
@Nonnull Context detach(@Nonnull Route.Handler next) throws Exception;
668668

669-
default @Nonnull Context upgrade(@Nonnull WebSocket.Handler handler) {
669+
default @Nonnull Context upgrade(@Nonnull WebSocket.Initializer handler) {
670670
return null;
671671
}
672672

jooby/src/main/java/io/jooby/Jooby.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ public <T> Jooby mvc(@Nonnull Class<T> router, @Nonnull Provider<T> provider) {
292292
}
293293
}
294294

295-
public Route ws(String pattern, WebSocket.Handler handler) {
295+
public Route ws(String pattern, WebSocket.Initializer handler) {
296296
return router.ws(pattern, handler);
297297
}
298298

jooby/src/main/java/io/jooby/Router.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,14 @@ interface Match {
216216
*/
217217
@Nonnull Router mvc(@Nonnull Object router);
218218

219-
@Nonnull Route ws(@Nonnull String pattern, @Nonnull WebSocket.Handler handler);
219+
/**
220+
* Add a websocket handler.
221+
*
222+
* @param pattern WebSocket path pattern.
223+
* @param handler WebSocket handler.
224+
* @return This route.
225+
*/
226+
@Nonnull Route ws(@Nonnull String pattern, @Nonnull WebSocket.Initializer handler);
220227

221228
/**
222229
* Returns all routes.

jooby/src/main/java/io/jooby/WebSocket.java

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,73 @@
33
import javax.annotation.Nonnull;
44
import java.util.Map;
55

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+
*/
634
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);
947
}
1048

49+
/**
50+
* On connect callback.
51+
*/
1152
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);
1359
}
1460

61+
/**
62+
* On message callback. When a Message is send by a client, this callback allow you to
63+
* handle/react to it.
64+
*/
1565
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);
1773
}
1874

1975
interface OnClose {

jooby/src/main/java/io/jooby/internal/RouterImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ public Router encoder(@Nonnull MediaType contentType, @Nonnull MessageEncoder en
334334
return beanConverters;
335335
}
336336

337-
@Nonnull @Override public Route ws(@Nonnull String pattern, @Nonnull WebSocket.Handler handler) {
337+
@Nonnull @Override public Route ws(@Nonnull String pattern, @Nonnull WebSocket.Initializer handler) {
338338
return route(GET, pattern, new WebSocketHandler(handler));
339339
}
340340

jooby/src/main/java/io/jooby/internal/handler/WebSocketHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
import javax.annotation.Nonnull;
99

1010
public class WebSocketHandler implements Route.Handler {
11-
private WebSocket.Handler handler;
11+
private WebSocket.Initializer handler;
1212

13-
public WebSocketHandler(WebSocket.Handler handler) {
13+
public WebSocketHandler(WebSocket.Initializer handler) {
1414
this.handler = handler;
1515
}
1616

modules/jooby-netty/src/main/java/io/jooby/internal/netty/NettyContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ public NettyContext(ChannelHandlerContext ctx, HttpRequest req, Router router, S
267267
return this.cookies;
268268
}
269269

270-
@Nonnull @Override public Context upgrade(WebSocket.Handler handler) {
270+
@Nonnull @Override public Context upgrade(WebSocket.Initializer handler) {
271271
try {
272272
String webSocketURL = getProtocol() + "://" + req.headers().get(HttpHeaderNames.HOST) + path;
273273
WebSocketDecoderConfig config = WebSocketDecoderConfig.newBuilder()

0 commit comments

Comments
 (0)