From bf33363e8d19721bb61957b924b3d43a904c298d Mon Sep 17 00:00:00 2001 From: Slawomir Chmiel Date: Wed, 25 Sep 2024 13:32:35 +0200 Subject: [PATCH] #noissue when the notifications message processing takes long time (few seconds up to 1 minute) PONG responses can be delayed as the socket is blocked, apply workaround in the TooTallNate client to address this issue --- .../tootallnate/TooTallNateWebSocketClient.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/notification2-examples/notification2-example-client/src/main/java/c8y/example/notification/client/websocket/tootallnate/TooTallNateWebSocketClient.java b/notification2-examples/notification2-example-client/src/main/java/c8y/example/notification/client/websocket/tootallnate/TooTallNateWebSocketClient.java index 547cb6809d..2315029ea6 100644 --- a/notification2-examples/notification2-example-client/src/main/java/c8y/example/notification/client/websocket/tootallnate/TooTallNateWebSocketClient.java +++ b/notification2-examples/notification2-example-client/src/main/java/c8y/example/notification/client/websocket/tootallnate/TooTallNateWebSocketClient.java @@ -4,6 +4,7 @@ import c8y.example.notification.client.websocket.NotificationCallback; import c8y.example.notification.client.websocket.WebSocketClient; import lombok.extern.slf4j.Slf4j; +import org.java_websocket.WebSocketImpl; import org.java_websocket.handshake.ServerHandshake; import java.net.URI; @@ -43,13 +44,23 @@ public void onOpen(ServerHandshake serverHandshake) { @Override public void onMessage(String message) { - Notification notification = Notification.parse(message); - this.callback.onNotification(notification); + try { + //Recommended workaround for client issue that can start disconnection if the PONG response are delayed because of busy socket (the RFC 6455 specification does not require the client to send Ping frames if it is constantly receiving messages from the server). + //((WebSocketImpl) getConnection()).updateLastPong(); + Notification notification = Notification.parse(message); + callback.onNotification(notification); + acknowledge(notification); + } catch (Throwable e) { + log.error("Error processing message '{}'. Acknowledge will not be sent so the message will be resent in the future.", message); + } + } + + private void acknowledge(final Notification notification) { if (notification.getAckHeader() != null) { // Best Practice: Acknowledge notifications as soon as possible. send(notification.getAckHeader()); // ack message } else { - throw new RuntimeException("No message id found for ack"); + log.error("Invalid message without ACK header, message will not be acknowledged"); } }