Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
motorCity
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
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
node_server
motorCity
Commits
bc414cfb
Commit
bc414cfb
authored
Aug 17, 2022
by
lixinming
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
no message
parent
13f7dbe5
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
29 deletions
+20
-29
wsData.ts
src/biz/jgfwnl/wsData.ts
+11
-12
wsData.ts
src/data/interface/wsData.ts
+9
-17
No files found.
src/biz/jgfwnl/wsData.ts
View file @
bc414cfb
import
{
initWSData
}
from
"../../data/interface/wsData"
;
import
{
Socket
}
from
"../../net/socket_server"
;
//连接池
let
connectionPool
=
{};
//广播消息
export
async
function
sendMsg
(
data
)
{
for
(
let
key
in
connectionPool
)
{
connectionPool
[
key
].
send
({
code
:
200
,
success
:
true
,
data
});
let
result
=
{
code
:
200
,
success
:
true
,
data
}
connectionPool
[
key
].
send
(
JSON
.
stringify
(
result
));
}
}
...
...
@@ -29,18 +32,15 @@ export async function initSocketServer(port) {
if
(
data
==
'i am heart'
)
ws
.
send
(
'{success:true, code:199}'
);
});
ws
.
on
(
'close'
,
function
(
data
,
req
){
console
.
log
(
"close"
,
data
,
req
);
ws
.
on
(
'close'
,
function
(
data
){
if
(
ws
.
_socket
&&
ws
.
_socket
.
_peername
&&
ws
.
_socket
.
_peername
.
address
)
{
let
userId
=
ws
.
_socket
.
_peername
.
address
;
console
.
log
(
"close"
,
userId
,
data
);
delete
connectionPool
[
userId
];
}
});
});
});
//todo 测试连接代码
test
();
}
function
test
()
{
initWSData
();
}
\ No newline at end of file
src/data/interface/wsData.ts
View file @
bc414cfb
...
...
@@ -24,7 +24,7 @@ async function sleep(ms) {
let
heartCount
=
0
;
//心跳
let
heart
=
{
timeout
:
4
*
1000
,
//40秒一次心跳包
timeout
:
4
0
*
1000
,
//40秒一次心跳包
serverTimeout
:
4
*
1000
,
//心跳包超时时间
timeoutObj
:
null
,
notRespontNum
:
3
,
//未响应次数阈值
...
...
@@ -63,28 +63,25 @@ let heart = {
}
//ws客户端
let
path
=
"ws://192.168.0.105:30002"
;
let
ws
=
new
WebSocket
(
path
,
{
perMessageDeflate
:
false
});
async
function
createSocket
()
{
// let path = "ws://10.51.50.136:8090/ws/v1";
let
path
=
"ws://10.51.50.136:8090/ws/v1"
;
let
ws
=
new
WebSocket
(
path
,
{
perMessageDeflate
:
false
});
ws
.
onclose
=
async
(
err
)
=>
{
//
console.log("连接关闭: ", err);
//
lockReconnect = false;
//
heartCount = 0;
//
//
断线重连
//
reconnect();
console
.
log
(
"连接关闭: "
,
err
);
lockReconnect
=
false
;
heartCount
=
0
;
//断线重连
reconnect
();
console
.
log
(
"客户端 关闭"
);
}
ws
.
onmessage
=
(
msg
)
=>
{
//接收数据
console
.
log
(
"客户端接收数据 =>"
,
msg
);
heart
.
start
(
ws
);
//
stats(msg);
stats
(
msg
);
}
ws
.
onerror
=
(
error
)
=>
{
...
...
@@ -98,11 +95,6 @@ async function createSocket() {
}
setTimeout
(()
=>
{
ws
.
close
();
},
10
*
1000
);
export
async
function
initWSData
()
{
await
createSocket
();
}
...
...
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