Skip to content

Commit 8472a1e

Browse files
committed
Enable and Disable ping/pong
TooTallNate#699
1 parent af0ed60 commit 8472a1e

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/main/java/org/java_websocket/AbstractWebSocket.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ public abstract class AbstractWebSocket extends WebSocketAdapter {
6767
*/
6868
private int connectionLostTimeout = 60;
6969

70+
/**
71+
* Attribute to keep track if the WebSocket Server/Client is running/connected
72+
* @since 1.3.9
73+
*/
74+
private boolean websocketRunning = false;
75+
7076
/**
7177
* Get the interval checking for lost connections
7278
* Default is 60 seconds
@@ -87,11 +93,23 @@ public int getConnectionLostTimeout() {
8793
public void setConnectionLostTimeout( int connectionLostTimeout ) {
8894
this.connectionLostTimeout = connectionLostTimeout;
8995
if (this.connectionLostTimeout <= 0) {
90-
stopConnectionLostTimer();
96+
if( WebSocketImpl.DEBUG )
97+
System.out.println( "Connection lost timer stopped" );
98+
cancelConnectionLostTimer();
99+
return;
91100
}
92-
if (connectionLostTimer != null || connectionLostTimerTask != null) {
101+
if (this.websocketRunning) {
93102
if( WebSocketImpl.DEBUG )
94103
System.out.println( "Connection lost timer restarted" );
104+
//Reset all the pings
105+
ArrayList<WebSocket> connections = new ArrayList<WebSocket>( getConnections() );
106+
WebSocketImpl webSocketImpl;
107+
for( WebSocket conn : connections ) {
108+
if( conn instanceof WebSocketImpl ) {
109+
webSocketImpl = ( WebSocketImpl ) conn;
110+
webSocketImpl.updateLastPong();
111+
}
112+
}
95113
restartConnectionLostTimer();
96114
}
97115
}
@@ -102,6 +120,7 @@ public void setConnectionLostTimeout( int connectionLostTimeout ) {
102120
*/
103121
protected void stopConnectionLostTimer() {
104122
if (connectionLostTimer != null ||connectionLostTimerTask != null) {
123+
this.websocketRunning = false;
105124
if( WebSocketImpl.DEBUG )
106125
System.out.println( "Connection lost timer stopped" );
107126
cancelConnectionLostTimer();
@@ -119,6 +138,7 @@ protected void startConnectionLostTimer() {
119138
}
120139
if (WebSocketImpl.DEBUG)
121140
System.out.println("Connection lost timer started");
141+
this.websocketRunning = true;
122142
restartConnectionLostTimer();
123143
}
124144

0 commit comments

Comments
 (0)