WebSocket json

This commit is contained in:
Gravit 2018-10-01 13:45:36 +07:00
parent e4b0c1c646
commit 3dfde49ac9
4 changed files with 60 additions and 5 deletions

View file

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

View file

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

View file

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

View file

@ -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<JsonResponse>, JsonDeserializer<JsonResponse> {
static HashMap<String,Class<JsonResponse>> 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<JsonResponse> 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;
}
}