Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
max_android_panel
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
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Zhang Xin
max_android_panel
Commits
56945370
Commit
56945370
authored
Dec 05, 2018
by
Zhang Xin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
重构socket
parent
bbeac87c
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
83 additions
and
93 deletions
+83
-93
client/components/CommonComponent/PopupComponent/index.css
+2
-1
client/config/socketType.js
+3
-1
client/containers/PageContainer/PageContainer.jsx
+7
-2
client/index.js
+2
-1
client/util/event.js
+4
-0
client/util/socket.js
+65
-88
No files found.
client/components/CommonComponent/PopupComponent/index.css
View file @
56945370
...
...
@@ -90,7 +90,7 @@
}
.defaultPopup
{
height
:
3.05rem
;
/*height: 3.05rem;*/
width
:
6.666666666666667rem
;
position
:
absolute
;
top
:
50%
;
...
...
@@ -100,6 +100,7 @@
background
:
#ffffff
;
overflow
:
hidden
;
text-align
:
center
;
padding-bottom
:
0.3333rem
;
}
.defaultPopup
.defaultPopupText
{
...
...
client/config/socketType.js
View file @
56945370
const
socketType
=
{
GETSTOREINFO
:
'GETSTOREINFO'
,
GETSOCKETMSG
:
'GETSOCKETMSG'
GETSOCKETMSG
:
'GETSOCKETMSG'
,
SOCKETERROR
:
'SOCKETERROR'
}
module
.
exports
=
socketType
;
\ No newline at end of file
client/containers/PageContainer/PageContainer.jsx
View file @
56945370
...
...
@@ -158,7 +158,10 @@ class PageContainer extends React.Component {
console
.
log
(
"==GETSTOREINFO==="
)
console
.
log
(
data
)
that
.
getSocketMsg
(
data
)
})
});
// on(CONFIG.socketType.SOCKETERROR,this,()=>{
// dispatch(saveCommonInfo({ip:""}));
// })
}
...
...
@@ -175,10 +178,12 @@ class PageContainer extends React.Component {
}
getSocketMsg
(
data
){
this
.
showError
(
JSON
.
stringify
(
data
));
let
{
dispatch
,
state
}
=
this
.
props
;
let
{
isSend
}
=
this
.
state
;
switch
(
data
.
type
){
case
'SUCCESS_OPENDOOR'
:
this
.
showError
(
JSON
.
stringify
(
data
));
//执行开门
let
info
=
data
.
info
;
let
commonInfo
=
state
.
commonInfo
;
...
...
@@ -558,7 +563,7 @@ class PageContainer extends React.Component {
if
(
userInfo
.
employeeId
&&
storeInfo
.
id
){
this
.
changePages
(
CONFIG
.
showPage
[
page
]);
}
else
{
this
.
showError
(
"没有用户信息或门店信息
"
);
this
.
showError
(
"没有用户信息或门店信息
ip:"
+
commonInfo
.
ip
);
}
}
else
{
...
...
client/index.js
View file @
56945370
...
...
@@ -29,7 +29,7 @@ function renderPage(store) {
let
store
=
activateVendor
();
// UTILPATH.socket.init(129,17);
// UTILPATH.socket.testIp(90);
UTILPATH
.
socket
.
getIp
();
// UTILPATH.socket.getIp();
renderPage
(
store
);
\ No newline at end of file
client/util/event.js
View file @
56945370
...
...
@@ -2,7 +2,9 @@ let events = {};
const
on
=
(
name
,
self
,
callback
)
=>
{
let
tuple
=
[
self
,
callback
];
console
.
log
(
"on "
,
name
,
tuple
)
let
cb
=
events
[
name
];
console
.
log
(
"on__"
,
events
,
cb
)
if
(
Array
.
isArray
(
cb
)){
cb
.
push
(
tuple
);
}
else
{
...
...
@@ -12,8 +14,10 @@ const on = (name,self,callback)=>{
const
emit
=
(
name
,
data
)
=>
{
let
callbacks
=
events
[
name
];
console
.
log
(
"emit "
,
name
,
callbacks
)
if
(
Array
.
isArray
(
callbacks
)){
callbacks
.
map
((
tuple
)
=>
{
console
.
log
(
"tuple "
,
tuple
);
let
self
=
tuple
[
0
];
let
callback
=
tuple
[
1
];
callback
.
call
(
self
,
data
);
...
...
client/util/socket.js
View file @
56945370
...
...
@@ -4,20 +4,14 @@ import {emit} from './event'
let
domainIp
=
'192.168.10.'
;
let
initIndex
=
1
;
let
totalIndex
=
255
/
5
;
let
failTotal
=
1
;
let
currentIpIndex
=
0
;
const
socket
=
{};
let
totalCount
=
0
;
let
getIpING
=
false
;
//正在获取IP
let
currentIpIndex
=
255
;
let
ws
=
null
;
let
inteval
=
null
;
let
timer
=
null
;
let
time
=
60000
;
let
intevalTime
=
30000
;
let
isFalse
=
false
;
const
checkStatus
=
(
response
)
=>
{
console
.
log
(
"==========="
)
console
.
log
(
response
);
if
(
response
.
status
>=
200
&&
response
.
status
<
300
)
{
return
response
;
}
...
...
@@ -25,17 +19,18 @@ const checkStatus = (response)=>{
error
.
response
=
response
;
throw
error
;
};
const
parseJson
=
(
response
)
=>
{
console
.
log
(
"socket response "
,
response
);
return
response
.
json
();
};
const
getIp
=
()
=>
{
console
.
log
(
"getIp : "
,
initIndex
)
for
(
let
i
=
initIndex
;
i
<
(
totalIndex
+
initIndex
)
;
i
++
){
socket
.
getIp
=
()
=>
{
console
.
log
(
"getIp "
,
getIpING
,
currentIpIndex
);
if
(
!
getIpING
){
getIpING
=
true
;
for
(
let
i
=
0
;
i
<
255
;
i
++
){
let
url
=
'http://'
+
domainIp
+
i
+
':9999/getmyid'
;
console
.
log
(
url
)
if
(
currentIpIndex
===
255
&&
getIpING
){
fetch
(
url
,{
// credentials : 'include',
method
:
'GET'
,
...
...
@@ -43,43 +38,31 @@ const getIp = ()=>{
}).
then
(
checkStatus
)
.
then
(
parseJson
)
.
then
((
data
)
=>
{
console
.
log
(
"data :"
,
data
);
if
(
data
.
storeId
){
let
wsUrl
=
'ws://'
+
domainIp
+
i
+
':7788/notify?from=plant&storeId='
+
data
.
storeId
;
currentIpIndex
=
i
;
console
.
error
(
"data :"
,
data
,
currentIpIndex
,
i
);
if
(
data
.
storeId
&&
currentIpIndex
===
255
){
init
(
i
,
data
.
storeId
);
}
})
.
catch
((
err
)
=>
{
console
.
error
(
"Err:"
,
err
);
failTotal
++
;
if
(
failTotal
>=
totalIndex
+
initIndex
){
initIndex
=
totalIndex
+
initIndex
;
if
(
initIndex
>
255
){
inteval
=
setTimeout
(
function
()
{
console
.
log
(
"------"
);
window
.
clearTimeout
(
inteval
);
inteval
=
null
;
initIndex
=
1
;
failTotal
=
1
;
getIp
();
},
intevalTime
)
}
else
{
getIp
();
totalCount
++
;
if
(
totalCount
>=
255
){
totalCount
=
0
;
currentIpIndex
=
255
;
getIpING
=
false
;
socket
.
getIp
();
}
});
}
}
});
}
};
const
testIp
=
(
ip
)
=>
{
console
.
log
(
"testIp : "
,
ip
);
};
socket
.
getStoreIdByIp
=
(
ip
)
=>
{
let
url
=
'http://'
+
domainIp
+
ip
+
':9999/getmyid'
;
console
.
log
(
"testIp url : "
,
url
);
fetch
(
url
,{
// credentials : 'include',
method
:
'GET'
,
...
...
@@ -89,46 +72,60 @@ const testIp = (ip) => {
.
then
((
data
)
=>
{
console
.
log
(
"socket data "
,
data
);
if
(
data
.
storeId
){
window
.
storeId
=
data
.
storeId
;
window
.
localIp
=
domainIp
+
ip
;
let
wsUrl
=
'ws://'
+
domainIp
+
ip
+
':7788/notify?from=plant&storeId='
+
data
.
storeId
;
currentIpIndex
=
ip
;
init
(
ip
,
data
.
storeId
);
init
(
ip
,
data
.
storeId
,
true
);
}
})
.
catch
((
err
)
=>
{
console
.
error
(
"Err:"
,
err
);
currentIpIndex
=
0
;
window
.
clearTimeout
(
inteval
);
inteval
=
null
;
initIndex
=
1
;
failTotal
=
1
;
getIp
()
if
(
!
getIpING
){
console
.
log
(
"getStoreIdByIp======="
)
currentIpIndex
=
255
;
totalCount
=
0
;
socket
.
getIp
()
;
}
});
}
const
init
=
(
ip
,
storeid
)
=>
{
const
getError
=
()
=>
{
console
.
error
(
"socket error"
);
getClose
();
}
const
getClose
=
()
=>
{
console
.
error
(
"socket close "
,
currentIpIndex
);
//如果有currentIp就没有必要再次循环
ws
=
null
;
if
(
currentIpIndex
!==
255
){
//只需再次获取storeId即可,以防storeId有变化
socket
.
getStoreIdByIp
(
currentIpIndex
);
}
else
{
//重新获取IP
if
(
!
getIpING
){
console
.
log
(
"socket close========"
)
currentIpIndex
=
255
;
totalCount
=
0
;
socket
.
getIp
();
}
}
}
const
init
=
(
ip
,
storeid
,
from
=
false
)
=>
{
let
wsUrl
=
'ws://'
+
domainIp
+
ip
+
':7788/notify?from=plant&storeId='
+
storeid
;
console
.
log
(
"init :"
,
wsUrl
);
if
(
"WebSocket"
in
window
&&
!
ws
&&
ip
&&
storeid
){
if
(
"WebSocket"
in
window
&&
!
ws
&&
ip
&&
storeid
&&
(
currentIpIndex
===
255
||
from
)
){
ws
=
new
WebSocket
(
wsUrl
);
ws
.
onopen
=
function
()
{
// Web Socket 已连接上,使用 send() 方法发送数据
// ws.send("发送数据");
clearInterval
(
timer
);
timer
=
null
;
window
.
clearTimeout
(
inteval
);
inteval
=
null
;
initIndex
=
1
;
failTotal
=
1
;
currentIpIndex
=
ip
;
getIpING
=
false
;
console
.
log
(
"open"
);
isFalse
=
false
;
emit
(
CONFIG
.
socketType
.
GETSTOREINFO
,{
ip
:
domainIp
+
ip
,
storeId
:
storeid
})
};
...
...
@@ -143,10 +140,12 @@ const init = (ip,storeid)=>{
{
// 关闭 websocket
console
.
log
(
"close : "
,
err
);
getError
();
emit
(
CONFIG
.
socketType
.
SOCKETERROR
,
""
);
getClose
();
};
ws
.
onerror
=
function
(
err
)
{
console
.
error
(
"error : "
,
err
);
emit
(
CONFIG
.
socketType
.
SOCKETERROR
,
""
);
getError
();
}
...
...
@@ -154,29 +153,8 @@ const init = (ip,storeid)=>{
}
};
const
getError
=
()
=>
{
ws
=
null
;
isFalse
=
true
;
console
.
log
(
"getError"
)
if
(
currentIpIndex
){
testIp
(
currentIpIndex
);
}
else
{
window
.
clearTimeout
(
inteval
);
inteval
=
null
;
initIndex
=
1
;
failTotal
=
1
;
getIp
();
}
// if(!ws && !timer){
// console.log("in timer")
// timer = setInterval(function () {
// getError();
// },time)
// }
}
const
sendMsg
=
(
msg
)
=>
{
socket
.
sendMsg
=
(
msg
)
=>
{
if
(
ws
){
try
{
ws
.
send
(
msg
);
...
...
@@ -197,5 +175,4 @@ const closeSocket = ()=>{
}
}
}
module
.
exports
=
{
getIp
,
sendMsg
,
init
,
testIp
};
\ No newline at end of file
module
.
exports
=
socket
;
\ No newline at end of file
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