Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
W
wx-school-app-public
Project
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
姜雷
wx-school-app-public
Commits
7d9d885d
Commit
7d9d885d
authored
Jul 08, 2019
by
姜雷
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加蓝牙和websocket的hook方法
parent
e77fcdb1
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
158 additions
and
0 deletions
+158
-0
useBluetooth.ts
src/hooks/useBluetooth.ts
+0
-0
useDeviceWS.ts
src/hooks/useDeviceWS.ts
+158
-0
No files found.
src/hooks/useBluetooth.ts
0 → 100644
View file @
7d9d885d
This diff is collapsed.
Click to expand it.
src/hooks/useDeviceWS.ts
0 → 100644
View file @
7d9d885d
import
Actions
from
'@/types/Store/Actions'
;
import
Taro
,
{
useReducer
,
useEffect
}
from
'@tarojs/taro'
;
import
{
str2ab
,
ab2str
}
from
'@/utils/arrayBuffer'
;
type
StoreState
=
{
socketState
:
boolean
;
socketTask
:
Taro
.
SocketTask
|
null
;
};
const
StopCode
=
1000
;
let
reConnectting
:
boolean
=
false
;
let
timer
:
NodeJS
.
Timeout
|
null
=
null
;
const
initState
=
{
socketState
:
false
,
socketTask
:
null
,
};
const
reducer
=
(
state
:
StoreState
,
action
:
Actions
):
StoreState
=>
{
switch
(
action
.
type
)
{
case
'GET_SOCKET_TASK'
:
return
{
...
state
,
socketTask
:
action
.
payload
};
case
'SOCKET_STATE_CHANGE'
:
return
{
...
state
,
socketState
:
action
.
payload
};
default
:
return
state
;
}
};
const
useDeviceWS
=
({
url
,
getSocketData
,
}:
{
url
:
string
;
getSocketData
:
(
msg
:
string
)
=>
void
;
}):
{
state
:
StoreState
;
sendMessageToServer
:
(
msg
:
string
,
successHandle
:
Taro
.
SocketTask
.
send
.
ParamPropSuccess
,
failHandle
:
Taro
.
SocketTask
.
send
.
ParamPropFail
,
)
=>
void
;
}
=>
{
const
[
state
,
dispatch
]
=
useReducer
(
reducer
,
initState
);
const
connectDeviceSocket
=
(
url
:
string
)
=>
{
Taro
.
connectSocket
({
url
:
url
}).
then
(
task
=>
{
dispatch
({
type
:
'GET_SOCKET_TASK'
,
payload
:
task
});
task
.
onOpen
(()
=>
{
console
.
log
(
'onOpen'
);
if
(
reConnectting
)
{
// reConnectDeviceSocket(true);
timer
&&
clearTimeout
(
timer
);
timer
=
null
;
reConnectting
=
false
;
}
task
.
send
({
data
:
str2ab
(
'{}'
)
});
dispatch
({
type
:
'SOCKET_STATE_CHANGE'
,
payload
:
true
});
// this.sendDeviceCode();
});
task
.
onMessage
(
res
=>
{
const
msg
:
string
=
ab2str
(
res
.
data
);
console
.
log
(
'socket onMessage: '
,
msg
);
if
(
msg
===
'[0]'
)
{
console
.
log
(
'结束蓝牙以及socket: '
,
msg
);
closeDeviceSocket
();
}
else
if
(
msg
===
'[]'
)
{
}
else
{
if
(
msg
.
length
>
100
)
{
getSocketData
(
'[]'
);
}
else
if
(
msg
.
length
>
20
)
{
for
(
let
index
=
0
;
index
<=
Math
.
floor
(
msg
.
length
/
20
);
index
++
)
{
let
str
=
msg
.
substring
(
index
*
20
,
(
index
+
1
)
*
20
);
getSocketData
(
str
);
}
}
else
{
if
(
msg
)
{
getSocketData
(
msg
);
}
}
}
});
task
.
onClose
(
e
=>
{
console
.
log
(
'socked关闭'
,
e
,
reConnectting
,
timer
);
dispatch
({
type
:
'SOCKET_STATE_CHANGE'
,
payload
:
false
});
dispatch
({
type
:
'GET_SOCKET_TASK'
,
payload
:
null
});
if
(
e
.
code
===
StopCode
)
{
console
.
log
(
'正确结束socket连接'
);
}
else
{
console
.
log
(
'开始重连socket'
);
reConnectDeviceSocket
();
}
});
});
};
const
closeDeviceSocket
=
()
=>
{
const
{
socketTask
}
=
state
;
console
.
log
(
'in close'
,
socketTask
,
timer
);
if
(
socketTask
)
{
socketTask
.
close
({
code
:
StopCode
,
complete
:
()
=>
{
dispatch
({
type
:
'SOCKET_STATE_CHANGE'
,
payload
:
false
});
dispatch
({
type
:
'GET_SOCKET_TASK'
,
payload
:
null
});
},
});
}
if
(
timer
)
{
clearTimeout
(
timer
);
}
};
const
reConnectDeviceSocket
=
()
=>
{
console
.
log
(
reConnectting
,
timer
);
if
(
reConnectting
)
{
connectDeviceSocket
(
url
);
}
else
if
(
timer
)
{
clearTimeout
(
timer
);
reConnectting
=
false
;
timer
=
null
;
console
.
log
(
'请保证网络正常'
);
Taro
.
showModal
({
title
:
'警告'
,
content
:
'请保持网络畅通正常'
,
});
}
else
{
timer
=
setTimeout
(()
=>
{
reConnectting
=
false
;
},
10000
);
reConnectting
=
true
;
console
.
log
(
reConnectting
,
timer
);
connectDeviceSocket
(
url
);
}
};
const
sendMessageToServer
=
(
msg
:
string
,
successHandle
:
Taro
.
SocketTask
.
send
.
ParamPropSuccess
,
failHandle
:
Taro
.
SocketTask
.
send
.
ParamPropFail
,
)
=>
{
const
{
socketTask
}
=
state
;
socketTask
&&
socketTask
.
send
({
data
:
str2ab
(
msg
),
success
:
successHandle
,
fail
:
failHandle
,
});
};
useEffect
(()
=>
{
connectDeviceSocket
(
url
);
return
closeDeviceSocket
;
},
[
url
]);
return
{
state
,
sendMessageToServer
};
};
export
default
useDeviceWS
;
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