mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-12-23 09:01:08 +03:00
WebSocket json
This commit is contained in:
parent
e4b0c1c646
commit
3dfde49ac9
4 changed files with 60 additions and 5 deletions
|
@ -12,6 +12,7 @@
|
||||||
import io.netty.handler.codec.http.HttpServerCodec;
|
import io.netty.handler.codec.http.HttpServerCodec;
|
||||||
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
|
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
|
||||||
import io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketServerCompressionHandler;
|
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.LogLevel;
|
||||||
import io.netty.handler.logging.LoggingHandler;
|
import io.netty.handler.logging.LoggingHandler;
|
||||||
import ru.gravit.launcher.LauncherAPI;
|
import ru.gravit.launcher.LauncherAPI;
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
package ru.gravit.launchserver.socket.websocket;
|
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.ByteBuf;
|
||||||
import io.netty.buffer.ByteBufInputStream;
|
import io.netty.buffer.ByteBufInputStream;
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
|
@ -7,13 +11,25 @@
|
||||||
import io.netty.channel.group.ChannelGroup;
|
import io.netty.channel.group.ChannelGroup;
|
||||||
import io.netty.channel.group.DefaultChannelGroup;
|
import io.netty.channel.group.DefaultChannelGroup;
|
||||||
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
|
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
|
||||||
|
import io.netty.handler.codec.json.JsonObjectDecoder;
|
||||||
import io.netty.util.concurrent.GlobalEventExecutor;
|
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.IOHelper;
|
||||||
import ru.gravit.utils.helper.LogHelper;
|
import ru.gravit.utils.helper.LogHelper;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.Reader;
|
||||||
|
|
||||||
public class WebSocketFrameHandler extends SimpleChannelInboundHandler<WebSocketFrame> {
|
public class WebSocketFrameHandler extends SimpleChannelInboundHandler<WebSocketFrame> {
|
||||||
static final ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
|
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
|
@Override
|
||||||
public void channelActive(ChannelHandlerContext ctx) {
|
public void channelActive(ChannelHandlerContext ctx) {
|
||||||
LogHelper.debug("New client %s", IOHelper.getIP(ctx.channel().remoteAddress()));
|
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
|
// ping and pong frames already handled
|
||||||
ByteBuf buf = frame.content();
|
ByteBuf buf = frame.content();
|
||||||
ByteBufInputStream input = new ByteBufInputStream(buf);
|
ByteBufInputStream input = new ByteBufInputStream(buf);
|
||||||
long handshake = input.readLong();
|
Reader reader = new InputStreamReader(input, "UTF-8");
|
||||||
long connection_flags = input.readLong();
|
JsonResponse response = gson.fromJson(reader,JsonResponse.class);
|
||||||
long type = input.readInt();
|
response.execute(ctx,frame);
|
||||||
LogHelper.debug("MessageHead: handshake %dl, flags %dl, type %d", handshake, connection_flags, type);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue