diff options
| author | Florian Rinke <develop@torui.de> | 2021-10-11 17:52:17 +0200 |
|---|---|---|
| committer | Florian Rinke <develop@torui.de> | 2021-10-11 17:52:17 +0200 |
| commit | 16b9821f55eec8f4a77ee1743cbc3f9b4ba3e303 (patch) | |
| tree | c7ede24fb2a7c0871c92c66fe4b4f7806344d326 /src/main/java/de/torui/coflsky/websocket | |
| parent | 9e923dbdf82b2a1fbebcb9c4559400f921104229 (diff) | |
| download | COFL-16b9821f55eec8f4a77ee1743cbc3f9b4ba3e303.tar.gz COFL-16b9821f55eec8f4a77ee1743cbc3f9b4ba3e303.tar.bz2 COFL-16b9821f55eec8f4a77ee1743cbc3f9b4ba3e303.zip | |
implements #11, #8
Diffstat (limited to 'src/main/java/de/torui/coflsky/websocket')
| -rw-r--r-- | src/main/java/de/torui/coflsky/websocket/WSClient.java | 68 | ||||
| -rw-r--r-- | src/main/java/de/torui/coflsky/websocket/WSClientWrapper.java | 106 |
2 files changed, 94 insertions, 80 deletions
diff --git a/src/main/java/de/torui/coflsky/websocket/WSClient.java b/src/main/java/de/torui/coflsky/websocket/WSClient.java index c4ad799..3cb2de2 100644 --- a/src/main/java/de/torui/coflsky/websocket/WSClient.java +++ b/src/main/java/de/torui/coflsky/websocket/WSClient.java @@ -14,7 +14,7 @@ import com.neovisionaries.ws.client.WebSocketException; import com.neovisionaries.ws.client.WebSocketFactory; import com.neovisionaries.ws.client.WebSocketState; import net.minecraft.client.Minecraft; - +import de.torui.coflsky.CoflSky; import de.torui.coflsky.WSCommandHandler; import de.torui.coflsky.core.Command; import de.torui.coflsky.core.StringCommand; @@ -38,10 +38,12 @@ public class WSClient extends WebSocketAdapter { } public URI uri; public WebSocket socket; + public boolean shouldRun = false; public WebSocketState currentState = WebSocketState.CLOSED; public WSClient(URI uri) { this.uri = uri; + } public void start() throws IOException, WebSocketException, NoSuchAlgorithmException { @@ -95,6 +97,11 @@ public class WSClient extends WebSocketAdapter { public void onStateChanged(WebSocket websocket, WebSocketState newState) throws Exception { System.out.println("WebSocket Changed state to: " + newState); currentState = newState; + + if(newState == WebSocketState.CLOSED && shouldRun) { + CoflSky.Wrapper.restartWebsocketConnection(); + } + super.onStateChanged(websocket, newState); } @@ -124,61 +131,4 @@ public class WSClient extends WebSocketAdapter { -} - -/* -public class WSClient extends WebSocketClient{ - - public static Gson gson; - - public static WSClient Instancce; - - - static { - gson = new GsonBuilder()/*.setFieldNamingStrategy(new FieldNamingStrategy() { - @Override - public String translateName(Field f) { - - String name = f.getName(); - char firstChar = name.charAt(0); - return Character.toLowerCase(firstChar) + name.substring(1); - } - })*.create(); - } - - public WSClient(URI serverUri) { - super(serverUri); - - } - - @Override - public void onOpen(ServerHandshake handshakedata) { - - } - - @Override - public void onMessage(String message) { - //System.out.println(message); - - Command cmd = gson.fromJson(message, Command.class); - //System.out.println(cmd); - WSCommandHandler.HandleCommand(cmd, Minecraft.getMinecraft().thePlayer); - } - - @Override - public void onClose(int code, String reason, boolean remote) { - System.out.printf("code: %n reason:%s remote:%b", code, reason,remote); - } - - @Override - public void onError(Exception ex) { - ex.printStackTrace(); - } - - public void SendCommand(Command command) { - String json = gson.toJson(command); - this.send(json); - } - -} -*/ +}
\ No newline at end of file diff --git a/src/main/java/de/torui/coflsky/websocket/WSClientWrapper.java b/src/main/java/de/torui/coflsky/websocket/WSClientWrapper.java index 2cdfda1..7d66600 100644 --- a/src/main/java/de/torui/coflsky/websocket/WSClientWrapper.java +++ b/src/main/java/de/torui/coflsky/websocket/WSClientWrapper.java @@ -11,33 +11,100 @@ import com.neovisionaries.ws.client.WebSocketException; import de.torui.coflsky.CoflSky; import de.torui.coflsky.core.Command; import de.torui.coflsky.core.StringCommand; +import de.torui.coflsky.minecraft_integration.PlayerDataProvider; +import de.torui.coflsky.minecraft_integration.CoflSessionManager; public class WSClientWrapper { public WSClient socket; - public Thread thread; + // public Thread thread; public boolean isRunning; - public String uri = ""; - public WSClientWrapper(String uri) { - this.uri = uri; + private String[] uris; + + + public WSClientWrapper(String[] uris) { + this.uris = uris; + } + + public void restartWebsocketConnection() { + socket.socket.clearListeners(); + socket.stop(); + + System.out.println("Sleeping..."); + + try { + Thread.sleep(2000); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + socket = new WSClient(socket.uri); + isRunning = false; + start(); } - public synchronized void start() { + + public boolean startConnection() { + + if(isRunning) + return false; + + for(String s : uris) { + + System.out.println("Trying connection with uri=" + s); + + if(initializeNewSocket(s)) { + return true; + } + } + + throw new Error("Could not connect to any websocket remote!"); + } + + + + private boolean initializeNewSocket(String uriPrefix) { + + + String uri = uriPrefix; + uri += "?version=" + CoflSky.VERSION; + + String username = PlayerDataProvider.getUsername(); + uri += "&player=" + username; + + //Generate a CoflSession + + try { + CoflSessionManager.UpdateCoflSessions(); + String coflSessionID = CoflSessionManager.GetCoflSession(username).SessionUUID; + + uri += "&SId=" + coflSessionID; + + socket = new WSClient(URI.create(uri)); + + boolean successfull = start(); + if(successfull) { + socket.shouldRun = true; + } + return successfull; + } catch(IOException e) { + e.printStackTrace(); + } + + return false; + + } + + private synchronized boolean start() { if(!isRunning) { try { - socket = new WSClient(new URI(uri)); - } catch (URISyntaxException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - /*thread = new Thread(socket); - thread.start(); - isRunning=true;*/ - isRunning = true; - try { socket.start(); + isRunning = true; + + return true; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -48,17 +115,14 @@ public class WSClientWrapper { // TODO Auto-generated catch block e.printStackTrace(); } + return false; } + return false; } public synchronized void stop() { if(isRunning) { - /* try { - //socket.closeBlocking(); - } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - }*/ + socket.shouldRun = false; socket.stop(); isRunning = false; socket = null; |
