Skip to content

Reset lastPong in onOpen #1204

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

Merged
merged 1 commit into from
Dec 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/org/java_websocket/WebSocketImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,7 @@ private void write(List<ByteBuffer> bufs) {
private void open(Handshakedata d) {
log.trace("open using draft: {}", draft);
readyState = ReadyState.OPEN;
updateLastPong();
try {
wsl.onWebsocketOpen(this, d);
} catch (RuntimeException e) {
Expand Down
95 changes: 95 additions & 0 deletions src/test/java/org/java_websocket/issues/Issue1203Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package org.java_websocket.issues;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.net.InetSocketAddress;
import java.net.URI;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.CountDownLatch;
import org.java_websocket.WebSocket;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.handshake.ServerHandshake;
import org.java_websocket.server.WebSocketServer;
import org.java_websocket.util.SocketUtil;
import org.junit.Assert;
import org.junit.Test;

public class Issue1203Test {
private final CountDownLatch countServerDownLatch = new CountDownLatch(1);
private final CountDownLatch countClientDownLatch = new CountDownLatch(1);
boolean isClosedCalled = false;
@Test(timeout = 50000)
public void testIssue() throws Exception {
int port = SocketUtil.getAvailablePort();
WebSocketServer server = new WebSocketServer(new InetSocketAddress(port)) {
@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {
}

@Override
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
}

@Override
public void onMessage(WebSocket conn, String message) {
}

@Override
public void onError(WebSocket conn, Exception ex) {
}

@Override
public void onStart() {
countServerDownLatch.countDown();
}
};
final WebSocketClient client = new WebSocketClient(new URI("ws://localhost:" + port)) {
@Override
public void onOpen(ServerHandshake handshakedata) {
countClientDownLatch.countDown();
}

@Override
public void onMessage(String message) {

}

@Override
public void onClose(int code, String reason, boolean remote) {
isClosedCalled = true;
}

@Override
public void onError(Exception ex) {

}
};

server.setConnectionLostTimeout(10);
server.start();
countServerDownLatch.await();

client.setConnectionLostTimeout(10);
Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
try {
client.connectBlocking();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
timer.schedule(task, 15000);
countClientDownLatch.await();
Thread.sleep(30000);
Assert.assertFalse(isClosedCalled);
client.closeBlocking();
server.stop();
}
}