Commit 9fca2e6b by pye52

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

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