mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-12-22 16:41:46 +03:00
Тестовый EchoResponse
This commit is contained in:
parent
3dfde49ac9
commit
5fa271314c
3 changed files with 28 additions and 6 deletions
|
@ -2,8 +2,6 @@
|
|||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.ByteBufInputStream;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
|
@ -11,14 +9,12 @@
|
|||
import io.netty.channel.group.ChannelGroup;
|
||||
import io.netty.channel.group.DefaultChannelGroup;
|
||||
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
|
||||
import io.netty.handler.codec.json.JsonObjectDecoder;
|
||||
import io.netty.util.concurrent.GlobalEventExecutor;
|
||||
import ru.gravit.launchserver.socket.websocket.json.JsonResponse;
|
||||
import ru.gravit.launchserver.socket.websocket.json.JsonResponseAdapter;
|
||||
import ru.gravit.utils.helper.IOHelper;
|
||||
import ru.gravit.utils.helper.LogHelper;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
package ru.gravit.launchserver.socket.websocket.json;
|
||||
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
|
||||
import ru.gravit.utils.helper.LogHelper;
|
||||
|
||||
public class EchoResponse implements JsonResponse {
|
||||
public final String echo;
|
||||
|
||||
public EchoResponse(String echo) {
|
||||
this.echo = echo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return "echo";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(ChannelHandlerContext ctx, WebSocketFrame frame) {
|
||||
LogHelper.info("Echo: %s",echo);
|
||||
}
|
||||
}
|
|
@ -6,12 +6,15 @@
|
|||
import java.util.HashMap;
|
||||
|
||||
public class JsonResponseAdapter implements JsonSerializer<JsonResponse>, JsonDeserializer<JsonResponse> {
|
||||
static HashMap<String,Class<JsonResponse>> map = new HashMap<>();
|
||||
static HashMap<String,Class> map = new HashMap<>();
|
||||
private static final String PROP_NAME = "type";
|
||||
static {
|
||||
map.put("echo",EchoResponse.class);
|
||||
}
|
||||
@Override
|
||||
public JsonResponse deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
||||
String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString();
|
||||
Class<JsonResponse> cls = map.get(typename);
|
||||
Class cls = map.get(typename);
|
||||
|
||||
|
||||
return (JsonResponse) context.deserialize(json, cls);
|
||||
|
|
Loading…
Reference in a new issue