From 5fa271314c7e258d98f6b2eb7ff763d91503868c Mon Sep 17 00:00:00 2001 From: Gravit Date: Mon, 1 Oct 2018 14:06:56 +0700 Subject: [PATCH] =?UTF-8?q?=D0=A2=D0=B5=D1=81=D1=82=D0=BE=D0=B2=D1=8B?= =?UTF-8?q?=D0=B9=20EchoResponse?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../websocket/WebSocketFrameHandler.java | 4 ---- .../socket/websocket/json/EchoResponse.java | 23 +++++++++++++++++++ .../websocket/json/JsonResponseAdapter.java | 7 ++++-- 3 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/EchoResponse.java diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/WebSocketFrameHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/WebSocketFrameHandler.java index 27b6965a..58652cb8 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/WebSocketFrameHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/WebSocketFrameHandler.java @@ -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; diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/EchoResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/EchoResponse.java new file mode 100644 index 00000000..d8382d92 --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/EchoResponse.java @@ -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); + } +} diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/JsonResponseAdapter.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/JsonResponseAdapter.java index 8dadf58e..e3f48525 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/JsonResponseAdapter.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/JsonResponseAdapter.java @@ -6,12 +6,15 @@ import java.util.HashMap; public class JsonResponseAdapter implements JsonSerializer, JsonDeserializer { - static HashMap> map = new HashMap<>(); + static HashMap 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 cls = map.get(typename); + Class cls = map.get(typename); return (JsonResponse) context.deserialize(json, cls);