From f9521b41a2a8081b8a5804f55e4b17e2b1e4ff05 Mon Sep 17 00:00:00 2001 From: Gravit Date: Sat, 26 Sep 2020 17:06:09 +0700 Subject: [PATCH] =?UTF-8?q?[FIX]=20=D0=A3=D1=82=D0=B5=D1=87=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=BF=D0=B0=D0=BC=D1=8F=D1=82=D0=B8=20=D0=B8=D0=B7=20=D0=B7?= =?UTF-8?q?=D0=B0=20=D0=BF=D0=B8=D0=BD=D0=B3=D0=BE=D0=B2(!)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../socket/handlers/WebSocketFrameHandler.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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); + } }