From 4f1bd3712e6cd9e3c7c3c43aa67bf01c61a315b2 Mon Sep 17 00:00:00 2001 From: zaxar163 Date: Tue, 5 Feb 2019 21:25:41 +0300 Subject: [PATCH] =?UTF-8?q?[FEATURE]=20WebSocket=20=D0=BA=D0=BB=D0=B8?= =?UTF-8?q?=D0=B5=D0=BD=D1=82.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LauncherAPI/build.gradle | 2 +- .../request/websockets/ClientJSONPoint.java | 69 ++++++++----------- .../websockets/ClientWebSocketService.java | 27 +++++--- .../websockets/LegacyRequestBridge.java | 12 +--- 4 files changed, 47 insertions(+), 63 deletions(-) diff --git a/LauncherAPI/build.gradle b/LauncherAPI/build.gradle index 3bbf0bdc..1a9366f9 100644 --- a/LauncherAPI/build.gradle +++ b/LauncherAPI/build.gradle @@ -3,7 +3,7 @@ dependencies { compile project(':libLauncher') - compileOnly 'javax.websocket:javax.websocket-client-api:1.1' + compile 'org.java-websocket:Java-WebSocket:1.3.9' compileOnly 'com.google.guava:guava:26.0-jre' compile files('../compat/authlib/authlib-clean.jar') } diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientJSONPoint.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientJSONPoint.java index 1d6c7ff0..ac61ba40 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientJSONPoint.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientJSONPoint.java @@ -1,52 +1,37 @@ package ru.gravit.launcher.request.websockets; -import javax.websocket.*; -import java.io.IOException; -import java.io.Reader; +import java.net.URI; +import java.util.Map; -/* - * public class Client { - * - * final static CountDownLatch messageLatch = new CountDownLatch(1); - * - * public static void main(String[] args) { - * try { - * WebSocketContainer container = ContainerProvider.getWebSocketContainer(); - * String uri = "ws://echo.websocket.org:80/"; - * System.out.println("Connecting to " + uri); - * container.connectToServer(MyClientEndpoint.class, URI.create(uri)); - * messageLatch.await(100, TimeUnit.SECONDS); - * } catch (DeploymentException | InterruptedException | IOException ex) { - * Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex); - * } - * } - * } - */ -@ClientEndpoint -public class ClientJSONPoint { - public Session session = null; +import org.java_websocket.client.WebSocketClient; +import org.java_websocket.drafts.Draft_6455; +import org.java_websocket.handshake.ServerHandshake; - @OnOpen - public void onOpen(final Session session_r) { - session = session_r; - System.out.println("Connected to endpoint: " + session.getBasicRemote()); - } +import ru.gravit.utils.helper.LogHelper; - @OnError - public void processError(final Throwable t) { - t.printStackTrace(); - } +public class ClientJSONPoint extends WebSocketClient { - @OnMessage - public void processMessage(Reader message) { + public ClientJSONPoint(URI serverUri, Map httpHeaders, int connectTimeout) { + super(serverUri, new Draft_6455(), httpHeaders, connectTimeout); + } - } + @Override + public void onOpen(ServerHandshake handshakedata) { + + } - public void send(String js) throws IOException { - session.getBasicRemote().sendText(js); - } + @Override + public void onMessage(String message) { + } + + @Override + public void onClose(int code, String reason, boolean remote) { + LogHelper.debug("Disconnected: " + code + " " + remote + " " + reason != null ? reason : "no reason"); + } + + @Override + public void onError(Exception ex) { + LogHelper.error(ex); + } - public void sendAsync(String js) { - session.getAsyncRemote().sendText(js); - } } \ No newline at end of file diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientWebSocketService.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientWebSocketService.java index b343e34b..73f7473a 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientWebSocketService.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/ClientWebSocketService.java @@ -5,9 +5,12 @@ import ru.gravit.launcher.hasher.HashedEntry; import ru.gravit.launcher.hasher.HashedEntryAdapter; import ru.gravit.launcher.request.ResultInterface; +import ru.gravit.utils.helper.LogHelper; import java.io.IOException; -import java.io.Reader; +import java.net.URI; +import java.net.URL; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -18,7 +21,8 @@ public class ClientWebSocketService extends ClientJSONPoint { private HashMap> results; private HashSet handlers; - public ClientWebSocketService(GsonBuilder gsonBuilder) { + public ClientWebSocketService(GsonBuilder gsonBuilder, String address, int port, int i) { + super(createURL(address, port), Collections.emptyMap(), i); requests = new HashMap<>(); results = new HashMap<>(); handlers = new HashSet<>(); @@ -27,9 +31,18 @@ public ClientWebSocketService(GsonBuilder gsonBuilder) { gsonBuilder.registerTypeAdapter(HashedEntry.class, new HashedEntryAdapter()); this.gson = gsonBuilder.create(); } - @Override - public void processMessage(Reader reader) { - ResultInterface result = gson.fromJson(reader, ResultInterface.class); + private static URI createURL(String address, int port) { + try { + URL u = new URL(address); + return new URL(u.getProtocol(), u.getHost(), port, u.getFile()).toURI(); + } catch (Throwable e) { + LogHelper.error(e); + return null; + } + } + @Override + public void onMessage(String message) { + ResultInterface result = gson.fromJson(message, ResultInterface.class); for(EventHandler handler : handlers) { handler.process(result); @@ -64,10 +77,6 @@ public void registerHandler(EventHandler eventHandler) handlers.add(eventHandler); } - public void sendObjectAsync(Object obj) throws IOException { - sendAsync(gson.toJson(obj)); - } - public void sendObject(Object obj) throws IOException { send(gson.toJson(obj)); } diff --git a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/LegacyRequestBridge.java b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/LegacyRequestBridge.java index 760fe522..c39b3c0c 100644 --- a/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/LegacyRequestBridge.java +++ b/LauncherAPI/src/main/java/ru/gravit/launcher/request/websockets/LegacyRequestBridge.java @@ -4,11 +4,7 @@ import ru.gravit.launcher.Launcher; import ru.gravit.launcher.request.ResultInterface; -import javax.websocket.ContainerProvider; -import javax.websocket.WebSocketContainer; import java.io.IOException; -import java.net.URI; - public class LegacyRequestBridge { public static WaitEventHandler waitEventHandler = new WaitEventHandler(); public static ClientWebSocketService service; @@ -30,13 +26,7 @@ public static ResultInterface sendRequest(RequestInterface request) throws IOExc } public static void initWebSockets(String address, int port) { - service = new ClientWebSocketService(new GsonBuilder()); - try { - WebSocketContainer container = ContainerProvider.getWebSocketContainer(); - container.connectToServer(service, new URI("ws://".concat(address).concat(":".concat(String.valueOf(port)).concat("/api")))); - } catch (Exception e) { - throw new RuntimeException(e); - } + service = new ClientWebSocketService(new GsonBuilder(), address, port, 5000); } static { if(Launcher.getConfig().nettyPort != 0)