Commit 258bc0aa by pye52

回滚旧版本,以发布生产包

parent 94dcef0e
...@@ -48,55 +48,24 @@ public class MainWebSocket extends WebSocketClient { ...@@ -48,55 +48,24 @@ public class MainWebSocket extends WebSocketClient {
public static final String CODE_FAIL = "-1"; public static final String CODE_FAIL = "-1";
private static MainWebSocket sInstance; private static MainWebSocket sInstance;
private static long lastLoopId = -1;
private static String sDeviceSN; private static String sDeviceSN;
private static int sReconnectTimes = 0; private static int sReconnectTimes = 0;
private static int sAutoCheckCount = 0; private static int sAutoCheckCount = 0;
public static void initialize() { public static void initialize() {
if (sInstance != null) { if (sInstance == null) {
return; try {
}
sDeviceSN = App.Companion.getDeviceSN(); sDeviceSN = App.Companion.getDeviceSN();
if (sDeviceSN.isEmpty()) { if (sDeviceSN.isEmpty()) return;
LogUtils.file(TAG, "device id is empty");
return;
}
EventBus.getDefault().post(new ConnectStateEvent(ConnectStateEvent.CONNECTING)); EventBus.getDefault().post(new ConnectStateEvent(ConnectStateEvent.CONNECTING));
String host = AppConfig.INSTANCE.getMainWebSocketServerUrl(sDeviceSN, BuildConfig.VERSION_NAME); sInstance = new MainWebSocket(new URI(AppConfig.INSTANCE.getMainWebSocketServerUrl(sDeviceSN, BuildConfig.VERSION_NAME)));
LogUtil.i(TAG, "try connecting to host: " + host);
try {
sInstance = new MainWebSocket(new URI(host));
} catch (URISyntaxException e) {
LogUtil.i(TAG, "invalidate host: " + host, e);
}
sInstance.setConnectionLostTimeout(10); sInstance.setConnectionLostTimeout(10);
sInstance.connect(); sInstance.connect();
// 保证只有一个loop在运行 TimerHelper.INSTANCE.loop(new TimerHelper.LoopTask() {
if (lastLoopId != -1) {
TimerHelper.INSTANCE.cancel(lastLoopId);
}
lastLoopId = TimerHelper.INSTANCE.loop(new TimerHelper.LoopTask() {
@Override @Override
public void run(long id, boolean isLastTime) { public void run(long id, boolean isLastTime) {
try {
if (sInstance == null) {
LogUtils.file(TAG, "instance of socket could not be null!!!");
initialize();
return;
}
if (sInstance.isClosed()) { if (sInstance.isClosed()) {
doReconnect();
} else {
doHeartbeat();
}
} catch (Exception e) {
LogUtils.file(TAG, "fatal error: " + e.getMessage());
}
}
private void doReconnect() {
if (sReconnectTimes < 2) { if (sReconnectTimes < 2) {
sReconnectTimes++; sReconnectTimes++;
sInstance.reconnect(); sInstance.reconnect();
...@@ -107,9 +76,7 @@ public class MainWebSocket extends WebSocketClient { ...@@ -107,9 +76,7 @@ public class MainWebSocket extends WebSocketClient {
EventBus.getDefault().post(new ConnectStateEvent(ConnectStateEvent.CHANGE_NETWORK)); EventBus.getDefault().post(new ConnectStateEvent(ConnectStateEvent.CHANGE_NETWORK));
} }
sAutoCheckCount = 0; sAutoCheckCount = 0;
} } else {
private void doHeartbeat() {
// 每10分钟发一次心跳包给后端 // 每10分钟发一次心跳包给后端
sAutoCheckCount -= 2; sAutoCheckCount -= 2;
if (sAutoCheckCount < 0) { if (sAutoCheckCount < 0) {
...@@ -123,7 +90,12 @@ public class MainWebSocket extends WebSocketClient { ...@@ -123,7 +90,12 @@ public class MainWebSocket extends WebSocketClient {
} }
} }
} }
}
}, 2000, -1, 2000); }, 2000, -1, 2000);
} catch (URISyntaxException e) {
LogUtils.file(TAG, "fatal error: " + e.getMessage());
}
}
} }
public static boolean isInited() { public static boolean isInited() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment