Тестовый EchoResponse

This commit is contained in:
Gravit 2018-10-01 14:06:56 +07:00
parent 3dfde49ac9
commit 5fa271314c
3 changed files with 28 additions and 6 deletions

View file

@ -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;

View file

@ -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);
}
}

View file

@ -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);