Skip to content

Commit dc5e281

Browse files
mkoubagsmet
authored andcommitted
WebSockets Next: Dev UI fixes
- a dev mode build should not fail if no server endpoint is defined - fix Server Endpoints static label (cherry picked from commit 37b1fb9)
1 parent 114807c commit dc5e281

File tree

3 files changed

+68
-11
lines changed

3 files changed

+68
-11
lines changed

extensions/websockets-next/deployment/src/main/java/io/quarkus/websockets/next/deployment/devui/WebSocketServerDevUIProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void pages(List<WebSocketEndpointBuildItem> endpoints, List<GeneratedEndp
4040
.title("Server Endpoints")
4141
.icon("font-awesome-solid:plug")
4242
.componentLink("qwc-wsn-endpoints.js")
43-
.staticLabel(String.valueOf(endpoints.size())));
43+
.staticLabel(String.valueOf(endpoints.stream().filter(WebSocketEndpointBuildItem::isServer).count())));
4444

4545
cardPages.produce(pageBuildItem);
4646
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.quarkus.websockets.next.test.devmode;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import jakarta.enterprise.event.Observes;
6+
import jakarta.inject.Inject;
7+
import jakarta.inject.Singleton;
8+
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.api.extension.RegisterExtension;
11+
12+
import io.quarkus.test.QuarkusDevModeTest;
13+
import io.quarkus.websockets.next.BasicWebSocketConnector;
14+
import io.restassured.RestAssured;
15+
import io.vertx.ext.web.Router;
16+
17+
/**
18+
* Just to make sure the Dev UI is not broken if no server endpoint is defined.
19+
*/
20+
public class NoServerEndpointDevModeTest {
21+
22+
@RegisterExtension
23+
static final QuarkusDevModeTest testConfig = new QuarkusDevModeTest()
24+
.withApplicationRoot(root -> root
25+
.addClass(MyBean.class));
26+
27+
@Test
28+
public void testConnectorIsInjected() {
29+
assertEquals("1", RestAssured.get("mybeantest").then().statusCode(200).extract().body().asString());
30+
}
31+
32+
@Singleton
33+
public static class MyBean {
34+
35+
@Inject
36+
BasicWebSocketConnector connector;
37+
38+
void addRoute(@Observes Router router) {
39+
router.get("/mybeantest").handler(rc -> {
40+
rc.end(connector != null ? "1" : "0");
41+
});
42+
}
43+
44+
}
45+
46+
}

extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/devui/WebSocketNextJsonRPCService.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.concurrent.ConcurrentMap;
1313

1414
import jakarta.enterprise.context.ApplicationScoped;
15+
import jakarta.enterprise.inject.Instance;
1516

1617
import org.jboss.logging.Logger;
1718

@@ -53,16 +54,18 @@ public class WebSocketNextJsonRPCService implements ConnectionListener {
5354

5455
private final WebSocketsServerRuntimeConfig.DevMode devModeConfig;
5556

56-
WebSocketNextJsonRPCService(ConnectionManager connectionManager, Vertx vertx, HttpConfiguration httpConfig,
57+
WebSocketNextJsonRPCService(Instance<ConnectionManager> connectionManager, Vertx vertx, HttpConfiguration httpConfig,
5758
WebSocketsServerRuntimeConfig config) {
5859
this.connectionStatus = BroadcastProcessor.create();
5960
this.connectionMessages = BroadcastProcessor.create();
60-
this.connectionManager = connectionManager;
61+
this.connectionManager = connectionManager.isResolvable() ? connectionManager.get() : null;
6162
this.vertx = vertx;
6263
this.httpConfig = httpConfig;
6364
this.devModeConfig = config.devMode();
6465
this.sockets = new ConcurrentHashMap<>();
65-
connectionManager.addListener(this);
66+
if (this.connectionManager != null) {
67+
this.connectionManager.addListener(this);
68+
}
6669
}
6770

6871
public Multi<JsonObject> connectionStatus() {
@@ -75,14 +78,16 @@ public Multi<JsonObject> connectionMessages() {
7578

7679
public JsonObject getConnections(List<String> endpoints) {
7780
JsonObject json = new JsonObject();
78-
for (String endpoint : endpoints) {
79-
List<WebSocketConnection> connections = new ArrayList<>(connectionManager.getConnections(endpoint));
80-
connections.sort(Comparator.comparing(WebSocketConnection::creationTime));
81-
JsonArray array = new JsonArray();
82-
for (WebSocketConnection c : connections) {
83-
array.add(toJsonObject(endpoint, c));
81+
if (connectionManager != null) {
82+
for (String endpoint : endpoints) {
83+
List<WebSocketConnection> connections = new ArrayList<>(connectionManager.getConnections(endpoint));
84+
connections.sort(Comparator.comparing(WebSocketConnection::creationTime));
85+
JsonArray array = new JsonArray();
86+
for (WebSocketConnection c : connections) {
87+
array.add(toJsonObject(endpoint, c));
88+
}
89+
json.put(endpoint, array);
8490
}
85-
json.put(endpoint, array);
8691
}
8792
json.put("connectionMessagesLimit", devModeConfig.connectionMessagesLimit());
8893
return json;
@@ -104,6 +109,9 @@ public JsonArray getMessages(String connectionKey) {
104109
}
105110

106111
public Uni<JsonObject> openDevConnection(String path, String endpointPath) {
112+
if (connectionManager == null) {
113+
return failureUni();
114+
}
107115
if (isInvalidPath(path, endpointPath)) {
108116
LOG.errorf("Invalid path %s; original endpoint path %s", path, endpointPath);
109117
return failureUni();
@@ -179,6 +187,9 @@ private static String normalize(String path) {
179187
}
180188

181189
public Uni<JsonObject> closeDevConnection(String connectionKey) {
190+
if (connectionManager == null) {
191+
return failureUni();
192+
}
182193
DevWebSocket socket = sockets.remove(connectionKey);
183194
if (socket != null) {
184195
Uni<Void> uni = UniHelper.toUni(socket.socket.close());

0 commit comments

Comments
 (0)