Skip to content

Commit

Permalink
#noissue when the notifications message processing takes long time (f…
Browse files Browse the repository at this point in the history
…ew 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
  • Loading branch information
schmiel-cumulocity committed Sep 27, 2024
1 parent 1034149 commit 5f68b90
Showing 1 changed file with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -43,13 +44,23 @@ public void onOpen(ServerHandshake serverHandshake) {

@Override
public void onMessage(String message) {
//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);
this.callback.onNotification(notification);
try {
callback.onNotification(notification);
acknowledge(notification);
} catch (RuntimeException e) {
log.error("Error processing message '{}'. Acknowledge will not be sent so the message will be resent in the future.", notification.getMessage());
}
}

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");
}
}

Expand Down

0 comments on commit 5f68b90

Please sign in to comment.