Add DisconnectListener

This commit is contained in:
MrLetsplay 2024-04-20 18:42:16 +02:00
parent 3baa9ce1e2
commit 4b0ec70f54
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
5 changed files with 26 additions and 1 deletions

View File

@ -0,0 +1,7 @@
package me.mrletsplay.shareclientcore.connection;
public interface DisconnectListener {
public void onDisconnect(String reason, boolean remote);
}

View File

@ -34,4 +34,9 @@ public class DummyConnection implements RemoteConnection {
} }
@Override
public void setDisconnectListener(DisconnectListener listener) {
}
} }

View File

@ -21,4 +21,6 @@ public interface RemoteConnection {
public void removeListener(MessageListener listener); public void removeListener(MessageListener listener);
public void setDisconnectListener(DisconnectListener listener);
} }

View File

@ -23,6 +23,7 @@ public class WebSocketConnection implements RemoteConnection {
private WSClient client; private WSClient client;
private String username; private String username;
private Set<MessageListener> listeners; private Set<MessageListener> listeners;
private DisconnectListener disconnectListener;
private int siteID; private int siteID;
private Object wait = new Object(); private Object wait = new Object();
@ -90,6 +91,11 @@ public class WebSocketConnection implements RemoteConnection {
listeners.remove(listener); listeners.remove(listener);
} }
@Override
public void setDisconnectListener(DisconnectListener listener) {
this.disconnectListener = listener;
}
private class WSClient extends WebSocketClient { private class WSClient extends WebSocketClient {
public WSClient(URI serverUri) { public WSClient(URI serverUri) {
@ -138,7 +144,7 @@ public class WebSocketConnection implements RemoteConnection {
@Override @Override
public void onClose(int code, String reason, boolean remote) { public void onClose(int code, String reason, boolean remote) {
// TODO: handle if(disconnectListener != null) disconnectListener.onDisconnect("WebSocket disconnected (" + code + "): " + reason, remote);
} }
@Override @Override

View File

@ -57,4 +57,9 @@ public record Char(Identifier[] position, int lamport, char value) implements Se
return new Char(pos, lamport, value); return new Char(pos, lamport, value);
} }
@Override
public String toString() {
return "Char [position=" + Arrays.toString(position) + ", lamport=" + lamport + ", value=" + value + "]";
}
} }