Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
huangzhicong
/
SmartCanteen
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
9fca2e6b
authored
May 12, 2020
by
pye52
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加离线防抖机制,避免因为离线支付导致的链接断开影响了在线支付
parent
98e1670a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
17 additions
and
10 deletions
+17
-10
app/src/main/java/com/bgycc/smartcanteen/socket/SCWebSocketClient.java
+1
-1
app/src/main/java/com/bgycc/smartcanteen/viewModel/PayOfflineViewModel.java
+16
-9
No files found.
app/src/main/java/com/bgycc/smartcanteen/socket/SCWebSocketClient.java
View file @
9fca2e6b
...
...
@@ -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
);
...
...
app/src/main/java/com/bgycc/smartcanteen/viewModel/PayOfflineViewModel.java
View file @
9fca2e6b
...
...
@@ -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
();
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment