Commit 9fca2e6b by pye52

增加离线防抖机制,避免因为离线支付导致的链接断开影响了在线支付

parent 98e1670a
......@@ -195,7 +195,7 @@ public class SCWebSocketClient extends WebSocketClient {
@Override
public void onClose(int code, String reason, boolean remote) {
LogUtils.w(TAG, "socket已关闭, 原因: " + reason + ", 是否服务器断开链接: " + remote);
LogUtils.w(TAG, "socket已关闭: " + code + ", 原因: " + reason + ", 是否服务器断开链接: " + remote);
connectState.postValue(new ConnectState(ConnectState.OFFLINE));
for (SCWebSocketListener l : listener) {
l.onClose(code, reason, remote);
......
......@@ -41,7 +41,6 @@ public class PayOfflineViewModel extends ViewModel {
// 在线支付延迟150ms执行,留出时间给扫码反馈
private static final long REQUEST_DELAY = 150;
private static final long DEFAULT_DELAY = 3 * 1000;
private static final long PAY_INTERVAL = 60 * 60 * 1000;
private Gson gson;
private String deviceSN;
private PayDataRepository payDataRepository;
......@@ -54,7 +53,6 @@ public class PayOfflineViewModel extends ViewModel {
}
private PayRequest payRequest;
private long lastExecTime = -1;
private ScheduledFuture<?> payOfflineFuture;
private ScheduledFuture<?> timeoutFuture;
......@@ -78,13 +76,6 @@ public class PayOfflineViewModel extends ViewModel {
}
private void traversalPayOfflineData() {
// 若与上次执行相隔太近,则跳过
long currentTime = System.currentTimeMillis();
if ((currentTime - lastExecTime) < PAY_INTERVAL) {
LogUtils.d(TAG, "离线检测过于频繁");
return;
}
lastExecTime = currentTime;
cancelPayOffline();
cancelTimeout();
TimeoutRunnable timeoutRunnable = new TimeoutRunnable();
......@@ -111,11 +102,21 @@ public class PayOfflineViewModel extends ViewModel {
private static final String RESPONSE_PAY_OFFLINE_RESULT = "PAY_OFFLINE_RESULT";
// 每心跳多少次后进行一次离线订单检测
private static final int PAY_OFFLINE_CHECK = 30;
// 当链接频繁断开时,给予离线订单检测一定缓冲时间
private static final long PAY_OFFLINE_INTERVAL = 30 * 60 * 1000;
private long lastPayTime = -1;
private int heartbeatCount = 0;
@Override
public void onOpen(ServerHandshake data) {
heartbeatCount = 0;
long currentTime = System.currentTimeMillis();
if ((currentTime - lastPayTime) < PAY_OFFLINE_INTERVAL) {
LogUtils.w(TAG, "离线检测过于频繁,可能是链接频繁断开导致");
return;
}
lastPayTime = currentTime;
traversalPayOfflineData();
}
......@@ -125,6 +126,12 @@ public class PayOfflineViewModel extends ViewModel {
heartbeatCount++;
if (heartbeatCount >= PAY_OFFLINE_CHECK) {
heartbeatCount = 0;
long currentTime = System.currentTimeMillis();
if ((currentTime - lastPayTime) < PAY_OFFLINE_INTERVAL) {
LogUtils.w(TAG, "心跳引发离线检测冲突,跳过该次检测");
return;
}
lastPayTime = currentTime;
traversalPayOfflineData();
}
}
......
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