Skip to content

Commit ee40a5c

Browse files
committed
Check for pong response
1 parent dfc40a5 commit ee40a5c

3 files changed

Lines changed: 33 additions & 3 deletions

File tree

‎src/main/java/org/java_websocket/WebSocket.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ public enum READYSTATE {
3131
*/
3232
public static final int DEFAULT_PORT = 80;
3333

34+
/**
35+
* The default wss port of WebSockets, as defined in the spec. If the nullary
36+
* constructor is used, DEFAULT_WSS_PORT will be the port the WebSocketServer
37+
* is binded to. Note that ports under 1024 usually require root permissions.
38+
*/
3439
public static final int DEFAULT_WSS_PORT = 443;
3540

3641
/**

‎src/main/java/org/java_websocket/WebSocketAdapter.java‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,20 @@ protected void startConnectionLostTimer() {
9191
connectionLostTimerTask = new TimerTask() {
9292
@Override
9393
public void run() {
94-
for (WebSocket conn: connections()) {
95-
conn.sendPing();
94+
Collection<WebSocket> con = connections();
95+
synchronized ( con ) {
96+
long current = (System.currentTimeMillis()-(connectionLostTimeout * 1500));
97+
for( WebSocket conn : con ) {
98+
if (conn instanceof WebSocketImpl) {
99+
if( ((WebSocketImpl)conn).getLastPong() < current ) {
100+
if (WebSocketImpl.DEBUG)
101+
System.out.println("Closing connection due to no pong received: " + conn.toString());
102+
conn.close( CloseFrame.ABNORMAL_CLOSE );
103+
} else {
104+
conn.sendPing();
105+
}
106+
}
107+
}
96108
}
97109
}
98110
};
@@ -218,5 +230,4 @@ public String getFlashPolicy( WebSocket conn ) throws InvalidDataException {
218230
return "<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"" + adr.getPort() + "\" /></cross-domain-policy>\0";
219231
}
220232

221-
222233
}

‎src/main/java/org/java_websocket/WebSocketImpl.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ public class WebSocketImpl implements WebSocket {
9797

9898
private String resourceDescriptor = null;
9999

100+
/**
101+
* Attribute, when the last pong was recieved
102+
*/
103+
private long lastPong = System.currentTimeMillis();
104+
100105
/**
101106
* Creates a websocket with server role
102107
*
@@ -349,6 +354,7 @@ private void decodeFrames( ByteBuffer socketBuffer ) {
349354
wsl.onWebsocketPing( this, f );
350355
continue;
351356
} else if( curop == Opcode.PONG ) {
357+
lastPong = System.currentTimeMillis();
352358
wsl.onWebsocketPong( this, f );
353359
continue;
354360
} else if( !fin || curop == Opcode.CONTINUOUS ) {
@@ -765,4 +771,12 @@ public void close() {
765771
public String getResourceDescriptor() {
766772
return resourceDescriptor;
767773
}
774+
775+
/**
776+
* Getter for the last pong recieved
777+
* @return the timestamp for the last recieved pong
778+
*/
779+
long getLastPong() {
780+
return lastPong;
781+
}
768782
}

0 commit comments

Comments
 (0)