mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-12-23 00:51:01 +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 {
|
||||
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')
|
||||
}
|
||||
|
|
|
@ -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<String, String> 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);
|
||||
}
|
||||
}
|
|
@ -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<String, Class<? extends ResultInterface>> results;
|
||||
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<>();
|
||||
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));
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue