Skip to content

Commit 4a1995b

Browse files
committed
WebSocket API: Jetty implementation
1 parent b444907 commit 4a1995b

18 files changed

Lines changed: 410 additions & 70 deletions

File tree

examples/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
</dependency>
3737
<dependency>
3838
<groupId>io.jooby</groupId>
39-
<artifactId>jooby-utow</artifactId>
39+
<artifactId>jooby-jetty</artifactId>
4040
<version>${jooby.version}</version>
4141
</dependency>
4242
<dependency>

examples/src/main/java/examples/WebSocketApp.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,19 @@ public class WebSocketApp extends Jooby {
2222
ws.send("Welcome");
2323
});
2424
initializer.onMessage((ws, msg) -> {
25-
ws.send("Got: " + msg.value(), true);
25+
throw new UnsupportedOperationException("OnMessage");
26+
// ws.send("Got: " + msg.value());
2627
});
2728
initializer.onClose((ws, closeStatus) -> {
2829
System.out.println("Closed " + closeStatus);
2930
});
3031

3132
initializer.onError((ws, cause) -> {
32-
ws.getContext().getRouter().getLog().error("error ", cause);
33+
if (ws.isOpen()) {
34+
ws.send("error: " + cause.getMessage() );
35+
} else {
36+
ws.getContext().getRouter().getLog().error("error ", cause);
37+
}
3338
});
3439

3540
});

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -666,9 +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.Initializer handler) {
670-
return null;
671-
}
669+
@Nonnull Context upgrade(@Nonnull WebSocket.Initializer handler);
672670

673671
/*
674672
* **********************************************************************************************

jooby/src/main/java/io/jooby/ForwardingContext.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,11 @@ public ForwardingContext(@Nonnull Context context) {
289289
return this;
290290
}
291291

292+
@Nonnull @Override public Context upgrade(@Nonnull WebSocket.Initializer handler) {
293+
ctx.upgrade(handler);
294+
return this;
295+
}
296+
292297
@Nonnull @Override public Context setResponseHeader(@Nonnull String name, @Nonnull Date value) {
293298
ctx.setResponseHeader(name, value);
294299
return this;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ interface Match {
8080
List<String> METHODS = unmodifiableList(
8181
asList(GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS, TRACE));
8282

83+
/** Web socket. */
84+
String WS = "WS";
85+
8386
/**
8487
* Mutable map of application attributes.
8588
*

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ interface OnError {
103103
void onError(@Nonnull WebSocket ws, @Nonnull Throwable cause);
104104
}
105105

106+
/** Max message size for websocket (13k). */
107+
int MAX_BUFFER_SIZE = 131072;
108+
106109
/**
107110
* Originating HTTP context. Please note this is a read-only context, so you are not allowed
108111
* to modify or produces a response from it.

jooby/src/main/java/io/jooby/WebSocketCloseStatus.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.jooby;
22

3-
import javax.annotation.Nonnull;
43
import javax.annotation.Nullable;
54
import java.util.Optional;
65

@@ -195,9 +194,11 @@ public class WebSocketCloseStatus {
195194
* @param code Status code.
196195
* @param reason Reason.
197196
*/
198-
public WebSocketCloseStatus(int code, @Nonnull String reason) {
197+
public WebSocketCloseStatus(int code, @Nullable String reason) {
199198
this.code = code;
200-
this.reason = reason;
199+
if (reason != null && reason.length() > 0) {
200+
this.reason = reason;
201+
}
201202
}
202203

203204
/**

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ public Router encoder(@Nonnull MediaType contentType, @Nonnull MessageEncoder en
335335
}
336336

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

341341
@Override
@@ -389,13 +389,20 @@ private Route defineRoute(@Nonnull String method, @Nonnull String pattern,
389389
String routePattern = normalizePath(basePath == null
390390
? safePattern
391391
: basePath + safePattern, false, true);
392-
tree.insert(route.getMethod(), routePattern, route);
393-
if (route.isHttpOptions()) {
394-
tree.insert(Router.OPTIONS, routePattern, route);
395-
} else if (route.isHttpTrace()) {
396-
tree.insert(Router.TRACE, routePattern, route);
397-
} else if (route.isHttpHead() && route.getMethod().equals(GET)) {
398-
tree.insert(Router.HEAD, routePattern, route);
392+
393+
if (route.getMethod().equals(WS)) {
394+
tree.insert(GET, routePattern, route);
395+
route.setReturnType(Context.class);
396+
} else {
397+
tree.insert(route.getMethod(), routePattern, route);
398+
399+
if (route.isHttpOptions()) {
400+
tree.insert(Router.OPTIONS, routePattern, route);
401+
} else if (route.isHttpTrace()) {
402+
tree.insert(Router.TRACE, routePattern, route);
403+
} else if (route.isHttpHead() && route.getMethod().equals(GET)) {
404+
tree.insert(Router.HEAD, routePattern, route);
405+
}
399406
}
400407
routes.add(route);
401408

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ public WebSocketHandler(WebSocket.Initializer handler) {
1818
boolean webSocket = ctx.header("Upgrade").value("")
1919
.equalsIgnoreCase("WebSocket");
2020
if (webSocket) {
21-
return ctx.upgrade(handler);
22-
} else {
21+
ctx.upgrade(handler);
22+
}
23+
if (!ctx.isResponseStarted()) {
2324
return ctx.send(StatusCode.NOT_FOUND);
2425
}
26+
return ctx;
2527
}
2628
}

modules/jooby-jetty/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
<artifactId>jetty-server</artifactId>
3232
</dependency>
3333

34+
<dependency>
35+
<groupId>org.eclipse.jetty.websocket</groupId>
36+
<artifactId>websocket-server</artifactId>
37+
</dependency>
38+
3439
<!-- Test dependencies -->
3540
<dependency>
3641
<groupId>org.junit.jupiter</groupId>

0 commit comments

Comments
 (0)