From f0a444d04d5286aed00f0e3d6e0866de21e62f32 Mon Sep 17 00:00:00 2001 From: czp Date: Wed, 31 Jan 2018 10:46:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=96=E6=B6=88=E8=87=AA=E5=8A=A8=E9=87=8D?= =?UTF-8?q?=E8=BF=9E=E6=9C=BA=E5=88=B6,=20=E9=87=8D=E8=BF=9E=E7=94=B1?= =?UTF-8?q?=E6=9C=80=E7=BB=88=E7=94=A8=E6=88=B7=E8=BF=9B=E8=A1=8C=E6=8E=A7?= =?UTF-8?q?=E5=88=B6=E9=81=BF=E5=85=8D=E8=80=A6=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bilibili/api/live/socket/LiveClient.java | 69 ++----------------- .../bilibili/api/test/LiveClientTest.java | 7 +- 2 files changed, 6 insertions(+), 70 deletions(-) diff --git a/src/main/java/com/hiczp/bilibili/api/live/socket/LiveClient.java b/src/main/java/com/hiczp/bilibili/api/live/socket/LiveClient.java index 139aec8..4e55ffd 100644 --- a/src/main/java/com/hiczp/bilibili/api/live/socket/LiveClient.java +++ b/src/main/java/com/hiczp/bilibili/api/live/socket/LiveClient.java @@ -1,13 +1,10 @@ package com.hiczp.bilibili.api.live.socket; import com.google.common.eventbus.EventBus; -import com.google.common.eventbus.Subscribe; import com.hiczp.bilibili.api.BilibiliServiceProvider; import com.hiczp.bilibili.api.live.entity.LiveRoomInfoEntity; import com.hiczp.bilibili.api.live.socket.codec.PackageDecoder; import com.hiczp.bilibili.api.live.socket.codec.PackageEncoder; -import com.hiczp.bilibili.api.live.socket.event.ConnectSucceedEvent; -import com.hiczp.bilibili.api.live.socket.event.ConnectionCloseEvent; import com.hiczp.bilibili.api.live.socket.handler.LiveClientHandler; import io.netty.bootstrap.Bootstrap; import io.netty.channel.Channel; @@ -24,7 +21,6 @@ import org.slf4j.LoggerFactory; import java.io.Closeable; import java.io.IOException; import java.util.Optional; -import java.util.concurrent.TimeUnit; public class LiveClient implements Closeable { private static final Logger LOGGER = LoggerFactory.getLogger(LiveClient.class); @@ -34,12 +30,8 @@ public class LiveClient implements Closeable { private final long showRoomId; private final long userId; - private long reconnectLimit = 0; - private long reconnectAttempt = 0; - private long reconnectDelay = 5L; - private boolean userWantClose = false; - private Long roomId; + private EventLoopGroup eventLoopGroup; private Channel channel; @@ -47,18 +39,12 @@ public class LiveClient implements Closeable { this.bilibiliServiceProvider = bilibiliServiceProvider; this.showRoomId = showRoomId; this.userId = 0; - initEventBus(); } public LiveClient(BilibiliServiceProvider bilibiliServiceProvider, long showRoomId, long userId) { this.bilibiliServiceProvider = bilibiliServiceProvider; this.showRoomId = showRoomId; this.userId = userId; - initEventBus(); - } - - private void initEventBus() { - eventBus.register(this); } public synchronized LiveClient connect() throws IOException { @@ -67,7 +53,9 @@ public class LiveClient implements Closeable { return this; } - reconnectAttempt++; + if (eventLoopGroup != null) { + eventLoopGroup.shutdownGracefully(); + } LOGGER.info("Fetching info of live room {}", showRoomId); LiveRoomInfoEntity.LiveRoomEntity liveRoomEntity = bilibiliServiceProvider.getLiveService() @@ -78,9 +66,6 @@ public class LiveClient implements Closeable { roomId = liveRoomEntity.getRoomId(); LOGGER.info("Get actual room id {}", roomId); - if (eventLoopGroup != null) { - eventLoopGroup.shutdownGracefully(); - } eventLoopGroup = new NioEventLoopGroup(1); LOGGER.debug("Init SocketChannel Bootstrap"); Bootstrap bootstrap = new Bootstrap() @@ -125,7 +110,6 @@ public class LiveClient implements Closeable { @Override public synchronized void close() { - userWantClose = true; if (eventLoopGroup != null) { LOGGER.info("Closing connection"); try { @@ -137,41 +121,6 @@ public class LiveClient implements Closeable { } } - @Subscribe - public void onConnectSucceed(ConnectSucceedEvent connectSucceedEvent) { - LOGGER.info("Connect succeed"); - if (userWantClose) { - reconnectAttempt = 0; - } - } - - @Subscribe - public void onConnectionClose(ConnectionCloseEvent connectionCloseEvent) { - LOGGER.info("Connection closed"); - if (!userWantClose && reconnectAttempt <= reconnectLimit) { - LOGGER.info("Reconnect attempt {}, limit {}", reconnectAttempt, reconnectLimit); - LOGGER.info("Auto reconnect after {} second", reconnectDelay); - eventLoopGroup.schedule( - () -> { - try { - connect(); - } catch (IOException e) { - e.printStackTrace(); - } - }, - reconnectDelay, - TimeUnit.SECONDS - ); - } else { - eventLoopGroup.shutdownGracefully(); - } - - if (userWantClose) { - userWantClose = false; - reconnectAttempt = 0; - } - } - public EventBus getEventBus() { return eventBus; } @@ -197,14 +146,4 @@ public class LiveClient implements Closeable { public Optional getRoomId() { return Optional.of(roomId); } - - public LiveClient setReconnectLimit(long reconnectLimit) { - this.reconnectLimit = reconnectLimit; - return this; - } - - public LiveClient setReconnectDelay(long reconnectDelay) { - this.reconnectDelay = reconnectDelay; - return this; - } } diff --git a/src/test/java/com/hiczp/bilibili/api/test/LiveClientTest.java b/src/test/java/com/hiczp/bilibili/api/test/LiveClientTest.java index 4af954a..558752e 100644 --- a/src/test/java/com/hiczp/bilibili/api/test/LiveClientTest.java +++ b/src/test/java/com/hiczp/bilibili/api/test/LiveClientTest.java @@ -25,9 +25,7 @@ public class LiveClientTest { @Test public void _0_duplicateConnectAndCloseTest() throws Exception { LiveClient liveClient = BILIBILI_API - .getLiveClient(ROOM_ID) - .setReconnectLimit(5) - .setReconnectDelay(1); + .getLiveClient(ROOM_ID); LOGGER.debug("Connecting!"); liveClient.connect(); Thread.sleep(5000); @@ -48,12 +46,11 @@ public class LiveClientTest { Thread.sleep(5000); } + @Ignore @Test public void _1_longTimeTest() throws Exception { LiveClient liveClient = BILIBILI_API .getLiveClient(ROOM_ID) - .setReconnectLimit(5) - .setReconnectDelay(1) .registerListener(new Listener()); LOGGER.debug("Start long-time test"); LOGGER.debug("Connecting!");