2019-06-02 05:03:08 +03:00
|
|
|
package pro.gravit.launchserver.websocket;
|
2018-09-21 15:38:39 +03:00
|
|
|
|
2018-10-01 09:45:36 +03:00
|
|
|
import com.google.gson.GsonBuilder;
|
2018-09-21 15:38:39 +03:00
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
|
|
import io.netty.channel.SimpleChannelInboundHandler;
|
2018-09-21 17:36:23 +03:00
|
|
|
import io.netty.channel.group.DefaultChannelGroup;
|
2019-04-06 10:02:06 +03:00
|
|
|
import io.netty.handler.codec.http.websocketx.*;
|
2018-09-21 17:36:23 +03:00
|
|
|
import io.netty.util.concurrent.GlobalEventExecutor;
|
2019-06-02 05:03:08 +03:00
|
|
|
import pro.gravit.launchserver.LaunchServer;
|
|
|
|
import pro.gravit.launchserver.socket.Client;
|
2019-06-03 10:56:00 +03:00
|
|
|
import pro.gravit.utils.helper.CommonHelper;
|
2019-06-02 05:03:08 +03:00
|
|
|
import pro.gravit.utils.helper.IOHelper;
|
|
|
|
import pro.gravit.utils.helper.LogHelper;
|
2018-11-08 15:30:16 +03:00
|
|
|
|
2019-04-06 10:02:06 +03:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
2018-09-21 15:38:39 +03:00
|
|
|
public class WebSocketFrameHandler extends SimpleChannelInboundHandler<WebSocketFrame> {
|
2018-10-01 10:48:24 +03:00
|
|
|
public static LaunchServer server;
|
2019-06-03 10:56:00 +03:00
|
|
|
public static GsonBuilder builder = CommonHelper.newBuilder();
|
2018-11-08 15:30:16 +03:00
|
|
|
public static WebSocketService service = new WebSocketService(new DefaultChannelGroup(GlobalEventExecutor.INSTANCE), LaunchServer.server, builder);
|
2019-05-09 16:00:10 +03:00
|
|
|
public NettyConnectContext context;
|
|
|
|
|
|
|
|
public WebSocketFrameHandler(NettyConnectContext context) {
|
|
|
|
this.context = context;
|
|
|
|
}
|
|
|
|
|
2018-10-01 12:42:42 +03:00
|
|
|
private Client client;
|
2018-11-08 15:30:16 +03:00
|
|
|
|
2018-10-01 09:45:36 +03:00
|
|
|
static {
|
2018-10-01 11:55:52 +03:00
|
|
|
service.registerResponses();
|
2018-10-01 09:45:36 +03:00
|
|
|
}
|
2019-05-15 14:11:22 +03:00
|
|
|
|
|
|
|
public void setClient(Client client) {
|
2019-04-04 09:50:04 +03:00
|
|
|
this.client = client;
|
|
|
|
}
|
2018-11-08 15:30:16 +03:00
|
|
|
|
2018-09-21 17:36:23 +03:00
|
|
|
@Override
|
|
|
|
public void channelActive(ChannelHandlerContext ctx) {
|
2019-05-07 14:48:09 +03:00
|
|
|
LogHelper.dev("New client %s", IOHelper.getIP(ctx.channel().remoteAddress()));
|
2018-10-01 12:42:42 +03:00
|
|
|
client = new Client(0);
|
2018-10-01 13:01:10 +03:00
|
|
|
service.registerClient(ctx.channel());
|
2019-04-06 10:02:06 +03:00
|
|
|
ctx.executor().schedule(() -> {
|
|
|
|
ctx.channel().writeAndFlush(new PingWebSocketFrame());
|
|
|
|
}, 30L, TimeUnit.SECONDS);
|
2018-09-21 17:36:23 +03:00
|
|
|
}
|
2018-09-22 17:33:00 +03:00
|
|
|
|
2018-09-21 15:38:39 +03:00
|
|
|
@Override
|
2019-01-15 06:32:46 +03:00
|
|
|
protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) {
|
2018-09-21 15:38:39 +03:00
|
|
|
// ping and pong frames already handled
|
2018-10-01 10:55:09 +03:00
|
|
|
if (frame instanceof TextWebSocketFrame) {
|
2019-05-09 16:05:53 +03:00
|
|
|
service.process(ctx, (TextWebSocketFrame) frame, client, context.ip);
|
2019-04-06 10:02:06 +03:00
|
|
|
} 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)) {
|
2019-04-06 10:02:06 +03:00
|
|
|
LogHelper.dev("WebSocket Client received pong");
|
2019-05-15 14:11:22 +03:00
|
|
|
} else if ((frame instanceof CloseWebSocketFrame)) {
|
2019-04-06 10:02:06 +03:00
|
|
|
ctx.channel().close();
|
2018-10-01 10:55:09 +03:00
|
|
|
} else {
|
|
|
|
String message = "unsupported frame type: " + frame.getClass().getName();
|
|
|
|
throw new UnsupportedOperationException(message);
|
|
|
|
}
|
2018-09-21 15:38:39 +03:00
|
|
|
}
|
|
|
|
}
|