Launcher/LaunchServer/src/main/java/ru/gravit/launchserver/websocket/WebSocketFrameHandler.java

48 lines
1.7 KiB
Java
Raw Normal View History

package ru.gravit.launchserver.websocket;
2018-10-01 09:45:36 +03:00
import com.google.gson.GsonBuilder;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import io.netty.util.concurrent.GlobalEventExecutor;
2018-10-01 10:48:24 +03:00
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.socket.Client;
import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.LogHelper;
2018-11-08 15:30:16 +03:00
public class WebSocketFrameHandler extends SimpleChannelInboundHandler<WebSocketFrame> {
2018-10-01 10:48:24 +03:00
public static LaunchServer server;
public static GsonBuilder builder = new GsonBuilder();
2018-11-08 15:30:16 +03:00
public static WebSocketService service = new WebSocketService(new DefaultChannelGroup(GlobalEventExecutor.INSTANCE), LaunchServer.server, builder);
private Client client;
2018-11-08 15:30:16 +03:00
2018-10-01 09:45:36 +03:00
static {
service.registerResponses();
2018-10-01 09:45:36 +03:00
}
public void setClient(Client client)
{
this.client = client;
}
2018-11-08 15:30:16 +03:00
@Override
public void channelActive(ChannelHandlerContext ctx) {
LogHelper.debug("New client %s", IOHelper.getIP(ctx.channel().remoteAddress()));
client = new Client(0);
2018-10-01 13:01:10 +03:00
service.registerClient(ctx.channel());
}
2018-09-22 17:33:00 +03:00
@Override
2019-01-15 06:32:46 +03:00
protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) {
// ping and pong frames already handled
if (frame instanceof TextWebSocketFrame) {
service.process(ctx, (TextWebSocketFrame) frame, client);
} else {
String message = "unsupported frame type: " + frame.getClass().getName();
throw new UnsupportedOperationException(message);
}
}
}