aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/torui/coflsky/websocket/WSClient.java
diff options
context:
space:
mode:
authorFlorian Rinke <develop@torui.de>2021-09-23 20:05:34 +0200
committerFlorian Rinke <develop@torui.de>2021-09-23 20:05:34 +0200
commit2c585a31b719e9a6fee3800a1cc57bd4eae6bd2d (patch)
tree1c7ff966a55009ab1faea9524e825997522f5f1d /src/main/java/de/torui/coflsky/websocket/WSClient.java
parente5e0c9a3a4fd9b2c10464e0f129bcb60b2db0f19 (diff)
downloadCOFL-2c585a31b719e9a6fee3800a1cc57bd4eae6bd2d.tar.gz
COFL-2c585a31b719e9a6fee3800a1cc57bd4eae6bd2d.tar.bz2
COFL-2c585a31b719e9a6fee3800a1cc57bd4eae6bd2d.zip
switched websocket library
Diffstat (limited to 'src/main/java/de/torui/coflsky/websocket/WSClient.java')
-rw-r--r--src/main/java/de/torui/coflsky/websocket/WSClient.java97
1 files changed, 89 insertions, 8 deletions
diff --git a/src/main/java/de/torui/coflsky/websocket/WSClient.java b/src/main/java/de/torui/coflsky/websocket/WSClient.java
index 9641277..59e9abb 100644
--- a/src/main/java/de/torui/coflsky/websocket/WSClient.java
+++ b/src/main/java/de/torui/coflsky/websocket/WSClient.java
@@ -1,20 +1,100 @@
package de.torui.coflsky.websocket;
-import java.net.URI;
-import java.util.LinkedList;
-import java.util.Queue;
-import org.java_websocket.client.WebSocketClient;
-import org.java_websocket.handshake.ServerHandshake;
+import java.io.IOException;
+import java.net.URI;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
+import com.neovisionaries.ws.client.WebSocket;
+import com.neovisionaries.ws.client.WebSocketAdapter;
+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.WSCommandHandler;
import de.torui.coflsky.core.Command;
-import net.minecraft.client.Minecraft;
-import net.minecraft.server.MinecraftServer;
+public class WSClient extends WebSocketAdapter {
+
+ public static Gson gson;
+
+
+ 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 URI uri;
+ public WebSocket socket;
+
+ public WSClient(URI uri) {
+ this.uri = uri;
+ }
+
+ public void start() throws IOException, WebSocketException {
+ WebSocketFactory factory = new WebSocketFactory();
+ this.socket = factory.createSocket(uri);
+ this.socket.addListener(this);
+ this.socket.connect();
+ }
+
+ public void stop() {
+ System.out.println("Closing Socket");
+ // socket.sendClose();
+ socket.clearListeners();
+
+ socket.disconnect();
+ /*try {
+ socket.getConnectedSocket().close();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (WebSocketException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }*/
+ System.out.println("Socket closed");
+
+ }
+
+ @Override
+ public void onStateChanged(WebSocket websocket, WebSocketState newState) throws Exception {
+ System.out.println("WebSocket Changed state to: " + newState);
+ super.onStateChanged(websocket, newState);
+ }
+
+
+
+ @Override
+ public void onTextMessage(WebSocket websocket, String text) throws Exception{
+
+ //super.onTextMessage(websocket, text);
+ System.out.println("Received: "+ text);
+ Command cmd = gson.fromJson(text, Command.class);
+ //System.out.println(cmd);
+ WSCommandHandler.HandleCommand(cmd, Minecraft.getMinecraft().thePlayer);
+
+ }
+
+ public void SendCommand(Command cmd) {
+ String json = gson.toJson(cmd);
+ this.socket.sendText(json);
+ }
+
+
+
+
+}
+
+/*
public class WSClient extends WebSocketClient{
public static Gson gson;
@@ -31,7 +111,7 @@ public class WSClient extends WebSocketClient{
char firstChar = name.charAt(0);
return Character.toLowerCase(firstChar) + name.substring(1);
}
- })*/.create();
+ })*.create();
}
public WSClient(URI serverUri) {
@@ -69,3 +149,4 @@ public class WSClient extends WebSocketClient{
}
}
+*/