Skip to content

Commit

Permalink
add debug and error event listening options
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianVennen committed May 27, 2022
1 parent e6d7c0e commit 10c1d1b
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/exaroton/api/ws/DebugListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.exaroton.api.ws;

public interface DebugListener {
public void onDebug(String message);
}
5 changes: 5 additions & 0 deletions src/main/java/com/exaroton/api/ws/ErrorListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.exaroton.api.ws;

public interface ErrorListener {
public void onError(String error, Throwable t);
}
3 changes: 3 additions & 0 deletions src/main/java/com/exaroton/api/ws/WebSocketClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public WebSocketClient(URI uri, WebSocketManager manager) {

@Override
public void onOpen(ServerHandshake handshakedata) {
manager.sendDebug("Websocket opened with status " + handshakedata.getHttpStatus() + ": " + handshakedata.getHttpStatusMessage());
manager.handleOpen();
}

Expand All @@ -51,10 +52,12 @@ public void onMessage(String message) {
@Override
public void onError(Exception ex) {
logger.error("A websocket error ocurred", ex);
manager.onError("A websocket error ocurred: " + ex.getMessage(), ex);
}

@Override
public void onClose(int code, String reason, boolean remote) {
manager.sendDebug("Websocket closed with code " + code + ": " + reason);
manager.handleClose(code, reason, remote);
}
}
39 changes: 39 additions & 0 deletions src/main/java/com/exaroton/api/ws/WebSocketManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public class WebSocketManager {
*/
private final Server server;

private ErrorListener errorListener = null;

private DebugListener debugListener = null;

public WebSocketManager(String uri, String apiToken, Server server) {
try {
URI u = new URI(uri);
Expand Down Expand Up @@ -302,4 +306,39 @@ public boolean serverHasStatus(int... status) {
}
return this.server.hasStatus(status);
}

/**
* Listen to websocket errors
* @param errorListener the only error listener
*/
public void setErrorListener(ErrorListener errorListener) {
this.errorListener = errorListener;
}

/**
* listen to websocket debug information
* @param debugListener the only debug listener
*/
public void setDebugListener(DebugListener debugListener) {
this.debugListener = debugListener;
}


/**
* send debug information to listeners
*/
void sendDebug(String message) {
if (this.debugListener != null) {
this.debugListener.onDebug(message);
}
}

/**
* send error to listeners
*/
void onError(String error, Throwable throwable) {
this.errorListener.onError(error, throwable);
}


}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.exaroton.api.ws.stream;

import com.exaroton.api.ws.WebSocketManager;
import com.exaroton.api.ws.subscriber.ConsoleSubscriber;

public class ConsoleStream extends Stream {

Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/exaroton/api/ws/stream/HeapStream.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.exaroton.api.ws.stream;

import com.exaroton.api.ws.WebSocketManager;
import com.exaroton.api.ws.subscriber.HeapSubscriber;

public class HeapStream extends Stream {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


import com.exaroton.api.ws.WebSocketManager;
import com.exaroton.api.ws.subscriber.ServerStatusSubscriber;

public class ServerStatusStream extends Stream {

Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/exaroton/api/ws/stream/StatsStream.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.exaroton.api.ws.stream;

import com.exaroton.api.ws.WebSocketManager;
import com.exaroton.api.ws.subscriber.StatsSubscriber;

public class StatsStream extends Stream {

Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/exaroton/api/ws/stream/TickStream.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.exaroton.api.ws.stream;

import com.exaroton.api.ws.WebSocketManager;
import com.exaroton.api.ws.subscriber.TickSubscriber;

public class TickStream extends Stream {

Expand Down

0 comments on commit 10c1d1b

Please sign in to comment.