Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
merchant-manage-ui
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ui
merchant-manage-ui
Commits
ac02eb28
Commit
ac02eb28
authored
Nov 02, 2022
by
李腾
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: websocket新增heart beat方法
parent
d61e98b9
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
35 deletions
+25
-35
BasicLayout.jsx
src/layouts/BasicLayout.jsx
+2
-25
websocket.js
src/utils/websocket.js
+23
-10
No files found.
src/layouts/BasicLayout.jsx
View file @
ac02eb28
...
...
@@ -63,11 +63,8 @@ const BasicLayout = props => {
url
:
getSocketUrl
({
token
,
channelId
}),
});
socket
.
connection
();
socket
.
event
.
on
(
'
open
'
,
()
=>
{
console
.
log
(
'
wesocket连接成功
'
);
});
socket
.
event
.
on
(
'
message
'
,
msg
=>
{
console
.
log
(
msg
);
dispatch
({
type
:
'
messageReminder/setUnReadData
'
,
payload
:
[
JSON
.
parse
(
msg
.
data
)],
...
...
@@ -76,22 +73,7 @@ const BasicLayout = props => {
}
catch
(
e
)
{
console
.
log
(
e
);
}
// let a = 100000000000;
// setInterval(() => {
// a++;
// socket.sendMessage(
// {
// id: a,
// channelId: 100001,
// bussinessId: 'self_40',
// type: 0,
// sendContent: `{"name":"商品21111","title":"商品21111", "orderNo":${a},"createdAt":"2022-10-13 12:12:12", "count":11}`,
// readStatus: 0,
// createdAt: '2022-10-18 14:05:12',
// updatedAt: '2022-10-18 17:15:19',
// },
// );
// }, 5000);
if
(
dispatch
)
{
dispatch
({
type
:
'
settings/getSetting
'
,
...
...
@@ -102,11 +84,6 @@ const BasicLayout = props => {
dispatch
({
type
:
'
messageReminder/getUnReadMsgList
'
,
});
setTimeout
(()
=>
{
dispatch
({
type
:
'
messageReminder/getUnReadCount
'
,
});
},
2000
);
}
},
[]);
/**
...
...
src/utils/websocket.js
View file @
ac02eb28
...
...
@@ -13,6 +13,7 @@ class Socket extends EventEmitter {
this
.
connected
=
false
;
this
.
waitingSendData
=
[];
this
.
reconnectCount
=
0
;
this
.
heartBeatTimer
=
null
;
return
this
;
}
...
...
@@ -53,8 +54,10 @@ class Socket extends EventEmitter {
// 连接成功触发
onopen
=
()
=>
{
const
{
heartBeatTime
}
=
this
.
options
;
console
.
log
(
'
ws:连接成功
'
);
this
.
connected
=
true
;
this
.
heartBeat
(
heartBeatTime
);
this
.
checkWaitingData
();
this
.
event
.
emit
(
'
open
'
);
};
...
...
@@ -68,56 +71,66 @@ class Socket extends EventEmitter {
// 关闭连接触发
onclose
=
e
=>
{
this
.
connected
=
false
;
// 关闭将标识符改为true
console
.
log
(
'
关闭socket收到的数据
'
);
this
.
connected
=
false
;
// 关闭将标识符改为true
if
(
this
.
heartBeatTimer
)
{
clearTimeout
(
this
.
heartBeatTimer
);
}
this
.
event
.
emit
(
'
close
'
,
e
);
// 根据后端返回的状态码做操作
// 我的项目是当前页面打开两个或者以上,就把当前以打开的socket关闭
// 否则就20秒重连一次,直到重连成功为止
// 最多重连10次
if
(
this
.
reconnectCount
>
10
)
{
this
.
reconnectCount
=
0
;
return
;
}
const
reconnect
=
()
=>
{
if
(
this
.
taskRemindInterval
)
{
clearTimeout
(
this
.
taskRemindInterval
);
}
this
.
taskRemindInterval
=
setTimeout
(()
=>
{
if
(
!
this
.
connected
)
{
this
.
reconnectCount
++
;
this
.
connection
();
reconnect
();
}
},
20
000
);
},
5
000
);
};
reconnect
();
};
onerror
=
e
=>
{
console
.
log
(
'
ws: error
'
,
e
);
this
.
socket
=
null
;
this
.
event
.
emit
(
'
error
'
,
e
);
};
sendMessage
=
value
=>
{
console
.
log
(
'
ws: send
'
,
value
);
// 向后端发送数据
if
(
this
.
socket
)
{
if
(
!
this
.
connected
)
{
this
.
waitingSendData
.
unshift
(
value
);
return
;
}
this
.
socket
.
send
(
JSON
.
stringify
(
value
));
const
sendValue
=
typeof
value
===
'
string
'
?
value
:
JSON
.
stringify
(
value
);
this
.
socket
.
send
(
sendValue
);
}
};
checkWaitingData
()
{
console
.
log
(
'
ws: checkWaitingData
'
,
this
.
waiting
);
if
(
this
.
waitingSendData
.
length
)
{
this
.
sendMessage
(
this
.
waitingSendData
.
splice
(
0
,
1
));
this
.
checkWaitingData
();
}
}
hearBeat
()
{
setTimeout
(()
=>
{
// 保持连接-默认每3分钟重置一下服务器关闭时间
heartBeat
(
time
)
{
this
.
heartBeatTimer
=
setTimeout
(()
=>
{
console
.
log
(
'
ws: heart beat
'
,
time
);
this
.
sendMessage
(
'
HeartBeat
'
);
},
300000
);
this
.
heartBeat
(
time
);
},
time
||
180000
);
}
}
...
...
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