diff options
Diffstat (limited to 'src/main/java/de/torui/coflsky/websocket')
-rw-r--r-- | src/main/java/de/torui/coflsky/websocket/WSClient.java | 97 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/websocket/WSClientWrapper.java | 26 |
2 files changed, 109 insertions, 14 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{ } } +*/ diff --git a/src/main/java/de/torui/coflsky/websocket/WSClientWrapper.java b/src/main/java/de/torui/coflsky/websocket/WSClientWrapper.java index ef2ee8b..b3c8832 100644 --- a/src/main/java/de/torui/coflsky/websocket/WSClientWrapper.java +++ b/src/main/java/de/torui/coflsky/websocket/WSClientWrapper.java @@ -1,9 +1,12 @@ package de.torui.coflsky.websocket; +import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.util.UUID; +import com.neovisionaries.ws.client.WebSocketException; + import de.torui.coflsky.CoflSky; import de.torui.coflsky.core.Command; @@ -22,28 +25,39 @@ public class WSClientWrapper { if(!isRunning) { String uuid = CoflSky.PlayerUUID; try { + socket = new WSClient(new URI(uri + uuid)); } catch (URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } - thread = new Thread(socket); + /*thread = new Thread(socket); thread.start(); - isRunning=true; + isRunning=true;*/ + isRunning = true; + try { + socket.start(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (WebSocketException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } } } public synchronized void stop() { if(isRunning) { - try { - socket.closeBlocking(); + /* try { + //socket.closeBlocking(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); - } + }*/ + socket.stop(); isRunning = false; socket = null; - socket = null; } } |