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

73 lines
2.8 KiB
Java
Raw Normal View History

package pro.gravit.launchserver.websocket;
2019-06-03 10:58:10 +03:00
import java.util.concurrent.TimeUnit;
2018-10-01 09:45:36 +03:00
import com.google.gson.GsonBuilder;
2019-06-03 10:58:10 +03:00
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.DefaultChannelGroup;
2019-06-03 10:58:10 +03:00
import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
import io.netty.handler.codec.http.websocketx.PingWebSocketFrame;
import io.netty.handler.codec.http.websocketx.PongWebSocketFrame;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import io.netty.util.concurrent.GlobalEventExecutor;
import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.socket.Client;
import pro.gravit.utils.helper.CommonHelper;
import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.LogHelper;
2018-11-08 15:30:16 +03:00
public class WebSocketFrameHandler extends SimpleChannelInboundHandler<WebSocketFrame> {
public final LaunchServer srv;
public static GsonBuilder builder = CommonHelper.newBuilder();
public final WebSocketService service;
2019-05-09 16:00:10 +03:00
public NettyConnectContext context;
public WebSocketFrameHandler(NettyConnectContext context, LaunchServer srv) {
2019-05-09 16:00:10 +03:00
this.context = context;
this.srv = srv;
service = new WebSocketService(new DefaultChannelGroup(GlobalEventExecutor.INSTANCE), srv, builder);
service.registerResponses();
2019-05-09 16:00:10 +03:00
}
private Client client;
2018-11-08 15:30:16 +03:00
2018-10-01 09:45:36 +03:00
static {
}
2019-05-15 14:11:22 +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.dev("New client %s", IOHelper.getIP(ctx.channel().remoteAddress()));
client = new Client(0);
2018-10-01 13:01:10 +03:00
service.registerClient(ctx.channel());
ctx.executor().schedule(() -> {
ctx.channel().writeAndFlush(new PingWebSocketFrame());
}, 30L, TimeUnit.SECONDS);
}
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) {
2019-05-09 16:05:53 +03:00
service.process(ctx, (TextWebSocketFrame) frame, client, context.ip);
} else if ((frame instanceof PingWebSocketFrame)) {
frame.content().retain();
ctx.channel().writeAndFlush(new PongWebSocketFrame(frame.content()));
//return;
2019-05-15 14:11:22 +03:00
} else if ((frame instanceof PongWebSocketFrame)) {
LogHelper.dev("WebSocket Client received pong");
2019-05-15 14:11:22 +03:00
} else if ((frame instanceof CloseWebSocketFrame)) {
ctx.channel().close();
} else {
String message = "unsupported frame type: " + frame.getClass().getName();
LogHelper.error(new UnsupportedOperationException(message)); // prevent strange crash here.
}
}
}