mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-12-23 00:51:01 +03:00
[FIX] Полностью рабочий WebSocket client
This commit is contained in:
parent
b4d184baae
commit
4c7e3a8d44
6 changed files with 66 additions and 17 deletions
|
@ -24,6 +24,7 @@
|
|||
import ru.gravit.launchserver.socket.websocket.json.update.UpdateListResponse;
|
||||
import ru.gravit.utils.helper.LogHelper;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.HashMap;
|
||||
|
||||
@SuppressWarnings({"unused", "rawtypes"})
|
||||
|
@ -80,11 +81,18 @@ public void registerResponses() {
|
|||
}
|
||||
|
||||
public void sendObject(ChannelHandlerContext ctx, Object obj) {
|
||||
ctx.channel().writeAndFlush(new TextWebSocketFrame(gson.toJson(obj)));
|
||||
ctx.channel().writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, ResultInterface.class)));
|
||||
}
|
||||
public void sendObject(ChannelHandlerContext ctx, Object obj, Type type) {
|
||||
ctx.channel().writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, type)));
|
||||
}
|
||||
|
||||
public void sendObjectAndClose(ChannelHandlerContext ctx, Object obj) {
|
||||
ctx.channel().writeAndFlush(new TextWebSocketFrame(gson.toJson(obj))).addListener(ChannelFutureListener.CLOSE);
|
||||
ctx.channel().writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, ResultInterface.class))).addListener(ChannelFutureListener.CLOSE);
|
||||
}
|
||||
|
||||
public void sendObjectAndClose(ChannelHandlerContext ctx, Object obj, Type type) {
|
||||
ctx.channel().writeAndFlush(new TextWebSocketFrame(gson.toJson(obj, type))).addListener(ChannelFutureListener.CLOSE);
|
||||
}
|
||||
|
||||
public void sendEvent(EventResult obj) {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package ru.gravit.launchserver.socket.websocket.json;
|
||||
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import ru.gravit.launcher.events.request.EchoRequestEvent;
|
||||
import ru.gravit.launchserver.socket.Client;
|
||||
import ru.gravit.launchserver.socket.websocket.WebSocketService;
|
||||
import ru.gravit.utils.helper.LogHelper;
|
||||
|
@ -20,14 +21,7 @@ public String getType() {
|
|||
@Override
|
||||
public void execute(WebSocketService service, ChannelHandlerContext ctx, Client client) {
|
||||
LogHelper.info("Echo: %s, isAuth %s", echo, client.isAuth ? "true" : "false");
|
||||
service.sendObject(ctx, new Result(echo));
|
||||
service.sendObject(ctx, new EchoRequestEvent(echo));
|
||||
}
|
||||
|
||||
public class Result {
|
||||
String echo;
|
||||
|
||||
public Result(String echo) {
|
||||
this.echo = echo;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,12 +2,14 @@
|
|||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import ru.gravit.launcher.events.request.EchoRequestEvent;
|
||||
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.lang.reflect.Type;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.util.Collections;
|
||||
|
@ -27,14 +29,15 @@ public ClientWebSocketService(GsonBuilder gsonBuilder, String address, int port,
|
|||
results = new HashMap<>();
|
||||
handlers = new HashSet<>();
|
||||
this.gsonBuilder = gsonBuilder;
|
||||
gsonBuilder.registerTypeAdapter(RequestInterface.class, new JsonRequestAdapter(this));
|
||||
gsonBuilder.registerTypeAdapter(HashedEntry.class, new HashedEntryAdapter());
|
||||
this.gsonBuilder.registerTypeAdapter(RequestInterface.class, new JsonRequestAdapter(this));
|
||||
this.gsonBuilder.registerTypeAdapter(ResultInterface.class, new JsonResultAdapter(this));
|
||||
this.gsonBuilder.registerTypeAdapter(HashedEntry.class, new HashedEntryAdapter());
|
||||
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();
|
||||
URI u = new URI("ws://".concat(address).concat(":").concat(String.valueOf(port)).concat("/api"));
|
||||
return u;
|
||||
} catch (Throwable e) {
|
||||
LogHelper.error(e);
|
||||
return null;
|
||||
|
@ -48,6 +51,11 @@ public void onMessage(String message) {
|
|||
handler.process(result);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void onError(Exception e)
|
||||
{
|
||||
LogHelper.error(e);
|
||||
}
|
||||
|
||||
public Class<? extends RequestInterface> getRequestClass(String key) {
|
||||
return requests.get(key);
|
||||
|
@ -69,7 +77,7 @@ public void registerResult(String key, Class<? extends ResultInterface> clazz) {
|
|||
}
|
||||
|
||||
public void registerResults() {
|
||||
|
||||
registerResult("echo", EchoRequestEvent.class);
|
||||
}
|
||||
|
||||
public void registerHandler(EventHandler eventHandler)
|
||||
|
@ -78,7 +86,10 @@ public void registerHandler(EventHandler eventHandler)
|
|||
}
|
||||
|
||||
public void sendObject(Object obj) throws IOException {
|
||||
send(gson.toJson(obj));
|
||||
send(gson.toJson(obj, RequestInterface.class));
|
||||
}
|
||||
public void sendObject(Object obj, Type type) throws IOException {
|
||||
send(gson.toJson(obj, type));
|
||||
}
|
||||
@FunctionalInterface
|
||||
public interface EventHandler
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
import com.google.gson.GsonBuilder;
|
||||
import ru.gravit.launcher.Launcher;
|
||||
import ru.gravit.launcher.request.ResultInterface;
|
||||
import ru.gravit.utils.helper.LogHelper;
|
||||
|
||||
import java.io.IOException;
|
||||
public class LegacyRequestBridge {
|
||||
|
@ -18,6 +19,7 @@ public static ResultInterface sendRequest(RequestInterface request) throws IOExc
|
|||
synchronized(e)
|
||||
{
|
||||
e.wait();
|
||||
LogHelper.debug("WAIT OK");
|
||||
}
|
||||
}
|
||||
ResultInterface result = e.result;
|
||||
|
@ -27,6 +29,15 @@ public static ResultInterface sendRequest(RequestInterface request) throws IOExc
|
|||
public static void initWebSockets(String address, int port)
|
||||
{
|
||||
service = new ClientWebSocketService(new GsonBuilder(), address, port, 5000);
|
||||
service.registerResults();
|
||||
service.registerRequests();
|
||||
service.registerHandler(waitEventHandler);
|
||||
try {
|
||||
if(!service.connectBlocking()) LogHelper.error("Error connecting");
|
||||
LogHelper.debug("Connect to %s:%d",address,port);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
static {
|
||||
if(Launcher.getConfig().nettyPort != 0)
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
import java.util.HashSet;
|
||||
|
||||
public class WaitEventHandler implements ClientWebSocketService.EventHandler {
|
||||
public HashSet<ResultEvent> requests;
|
||||
public HashSet<ResultEvent> requests = new HashSet<>();
|
||||
@Override
|
||||
public void process(ResultInterface result) {
|
||||
for(ResultEvent r : requests)
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
package ru.gravit.launcher.events.request;
|
||||
|
||||
import ru.gravit.launcher.request.ResultInterface;
|
||||
import ru.gravit.utils.event.EventInterface;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class EchoRequestEvent implements ResultInterface, EventInterface {
|
||||
private static final UUID uuid = UUID.fromString("0a1f820f-7cd5-47a5-ae0e-17492e0e1fe1");
|
||||
public String echo;
|
||||
|
||||
public EchoRequestEvent(String echo) {
|
||||
this.echo = echo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return "echo";
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID getUUID() {
|
||||
return uuid;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue