Skip to content

Commit dc51e4c

Browse files
Added retry-on-no-response so small issues don't lead to disconnect
1 parent 287c558 commit dc51e4c

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

lib/src/main/java/org/jighthouse/WSConnector.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class WSConnector extends Thread {
1515
private String token;
1616
private String address;
1717
private int waitPeriod;
18+
private int retryWhenNoResponse;
1819
// Flags
1920
private boolean isRunning;
2021
private boolean isConnected;
@@ -41,6 +42,7 @@ public WSConnector(String username, String token, String address,
4142
this.waitPeriod = framerate > 0 ? ((int) (1000 / framerate)) : 1;
4243
this.isConnected = false;
4344
this.isRunning = false;
45+
this.retryWhenNoResponse = 10;
4446
}
4547

4648
private boolean gotValidHttpCode() {
@@ -219,6 +221,8 @@ public void run() {
219221

220222
JhFrameObject frame = JhFrameObject.getEmptyFrame();
221223

224+
int retry = retryWhenNoResponse;
225+
222226
// Main loop
223227
while (this.isRunning && this.isConnected) {
224228
setStatus(WSCStatus.RUNNING);
@@ -252,8 +256,15 @@ public void run() {
252256
framesDisplayed++;
253257

254258
if (ws.millisSinceResponse() > (500 + 2 * waitPeriod)) {
255-
System.err.println("Error: Server not responding! Please check your network connection.");
256-
this.isRunning = false;
259+
if (retry < 1) {
260+
System.err.println("Error: Server not responding! Please check your network connection.");
261+
this.isRunning = false;
262+
} else {
263+
retry--;
264+
System.err.println("Error: Server not responding! Retrying ...");
265+
}
266+
} else {
267+
retry = retryWhenNoResponse;
257268
}
258269

259270
// 3. Sleep depending on framerate, with small negative offset.

0 commit comments

Comments
 (0)