mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-12-22 16:41:46 +03:00
Клиент WebSockets
This commit is contained in:
parent
7ae32bf1e7
commit
6167699c20
7 changed files with 119 additions and 23 deletions
|
@ -11,7 +11,7 @@
|
|||
import ru.gravit.launchserver.LaunchServer;
|
||||
import ru.gravit.launchserver.socket.Client;
|
||||
import ru.gravit.launchserver.socket.websocket.json.EchoResponse;
|
||||
import ru.gravit.launchserver.socket.websocket.json.HashedEntryAdapter;
|
||||
import ru.gravit.launcher.hasher.HashedEntryAdapter;
|
||||
import ru.gravit.launchserver.socket.websocket.json.JsonResponseAdapter;
|
||||
import ru.gravit.launchserver.socket.websocket.json.JsonResponseInterface;
|
||||
import ru.gravit.launchserver.socket.websocket.json.auth.AuthResponse;
|
||||
|
@ -31,7 +31,7 @@ public WebSocketService(ChannelGroup channels, LaunchServer server, GsonBuilder
|
|||
this.gsonBuiler = gson;
|
||||
this.
|
||||
gsonBuiler.registerTypeAdapter(JsonResponseInterface.class,new JsonResponseAdapter(this));
|
||||
gsonBuiler.registerTypeAdapter(HashedEntry.class,new HashedEntryAdapter(this));
|
||||
gsonBuiler.registerTypeAdapter(HashedEntry.class,new HashedEntryAdapter());
|
||||
this.gson = gsonBuiler.create();
|
||||
}
|
||||
|
||||
|
|
|
@ -9,10 +9,6 @@
|
|||
import javax.websocket.OnOpen;
|
||||
import javax.websocket.Session;
|
||||
|
||||
import com.eclipsesource.json.Json;
|
||||
import com.eclipsesource.json.JsonValue;
|
||||
import com.eclipsesource.json.ParseException;
|
||||
|
||||
import ru.gravit.utils.helper.LogHelper;
|
||||
|
||||
/*
|
||||
|
@ -36,6 +32,11 @@
|
|||
@ClientEndpoint
|
||||
public class ClientJSONPoint {
|
||||
public Session session = null;
|
||||
private ClientWebSocketService service;
|
||||
|
||||
public void setService(ClientWebSocketService service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
@OnOpen
|
||||
public void onOpen(final Session session_r) {
|
||||
|
@ -50,18 +51,14 @@ public void processError(final Throwable t) {
|
|||
|
||||
@OnMessage
|
||||
public void processMessage(Reader message) {
|
||||
try {
|
||||
JsonValue json = Json.parse(message);
|
||||
} catch (ParseException | IOException ex) {
|
||||
LogHelper.error(ex);
|
||||
}
|
||||
service.processMessage(message);
|
||||
}
|
||||
|
||||
public void send(JsonValue js) throws IOException {
|
||||
session.getBasicRemote().sendText(js.asString());
|
||||
public void send(String js) throws IOException {
|
||||
session.getBasicRemote().sendText(js);
|
||||
}
|
||||
|
||||
public void sendAsync(JsonValue js) throws IOException {
|
||||
session.getAsyncRemote().sendText(js.asString());
|
||||
public void sendAsync(String js) throws IOException {
|
||||
session.getAsyncRemote().sendText(js);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
package ru.gravit.launcher.request.websockets;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import ru.gravit.launcher.hasher.HashedEntry;
|
||||
import ru.gravit.launcher.hasher.HashedEntryAdapter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class ClientWebSocketService {
|
||||
public final GsonBuilder gsonBuilder;
|
||||
public final Gson gson;
|
||||
public final ClientJSONPoint point;
|
||||
private HashMap<String,Class> requests;
|
||||
private HashMap<String,Class> results;
|
||||
|
||||
public ClientWebSocketService(GsonBuilder gsonBuilder,ClientJSONPoint point) {
|
||||
requests = new HashMap<>();
|
||||
results = new HashMap<>();
|
||||
this.gsonBuilder = gsonBuilder;
|
||||
gsonBuilder.registerTypeAdapter(RequestInterface.class, new JsonRequestAdapter(this));
|
||||
gsonBuilder.registerTypeAdapter(HashedEntry.class,new HashedEntryAdapter());
|
||||
this.gson = gsonBuilder.create();
|
||||
this.point = point;
|
||||
point.setService(this);
|
||||
}
|
||||
public void processMessage(Reader reader)
|
||||
{
|
||||
ResultInterface result = gson.fromJson(reader,ResultInterface.class);
|
||||
result.process();
|
||||
}
|
||||
public Class getRequestClass(String key)
|
||||
{
|
||||
return requests.get(key);
|
||||
}
|
||||
public void registerRequest(String key, Class clazz)
|
||||
{
|
||||
requests.put(key,clazz);
|
||||
}
|
||||
public void registerRequests()
|
||||
{
|
||||
|
||||
}
|
||||
public void registerResult(String key, Class clazz)
|
||||
{
|
||||
results.put(key,clazz);
|
||||
}
|
||||
public void registerResults()
|
||||
{
|
||||
|
||||
}
|
||||
public void sendObjectAsync(Object obj) throws IOException {
|
||||
point.sendAsync(gson.toJson(obj));
|
||||
}
|
||||
public void sendObject(Object obj) throws IOException {
|
||||
point.send(gson.toJson(obj));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package ru.gravit.launcher.request.websockets;
|
||||
|
||||
import com.google.gson.*;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class JsonRequestAdapter implements JsonSerializer<RequestInterface>, JsonDeserializer<RequestInterface> {
|
||||
private final ClientWebSocketService service;
|
||||
private static final String PROP_NAME = "type";
|
||||
|
||||
public JsonRequestAdapter(ClientWebSocketService service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RequestInterface deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
||||
String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString();
|
||||
Class<RequestInterface> cls = service.getRequestClass(typename);
|
||||
|
||||
|
||||
return (RequestInterface) context.deserialize(json, cls);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(RequestInterface src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
// note : won't work, you must delegate this
|
||||
JsonObject jo = context.serialize(src).getAsJsonObject();
|
||||
|
||||
String classPath = src.getType();
|
||||
jo.add(PROP_NAME, new JsonPrimitive(classPath));
|
||||
|
||||
return jo;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package ru.gravit.launcher.request.websockets;
|
||||
|
||||
public interface RequestInterface {
|
||||
String getType();
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package ru.gravit.launcher.request.websockets;
|
||||
|
||||
public interface ResultInterface {
|
||||
void process();
|
||||
}
|
|
@ -1,20 +1,15 @@
|
|||
package ru.gravit.launchserver.socket.websocket.json;
|
||||
package ru.gravit.launcher.hasher;
|
||||
|
||||
import com.google.gson.*;
|
||||
import ru.gravit.launcher.hasher.HashedDir;
|
||||
import ru.gravit.launcher.hasher.HashedEntry;
|
||||
import ru.gravit.launcher.hasher.HashedFile;
|
||||
import ru.gravit.launchserver.socket.websocket.WebSocketService;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
|
||||
public class HashedEntryAdapter implements JsonSerializer<HashedEntry>, JsonDeserializer<HashedEntry> {
|
||||
private final WebSocketService service;
|
||||
private static final String PROP_NAME = "type";
|
||||
|
||||
public HashedEntryAdapter(WebSocketService service) {
|
||||
this.service = service;
|
||||
public HashedEntryAdapter() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
Loading…
Reference in a new issue