Skip to content

Commit

Permalink
Handle pong message correctly (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rakambda committed Jul 31, 2022
1 parent a1d2f5d commit 1d63faf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.java_websocket.WebSocket;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.framing.Framedata;
import org.java_websocket.framing.PongFrame;
import org.java_websocket.handshake.ServerHandshake;
import org.jetbrains.annotations.NotNull;
import java.net.URI;
Expand Down Expand Up @@ -57,6 +58,9 @@ public void onOpen(ServerHandshake serverHandshake){
public void onMessage(String messageStr){
try(var logContext = LogContext.empty().withSocketId(uuid)){
log.trace("Received Chat Websocket message: {}", messageStr.strip());
if(messageStr.startsWith("PONG :tmi.twitch.tv")){
onWebsocketPong(this, new PongFrame());
}
}
catch(Exception e){
log.error("Failed to handle Chat WebSocket message {}", messageStr, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import java.net.URI;
import java.time.Duration;
import java.time.Instant;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
Expand Down Expand Up @@ -95,6 +97,20 @@ void pongUpdatesHeartbeat(WebsocketMockServer server) throws InterruptedExceptio
assertThat(tested.getLastHeartbeat()).isAfter(now);
}

@Test
void pongUpdatesHeartbeat2(WebsocketMockServer server) throws InterruptedException{
tested.connectBlocking();
server.awaitMessage(3);
server.reset();

var now = Instant.now();
Thread.sleep(100);

server.send("PONG :tmi.twitch.tv");

await().atMost(Duration.ofSeconds(30)).until(() -> tested.getLastHeartbeat().isAfter(now));
}

@Test
void sendPing(WebsocketMockServer server) throws InterruptedException{
tested.connectBlocking();
Expand Down

0 comments on commit 1d63faf

Please sign in to comment.