diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/NettyServerSocketHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/NettyServerSocketHandler.java index 939a11d1..2644b359 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/NettyServerSocketHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/NettyServerSocketHandler.java @@ -12,6 +12,7 @@ import io.netty.handler.codec.http.HttpServerCodec; import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler; import io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketServerCompressionHandler; +import io.netty.handler.codec.json.JsonObjectDecoder; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; import ru.gravit.launcher.LauncherAPI; 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 efe6cce8..27b6965a 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 @@ -1,5 +1,9 @@ package ru.gravit.launchserver.socket.websocket; +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; @@ -7,13 +11,25 @@ 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; + public class WebSocketFrameHandler extends SimpleChannelInboundHandler { static final ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); - + static Gson gson; + static GsonBuilder builder = new GsonBuilder(); + static { + builder.registerTypeAdapter(JsonResponse.class,new JsonResponseAdapter()); + gson = builder.create(); + } @Override public void channelActive(ChannelHandlerContext ctx) { LogHelper.debug("New client %s", IOHelper.getIP(ctx.channel().remoteAddress())); @@ -25,9 +41,8 @@ protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) thr // ping and pong frames already handled ByteBuf buf = frame.content(); ByteBufInputStream input = new ByteBufInputStream(buf); - long handshake = input.readLong(); - long connection_flags = input.readLong(); - long type = input.readInt(); - LogHelper.debug("MessageHead: handshake %dl, flags %dl, type %d", handshake, connection_flags, type); + Reader reader = new InputStreamReader(input, "UTF-8"); + JsonResponse response = gson.fromJson(reader,JsonResponse.class); + response.execute(ctx,frame); } } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/JsonResponse.java b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/JsonResponse.java new file mode 100644 index 00000000..0896c639 --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/JsonResponse.java @@ -0,0 +1,9 @@ +package ru.gravit.launchserver.socket.websocket.json; + +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.http.websocketx.WebSocketFrame; + +public interface JsonResponse { + String getType(); + void execute(ChannelHandlerContext ctx, WebSocketFrame frame); +} 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 new file mode 100644 index 00000000..8dadf58e --- /dev/null +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/socket/websocket/json/JsonResponseAdapter.java @@ -0,0 +1,30 @@ +package ru.gravit.launchserver.socket.websocket.json; + +import com.google.gson.*; + +import java.lang.reflect.Type; +import java.util.HashMap; + +public class JsonResponseAdapter implements JsonSerializer, JsonDeserializer { + static HashMap> map = new HashMap<>(); + private static final String PROP_NAME = "type"; + @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); + + + return (JsonResponse) context.deserialize(json, cls); + } + + @Override + public JsonElement serialize(JsonResponse 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; + } +}