Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Websocket bug: PING to server sends PING Frame instead of PONG Frame #5533

Merged
merged 3 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ static WebSocketFrame ofPong(byte[] data) {
if (data.length == 0) {
return EMPTY_PONG;
}
return new ByteArrayWebSocketFrame(data, WebSocketFrameType.PING);
return new ByteArrayWebSocketFrame(data, WebSocketFrameType.PONG);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.framing.BinaryFrame;
import org.java_websocket.framing.ContinuousFrame;
import org.java_websocket.framing.Framedata;
import org.java_websocket.framing.PingFrame;
import org.java_websocket.framing.TextFrame;
import org.java_websocket.handshake.ServerHandshake;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -96,6 +98,19 @@ void echoText() throws Exception {
await().until(() -> client.closeStatus().get() == WebSocketCloseStatus.NORMAL_CLOSURE.code());
}

@Test
void testPing() throws Exception {
final String pingContent = "pingContent";
final PingFrame pingFrame = new PingFrame();
pingFrame.setPayload(ByteBuffer.wrap(pingContent.getBytes(StandardCharsets.UTF_8)));

client.sendFrame(pingFrame);

final String pongContent = client.receivedMessages().poll(5, TimeUnit.SECONDS);

assertThat(pongContent).isEqualTo(pingContent);
}

@Test
void echoBinary() throws Exception {
final List<String> sendingMessages = ImmutableList.of("foobar", Strings.repeat("a", 3 * 1024), "baz");
Expand Down Expand Up @@ -240,8 +255,9 @@ public void onSubscribe(Subscription s) {

@Override
public void onNext(WebSocketFrame webSocketFrame) {
if (webSocketFrame.type() != WebSocketFrameType.PING &&
webSocketFrame.type() != WebSocketFrameType.PONG) {
if (webSocketFrame.type() == WebSocketFrameType.PING) {
writer.writePong(webSocketFrame.array());
} else if (webSocketFrame.type() != WebSocketFrameType.PONG) {
writer.write(webSocketFrame);
}
}
Expand Down