Skip to content

Commit afa1ab1

Browse files
feat: Refactor WebSocket configuration and initialization to enhance endpoint handling and logging
Signed-off-by: Mario Serrano <mario@dynamiasoluciones.com>
1 parent 6c9a874 commit afa1ab1

3 files changed

Lines changed: 10 additions & 11 deletions

File tree

zk/src/main/java/tools/dynamia/zk/websocket/WebSocketGlobalCommandHandler.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ protected void handleTextMessage(WebSocketSession session, TextMessage message)
116116
public void afterConnectionEstablished(WebSocketSession session) {
117117
log("WebSocket connection established: " + session.getId());
118118
sessions.put(session.getId(), session);
119-
session.get
120119
}
121120

122121
/**

zk/src/main/java/tools/dynamia/zk/websocket/WebSocketPushSender.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ public static void initWS() {
122122
if (ZKUtil.isInEventListener()) {
123123
try {
124124
LOGGER.info("Requesting to initialize Websocket connection");
125-
Clients.evalJavaScript("DynamiaToolsWS.init('/ws-commands');");
125+
var config = Containers.get().findObject(ZKWebSocketConfigurer.class);
126+
var endpoint = config != null ? config.getEndpoint() : "/ws-commands";
127+
Clients.evalJavaScript("DynamiaToolsWS.init('" + endpoint + "');");
126128
} catch (Exception e) {
127129
//
128130
}

zk/src/main/java/tools/dynamia/zk/websocket/ZKWebSocketConfigurer.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
2222
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
2323
import org.springframework.web.socket.server.support.DefaultHandshakeHandler;
24-
import tools.dynamia.commons.logger.LoggingService;
25-
import tools.dynamia.commons.logger.SLF4JLoggingService;
24+
import tools.dynamia.commons.logger.Loggable;
2625

2726

2827
/**
@@ -34,15 +33,15 @@
3433
* <li>{@link WebSocketGlobalCommandHandler}</li>
3534
* <li>{@link DefaultHandshakeHandler}</li>
3635
* </ul>
37-
*
36+
* <p>
3837
* Usage:
3938
* <ul>
4039
* <li>Extend this class in your own {@code @Configuration} to customize endpoint,
4140
* allowed origins or handshake behavior.</li>
4241
* <li>Or import it from another {@code @Configuration} class using
4342
* {@code @Import(ZKWebSocketConfigurer.class)} to reuse the default configuration.</li>
4443
* </ul>
45-
*
44+
* <p>
4645
* Important: When using this class as a Spring configuration (either by extending it
4746
* or importing it), the hosting configuration must enable WebSocket support by being
4847
* annotated with {@code @EnableWebSocket}. Without {@code @EnableWebSocket} Spring
@@ -63,25 +62,24 @@
6362
* @Import(ZKWebSocketConfigurer.class)
6463
* public class AppConfig { }
6564
* }</pre>
66-
*
65+
* <p>
6766
* This class is intentionally lightweight and provides sensible defaults. Override
6867
* or extend behaviour when a project needs different endpoint paths, CORS rules
6968
* or a custom handshake handler.
7069
*/
71-
public class ZKWebSocketConfigurer implements WebSocketConfigurer {
70+
public class ZKWebSocketConfigurer implements WebSocketConfigurer, Loggable {
7271

7372
private String endpoint = "/ws-commands";
7473
private String[] allowedOrigins = new String[]{"*"};
7574
private String[] allowedOriginPatterns = null;
7675

77-
private final LoggingService logger = new SLF4JLoggingService(ZKWebSocketConfigurer.class);
7876

7977
/**
8078
* Creates a new ZKWebSocketConfigurer with default configuration values.
8179
* A startup message is logged when an instance is created.
8280
*/
8381
public ZKWebSocketConfigurer() {
84-
logger.info("Starting " + getClass());
82+
log("Starting " + getClass());
8583
}
8684

8785
/**
@@ -93,7 +91,7 @@ public ZKWebSocketConfigurer() {
9391
*/
9492
@Override
9593
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
96-
logger.info("Registering WS Handler for ZK Commands");
94+
log("Registering WS Handler for ZK Commands");
9795
var reg = registry.addHandler(globalCommandHandler(), endpoint != null ? endpoint : "/ws-commands");
9896
if (allowedOrigins != null) {
9997
reg.setAllowedOrigins(allowedOrigins);

0 commit comments

Comments
 (0)