diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/WebSocketFrameHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/WebSocketFrameHandler.java index a86cdeb9..0d75f4da 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/WebSocketFrameHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/handlers/WebSocketFrameHandler.java @@ -4,6 +4,7 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.handler.codec.http.websocketx.*; +import io.netty.util.concurrent.ScheduledFuture; import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.socket.Client; import pro.gravit.launchserver.socket.NettyConnectContext; @@ -23,6 +24,7 @@ public class WebSocketFrameHandler extends SimpleChannelInboundHandler hooks = new BiHookSet<>(); private Client client; + private ScheduledFuture future; public WebSocketFrameHandler(NettyConnectContext context, LaunchServer srv, WebSocketService service) { this.context = context; @@ -50,7 +52,7 @@ public void channelActive(ChannelHandlerContext ctx) { client = new Client(null); Channel ch = ctx.channel(); service.registerClient(ch); - ctx.executor().scheduleAtFixedRate(() -> ch.writeAndFlush(new PingWebSocketFrame(), ch.voidPromise()), 30L, 30L, TimeUnit.SECONDS); + future = ctx.executor().scheduleAtFixedRate(() -> ch.writeAndFlush(new PingWebSocketFrame(), ch.voidPromise()), 30L, 30L, TimeUnit.SECONDS); } @Override @@ -92,4 +94,10 @@ protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) { LogHelper.error(new UnsupportedOperationException(message)); // prevent strange crash here. } } + + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + if(future != null) future.cancel(true); + super.channelInactive(ctx); + } }