mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-11-16 12:09:12 +03:00
64 lines
2.4 KiB
Java
64 lines
2.4 KiB
Java
package pro.gravit.launchserver.websocket;
|
|
|
|
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.*;
|
|
import io.netty.util.concurrent.GlobalEventExecutor;
|
|
import pro.gravit.launchserver.LaunchServer;
|
|
import pro.gravit.launchserver.socket.Client;
|
|
import pro.gravit.utils.helper.IOHelper;
|
|
import pro.gravit.utils.helper.LogHelper;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
public class WebSocketFrameHandler extends SimpleChannelInboundHandler<WebSocketFrame> {
|
|
public static LaunchServer server;
|
|
public static GsonBuilder builder = new GsonBuilder();
|
|
public static WebSocketService service = new WebSocketService(new DefaultChannelGroup(GlobalEventExecutor.INSTANCE), LaunchServer.server, builder);
|
|
public NettyConnectContext context;
|
|
|
|
public WebSocketFrameHandler(NettyConnectContext context) {
|
|
this.context = context;
|
|
}
|
|
|
|
private Client client;
|
|
|
|
static {
|
|
service.registerResponses();
|
|
}
|
|
|
|
public void setClient(Client client) {
|
|
this.client = client;
|
|
}
|
|
|
|
@Override
|
|
public void channelActive(ChannelHandlerContext ctx) {
|
|
LogHelper.dev("New client %s", IOHelper.getIP(ctx.channel().remoteAddress()));
|
|
client = new Client(0);
|
|
service.registerClient(ctx.channel());
|
|
ctx.executor().schedule(() -> {
|
|
ctx.channel().writeAndFlush(new PingWebSocketFrame());
|
|
}, 30L, TimeUnit.SECONDS);
|
|
}
|
|
|
|
@Override
|
|
protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) {
|
|
// ping and pong frames already handled
|
|
if (frame instanceof TextWebSocketFrame) {
|
|
service.process(ctx, (TextWebSocketFrame) frame, client, context.ip);
|
|
} else if ((frame instanceof PingWebSocketFrame)) {
|
|
frame.content().retain();
|
|
ctx.channel().writeAndFlush(new PongWebSocketFrame(frame.content()));
|
|
//return;
|
|
} else if ((frame instanceof PongWebSocketFrame)) {
|
|
LogHelper.dev("WebSocket Client received pong");
|
|
} else if ((frame instanceof CloseWebSocketFrame)) {
|
|
ctx.channel().close();
|
|
} else {
|
|
String message = "unsupported frame type: " + frame.getClass().getName();
|
|
throw new UnsupportedOperationException(message);
|
|
}
|
|
}
|
|
}
|