diff --git a/libLauncher/src/main/java/ru/gravit/utils/websocket/MessageInterface.java b/libLauncher/src/main/java/ru/gravit/utils/websocket/MessageInterface.java new file mode 100644 index 00000000..0502814b --- /dev/null +++ b/libLauncher/src/main/java/ru/gravit/utils/websocket/MessageInterface.java @@ -0,0 +1,8 @@ +package ru.gravit.utils.websocket; + +import ru.gravit.launcher.serialize.HInput; + +@FunctionalInterface +public interface MessageInterface { + void request(HInput input); +} diff --git a/libLauncher/src/main/java/ru/gravit/utils/websocket/WebSocketClient.java b/libLauncher/src/main/java/ru/gravit/utils/websocket/WebSocketClient.java index 0ebcbf14..70c58a72 100644 --- a/libLauncher/src/main/java/ru/gravit/utils/websocket/WebSocketClient.java +++ b/libLauncher/src/main/java/ru/gravit/utils/websocket/WebSocketClient.java @@ -1,5 +1,6 @@ package ru.gravit.utils.websocket; +import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; @@ -15,6 +16,7 @@ import org.eclipse.jetty.websocket.api.annotations.WebSocket; import ru.gravit.launcher.Launcher; import ru.gravit.launcher.serialize.HInput; +import ru.gravit.launcher.serialize.HOutput; import ru.gravit.utils.helper.IOHelper; /** @@ -26,10 +28,11 @@ public class WebSocketClient private final CountDownLatch closeLatch; @SuppressWarnings("unused") private Session session; - - public WebSocketClient() + private final MessageInterface adapter; + public WebSocketClient(MessageInterface adapter) { this.closeLatch = new CountDownLatch(1); + this.adapter = adapter; } public boolean awaitClose(int duration, TimeUnit unit) throws InterruptedException @@ -50,38 +53,15 @@ public void onConnect(Session session) { System.out.printf("Got connect: %s%n",session); this.session = session; - try - { - Future fut; - fut = session.getRemote().sendStringByFuture("Hello"); - fut.get(2,TimeUnit.SECONDS); // wait for send to complete. - - fut = session.getRemote().sendStringByFuture("Thanks for the conversation."); - fut.get(2,TimeUnit.SECONDS); // wait for send to complete. - - session.close(StatusCode.NORMAL,"I'm done"); - } - catch (Throwable t) - { - t.printStackTrace(); - } } - public void request(ByteBuffer buffer) throws InterruptedException, ExecutionException, TimeoutException { - Future fut; - fut = session.getRemote().sendBytesByFuture(buffer); - fut.get(2,TimeUnit.SECONDS); + public void request(ByteBuffer buffer) throws IOException { + session.getRemote().sendBytes(buffer); } @OnWebSocketMessage public void onMessage(String msg) throws IOException { byte[] bytes = msg.getBytes(); InputStream stream = new ByteArrayInputStream(bytes); HInput input = new HInput(stream); - long handshake = input.readLong(); - if(handshake != Launcher.PROTOCOL_MAGIC) - { - throw new IOException("INVALID HANDSHAKE"); - } - int type = input.readInt(); - System.out.printf("Got msg: %l %d",handshake,type); + adapter.request(input); } }