mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-11-15 03:31:15 +03:00
Merge pull request #168 from GravitLauncher/feature/wClient
[FEATURE] WebSocket клиент.
This commit is contained in:
commit
4bb49799af
4 changed files with 47 additions and 63 deletions
|
@ -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')
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
|
||||||
System.out.println("Connected to endpoint: " + session.getBasicRemote());
|
|
||||||
}
|
|
||||||
|
|
||||||
@OnError
|
public class ClientJSONPoint extends WebSocketClient {
|
||||||
public void processError(final Throwable t) {
|
|
||||||
t.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
@OnMessage
|
public ClientJSONPoint(URI serverUri, Map<String, String> httpHeaders, int connectTimeout) {
|
||||||
public void processMessage(Reader message) {
|
super(serverUri, new Draft_6455(), httpHeaders, connectTimeout);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
@Override
|
||||||
|
public void onOpen(ServerHandshake handshakedata) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public void send(String js) throws IOException {
|
@Override
|
||||||
session.getBasicRemote().sendText(js);
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -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<>();
|
||||||
|
@ -27,9 +31,18 @@ public ClientWebSocketService(GsonBuilder gsonBuilder) {
|
||||||
gsonBuilder.registerTypeAdapter(HashedEntry.class, new HashedEntryAdapter());
|
gsonBuilder.registerTypeAdapter(HashedEntry.class, new HashedEntryAdapter());
|
||||||
this.gson = gsonBuilder.create();
|
this.gson = gsonBuilder.create();
|
||||||
}
|
}
|
||||||
@Override
|
private static URI createURL(String address, int port) {
|
||||||
public void processMessage(Reader reader) {
|
try {
|
||||||
ResultInterface result = gson.fromJson(reader, ResultInterface.class);
|
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)
|
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));
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
|
Loading…
Reference in a new issue