Skip to content

Commit 262e46c

Browse files
feat: Refactor WebSocketPushSender to improve handler retrieval and add session check
Signed-off-by: Mario Serrano <mario@dynamiasoluciones.com>
1 parent d6225b4 commit 262e46c

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static boolean sendPushCommand(Desktop desktop, String command) {
5858
* @return true if command is sended successfull. Check log for false response
5959
*/
6060
public static boolean sendPushCommand(Desktop desktop, String command, Map<String, Object> payload) {
61-
WebSocketGlobalCommandHandler handler = Containers.get().findObject(WebSocketGlobalCommandHandler.class);
61+
WebSocketGlobalCommandHandler handler = getHandler();
6262
if (handler != null) {
6363
try {
6464
Map<String, Object> data = new HashMap<>();
@@ -99,7 +99,7 @@ public static boolean sendPushCommand(String command) {
9999
*
100100
*/
101101
public static void broadcastCommand(String command) {
102-
WebSocketGlobalCommandHandler handler = Containers.get().findObject(WebSocketGlobalCommandHandler.class);
102+
WebSocketGlobalCommandHandler handler = getHandler();
103103
if (handler != null) {
104104
handler.getAllSessions().forEach(s -> {
105105
try {
@@ -111,8 +111,12 @@ public static void broadcastCommand(String command) {
111111
}
112112
}
113113

114+
private static WebSocketGlobalCommandHandler getHandler() {
115+
return Containers.get().findObject(WebSocketGlobalCommandHandler.class);
116+
}
117+
114118
/**
115-
* Init WS connection with client
119+
* Initialize WebSocket connection on client side by requesting it via Clients.evalJavaScript
116120
*/
117121
public static void initWS() {
118122
if (ZKUtil.isInEventListener()) {
@@ -124,4 +128,17 @@ public static void initWS() {
124128
}
125129
}
126130
}
131+
132+
/**
133+
* Check if desktop has an open WS session, if not try to init WS connection
134+
*/
135+
public static void checkDesktop(Desktop desktop) {
136+
var handler = getHandler();
137+
if (handler != null) {
138+
var session = handler.findSession(desktop);
139+
if (session == null || !session.isOpen()) {
140+
initWS();
141+
}
142+
}
143+
}
127144
}

0 commit comments

Comments
 (0)