Правильная инициализация WebSocketService

This commit is contained in:
Gravit 2018-10-01 15:55:52 +07:00
parent b76e009aaa
commit a32402f1a6
3 changed files with 34 additions and 14 deletions

View file

@ -10,21 +10,16 @@
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import io.netty.util.concurrent.GlobalEventExecutor;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.socket.websocket.json.JsonResponseInterface;
import ru.gravit.launchserver.socket.websocket.json.JsonResponseAdapter;
import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.LogHelper;
public class WebSocketFrameHandler extends SimpleChannelInboundHandler<WebSocketFrame> {
public static final ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
public static Gson gson;
public WebSocketService service = new WebSocketService(LaunchServer.server,gson);
public static LaunchServer server;
public static GsonBuilder builder = new GsonBuilder();
public static WebSocketService service = new WebSocketService(LaunchServer.server,builder);
static {
builder.registerTypeAdapter(JsonResponseInterface.class,new JsonResponseAdapter());
gson = builder.create();
service.registerResponses();
}
@Override
public void channelActive(ChannelHandlerContext ctx) {

View file

@ -1,20 +1,30 @@
package ru.gravit.launchserver.socket.websocket;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.socket.websocket.json.EchoResponse;
import ru.gravit.launchserver.socket.websocket.json.JsonResponseAdapter;
import ru.gravit.launchserver.socket.websocket.json.JsonResponseInterface;
import ru.gravit.launchserver.socket.websocket.json.auth.AuthResponse;
import ru.gravit.utils.helper.LogHelper;
import java.util.HashMap;
public class WebSocketService {
public WebSocketService(LaunchServer server, Gson gson) {
public WebSocketService(LaunchServer server, GsonBuilder gson) {
this.server = server;
this.gson = gson;
this.gsonBuiler = gson;
gsonBuiler.registerTypeAdapter(JsonResponseInterface.class,new JsonResponseAdapter(this));
this.gson = gsonBuiler.create();
}
private final LaunchServer server;
private static final HashMap<String,Class> responses = new HashMap<>();
private final Gson gson;
private final GsonBuilder gsonBuiler;
void process(ChannelHandlerContext ctx, TextWebSocketFrame frame)
{
@ -28,6 +38,19 @@ void process(ChannelHandlerContext ctx, TextWebSocketFrame frame)
sendObject(ctx,new ExceptionResult(e));
}
}
public Class getResponseClass(String type)
{
return responses.get(type);
}
public void registerResponse(String key,Class responseInterfaceClass)
{
responses.put(key,responseInterfaceClass);
}
public void registerResponses()
{
registerResponse("echo", EchoResponse.class);
registerResponse("auth", AuthResponse.class);
}
public void sendObject(ChannelHandlerContext ctx, Object obj)
{
ctx.channel().writeAndFlush(new TextWebSocketFrame(gson.toJson(obj)));

View file

@ -1,20 +1,22 @@
package ru.gravit.launchserver.socket.websocket.json;
import com.google.gson.*;
import ru.gravit.launchserver.socket.websocket.WebSocketService;
import java.lang.reflect.Type;
import java.util.HashMap;
public class JsonResponseAdapter implements JsonSerializer<JsonResponseInterface>, JsonDeserializer<JsonResponseInterface> {
static HashMap<String,Class> map = new HashMap<>();
private final WebSocketService service;
private static final String PROP_NAME = "type";
static {
map.put("echo",EchoResponse.class);
public JsonResponseAdapter(WebSocketService service) {
this.service = service;
}
@Override
public JsonResponseInterface deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString();
Class cls = map.get(typename);
Class<JsonResponseInterface> cls = service.getResponseClass(typename);
return (JsonResponseInterface) context.deserialize(json, cls);