Skip to content

Commit

Permalink
WebSockets Next: Dev UI fixes
Browse files Browse the repository at this point in the history
- a dev mode build should not fail if no server endpoint is defined
- fix Server Endpoints static label

(cherry picked from commit 37b1fb9)
  • Loading branch information
mkouba authored and gsmet committed Jul 9, 2024
1 parent 114807c commit dc5e281
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void pages(List<WebSocketEndpointBuildItem> endpoints, List<GeneratedEndp
.title("Server Endpoints")
.icon("font-awesome-solid:plug")
.componentLink("qwc-wsn-endpoints.js")
.staticLabel(String.valueOf(endpoints.size())));
.staticLabel(String.valueOf(endpoints.stream().filter(WebSocketEndpointBuildItem::isServer).count())));

cardPages.produce(pageBuildItem);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.quarkus.websockets.next.test.devmode;

import static org.junit.jupiter.api.Assertions.assertEquals;

import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusDevModeTest;
import io.quarkus.websockets.next.BasicWebSocketConnector;
import io.restassured.RestAssured;
import io.vertx.ext.web.Router;

/**
* Just to make sure the Dev UI is not broken if no server endpoint is defined.
*/
public class NoServerEndpointDevModeTest {

@RegisterExtension
static final QuarkusDevModeTest testConfig = new QuarkusDevModeTest()
.withApplicationRoot(root -> root
.addClass(MyBean.class));

@Test
public void testConnectorIsInjected() {
assertEquals("1", RestAssured.get("mybeantest").then().statusCode(200).extract().body().asString());
}

@Singleton
public static class MyBean {

@Inject
BasicWebSocketConnector connector;

void addRoute(@Observes Router router) {
router.get("/mybeantest").handler(rc -> {
rc.end(connector != null ? "1" : "0");
});
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.concurrent.ConcurrentMap;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Instance;

import org.jboss.logging.Logger;

Expand Down Expand Up @@ -53,16 +54,18 @@ public class WebSocketNextJsonRPCService implements ConnectionListener {

private final WebSocketsServerRuntimeConfig.DevMode devModeConfig;

WebSocketNextJsonRPCService(ConnectionManager connectionManager, Vertx vertx, HttpConfiguration httpConfig,
WebSocketNextJsonRPCService(Instance<ConnectionManager> connectionManager, Vertx vertx, HttpConfiguration httpConfig,
WebSocketsServerRuntimeConfig config) {
this.connectionStatus = BroadcastProcessor.create();
this.connectionMessages = BroadcastProcessor.create();
this.connectionManager = connectionManager;
this.connectionManager = connectionManager.isResolvable() ? connectionManager.get() : null;
this.vertx = vertx;
this.httpConfig = httpConfig;
this.devModeConfig = config.devMode();
this.sockets = new ConcurrentHashMap<>();
connectionManager.addListener(this);
if (this.connectionManager != null) {
this.connectionManager.addListener(this);
}
}

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

public JsonObject getConnections(List<String> endpoints) {
JsonObject json = new JsonObject();
for (String endpoint : endpoints) {
List<WebSocketConnection> connections = new ArrayList<>(connectionManager.getConnections(endpoint));
connections.sort(Comparator.comparing(WebSocketConnection::creationTime));
JsonArray array = new JsonArray();
for (WebSocketConnection c : connections) {
array.add(toJsonObject(endpoint, c));
if (connectionManager != null) {
for (String endpoint : endpoints) {
List<WebSocketConnection> connections = new ArrayList<>(connectionManager.getConnections(endpoint));
connections.sort(Comparator.comparing(WebSocketConnection::creationTime));
JsonArray array = new JsonArray();
for (WebSocketConnection c : connections) {
array.add(toJsonObject(endpoint, c));
}
json.put(endpoint, array);
}
json.put(endpoint, array);
}
json.put("connectionMessagesLimit", devModeConfig.connectionMessagesLimit());
return json;
Expand All @@ -104,6 +109,9 @@ public JsonArray getMessages(String connectionKey) {
}

public Uni<JsonObject> openDevConnection(String path, String endpointPath) {
if (connectionManager == null) {
return failureUni();
}
if (isInvalidPath(path, endpointPath)) {
LOG.errorf("Invalid path %s; original endpoint path %s", path, endpointPath);
return failureUni();
Expand Down Expand Up @@ -179,6 +187,9 @@ private static String normalize(String path) {
}

public Uni<JsonObject> closeDevConnection(String connectionKey) {
if (connectionManager == null) {
return failureUni();
}
DevWebSocket socket = sockets.remove(connectionKey);
if (socket != null) {
Uni<Void> uni = UniHelper.toUni(socket.socket.close());
Expand Down

0 comments on commit dc5e281

Please sign in to comment.