[FEATURE] WebSocket клиент.

This commit is contained in:
zaxar163 2019-02-05 21:25:41 +03:00
parent 7f9ca582d6
commit 4f1bd3712e
No known key found for this signature in database
GPG key ID: E3B309DD3852DE06
4 changed files with 47 additions and 63 deletions

View file

@ -3,7 +3,7 @@
dependencies { dependencies {
compile project(':libLauncher') 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' compileOnly 'com.google.guava:guava:26.0-jre'
compile files('../compat/authlib/authlib-clean.jar') compile files('../compat/authlib/authlib-clean.jar')
} }

View file

@ -1,52 +1,37 @@
package ru.gravit.launcher.request.websockets; package ru.gravit.launcher.request.websockets;
import javax.websocket.*; import java.net.URI;
import java.io.IOException; import java.util.Map;
import java.io.Reader;
/* import org.java_websocket.client.WebSocketClient;
* public class Client { import org.java_websocket.drafts.Draft_6455;
* import org.java_websocket.handshake.ServerHandshake;
* 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;
@OnOpen import ru.gravit.utils.helper.LogHelper;
public void onOpen(final Session session_r) {
session = session_r; public class ClientJSONPoint extends WebSocketClient {
System.out.println("Connected to endpoint: " + session.getBasicRemote());
public ClientJSONPoint(URI serverUri, Map<String, String> httpHeaders, int connectTimeout) {
super(serverUri, new Draft_6455(), httpHeaders, connectTimeout);
} }
@OnError @Override
public void processError(final Throwable t) { public void onOpen(ServerHandshake handshakedata) {
t.printStackTrace();
}
@OnMessage
public void processMessage(Reader message) {
} }
public void send(String js) throws IOException { @Override
session.getBasicRemote().sendText(js); public void onMessage(String message) {
} }
public void sendAsync(String js) { @Override
session.getAsyncRemote().sendText(js); 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);
}
} }

View file

@ -5,9 +5,12 @@
import ru.gravit.launcher.hasher.HashedEntry; import ru.gravit.launcher.hasher.HashedEntry;
import ru.gravit.launcher.hasher.HashedEntryAdapter; import ru.gravit.launcher.hasher.HashedEntryAdapter;
import ru.gravit.launcher.request.ResultInterface; import ru.gravit.launcher.request.ResultInterface;
import ru.gravit.utils.helper.LogHelper;
import java.io.IOException; 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.HashMap;
import java.util.HashSet; import java.util.HashSet;
@ -18,7 +21,8 @@ public class ClientWebSocketService extends ClientJSONPoint {
private HashMap<String, Class<? extends ResultInterface>> results; private HashMap<String, Class<? extends ResultInterface>> results;
private HashSet<EventHandler> handlers; private HashSet<EventHandler> 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<>(); requests = new HashMap<>();
results = new HashMap<>(); results = new HashMap<>();
handlers = new HashSet<>(); handlers = new HashSet<>();
@ -26,10 +30,19 @@ public ClientWebSocketService(GsonBuilder gsonBuilder) {
gsonBuilder.registerTypeAdapter(RequestInterface.class, new JsonRequestAdapter(this)); gsonBuilder.registerTypeAdapter(RequestInterface.class, new JsonRequestAdapter(this));
gsonBuilder.registerTypeAdapter(HashedEntry.class, new HashedEntryAdapter()); gsonBuilder.registerTypeAdapter(HashedEntry.class, new HashedEntryAdapter());
this.gson = gsonBuilder.create(); this.gson = gsonBuilder.create();
}
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 @Override
public void processMessage(Reader reader) { public void onMessage(String message) {
ResultInterface result = gson.fromJson(reader, ResultInterface.class); ResultInterface result = gson.fromJson(message, ResultInterface.class);
for(EventHandler handler : handlers) for(EventHandler handler : handlers)
{ {
handler.process(result); handler.process(result);
@ -64,10 +77,6 @@ public void registerHandler(EventHandler eventHandler)
handlers.add(eventHandler); handlers.add(eventHandler);
} }
public void sendObjectAsync(Object obj) throws IOException {
sendAsync(gson.toJson(obj));
}
public void sendObject(Object obj) throws IOException { public void sendObject(Object obj) throws IOException {
send(gson.toJson(obj)); send(gson.toJson(obj));
} }

View file

@ -4,11 +4,7 @@
import ru.gravit.launcher.Launcher; import ru.gravit.launcher.Launcher;
import ru.gravit.launcher.request.ResultInterface; import ru.gravit.launcher.request.ResultInterface;
import javax.websocket.ContainerProvider;
import javax.websocket.WebSocketContainer;
import java.io.IOException; import java.io.IOException;
import java.net.URI;
public class LegacyRequestBridge { public class LegacyRequestBridge {
public static WaitEventHandler waitEventHandler = new WaitEventHandler(); public static WaitEventHandler waitEventHandler = new WaitEventHandler();
public static ClientWebSocketService service; public static ClientWebSocketService service;
@ -30,13 +26,7 @@ public static ResultInterface sendRequest(RequestInterface request) throws IOExc
} }
public static void initWebSockets(String address, int port) public static void initWebSockets(String address, int port)
{ {
service = new ClientWebSocketService(new GsonBuilder()); service = new ClientWebSocketService(new GsonBuilder(), address, port, 5000);
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);
}
} }
static { static {
if(Launcher.getConfig().nettyPort != 0) if(Launcher.getConfig().nettyPort != 0)