Commit 56945370 by Zhang Xin

重构socket

parent bbeac87c
...@@ -90,7 +90,7 @@ ...@@ -90,7 +90,7 @@
} }
.defaultPopup { .defaultPopup {
height: 3.05rem; /*height: 3.05rem;*/
width: 6.666666666666667rem; width: 6.666666666666667rem;
position: absolute; position: absolute;
top: 50%; top: 50%;
...@@ -100,6 +100,7 @@ ...@@ -100,6 +100,7 @@
background: #ffffff; background: #ffffff;
overflow: hidden; overflow: hidden;
text-align: center; text-align: center;
padding-bottom: 0.3333rem;
} }
.defaultPopup .defaultPopupText { .defaultPopup .defaultPopupText {
......
const socketType = { const socketType = {
GETSTOREINFO:'GETSTOREINFO', GETSTOREINFO:'GETSTOREINFO',
GETSOCKETMSG:'GETSOCKETMSG' GETSOCKETMSG:'GETSOCKETMSG',
SOCKETERROR : 'SOCKETERROR'
} }
module.exports = socketType; module.exports = socketType;
\ No newline at end of file
...@@ -158,7 +158,10 @@ class PageContainer extends React.Component { ...@@ -158,7 +158,10 @@ class PageContainer extends React.Component {
console.log("==GETSTOREINFO===") console.log("==GETSTOREINFO===")
console.log(data) console.log(data)
that.getSocketMsg(data) that.getSocketMsg(data)
}) });
// on(CONFIG.socketType.SOCKETERROR,this,()=>{
// dispatch(saveCommonInfo({ip:""}));
// })
} }
...@@ -175,10 +178,12 @@ class PageContainer extends React.Component { ...@@ -175,10 +178,12 @@ class PageContainer extends React.Component {
} }
getSocketMsg(data){ getSocketMsg(data){
this.showError(JSON.stringify(data));
let {dispatch,state} = this.props; let {dispatch,state} = this.props;
let {isSend} = this.state; let {isSend} = this.state;
switch (data.type){ switch (data.type){
case 'SUCCESS_OPENDOOR': case 'SUCCESS_OPENDOOR':
this.showError(JSON.stringify(data));
//执行开门 //执行开门
let info = data.info; let info = data.info;
let commonInfo = state.commonInfo; let commonInfo = state.commonInfo;
...@@ -558,7 +563,7 @@ class PageContainer extends React.Component { ...@@ -558,7 +563,7 @@ class PageContainer extends React.Component {
if(userInfo.employeeId && storeInfo.id){ if(userInfo.employeeId && storeInfo.id){
this.changePages(CONFIG.showPage[page]); this.changePages(CONFIG.showPage[page]);
} else{ } else{
this.showError("没有用户信息或门店信息"); this.showError("没有用户信息或门店信息 ip:"+commonInfo.ip);
} }
}else{ }else{
......
...@@ -29,7 +29,7 @@ function renderPage(store) { ...@@ -29,7 +29,7 @@ function renderPage(store) {
let store = activateVendor(); let store = activateVendor();
// UTILPATH.socket.init(129,17); // UTILPATH.socket.init(129,17);
// UTILPATH.socket.testIp(90);
UTILPATH.socket.getIp(); UTILPATH.socket.getIp();
// UTILPATH.socket.getIp();
renderPage(store); renderPage(store);
\ No newline at end of file
...@@ -2,7 +2,9 @@ let events = {}; ...@@ -2,7 +2,9 @@ let events = {};
const on = (name,self,callback)=>{ const on = (name,self,callback)=>{
let tuple = [self,callback]; let tuple = [self,callback];
console.log("on ",name,tuple)
let cb = events[name]; let cb = events[name];
console.log("on__",events,cb)
if(Array.isArray(cb)){ if(Array.isArray(cb)){
cb.push(tuple); cb.push(tuple);
}else { }else {
...@@ -12,8 +14,10 @@ const on = (name,self,callback)=>{ ...@@ -12,8 +14,10 @@ const on = (name,self,callback)=>{
const emit = (name,data)=>{ const emit = (name,data)=>{
let callbacks = events[name]; let callbacks = events[name];
console.log("emit ",name,callbacks)
if(Array.isArray(callbacks)){ if(Array.isArray(callbacks)){
callbacks.map((tuple)=>{ callbacks.map((tuple)=>{
console.log("tuple ",tuple);
let self = tuple[0]; let self = tuple[0];
let callback = tuple[1]; let callback = tuple[1];
callback.call(self,data); callback.call(self,data);
......
...@@ -4,20 +4,14 @@ import {emit} from './event' ...@@ -4,20 +4,14 @@ import {emit} from './event'
let domainIp = '192.168.10.'; let domainIp = '192.168.10.';
let initIndex = 1; const socket = {};
let totalIndex = 255 / 5;
let failTotal = 1; let totalCount = 0;
let currentIpIndex = 0; let getIpING = false; //正在获取IP
let currentIpIndex = 255;
let ws = null; let ws = null;
let inteval = null;
let timer = null;
let time = 60000;
let intevalTime = 30000;
let isFalse = false;
const checkStatus = (response)=>{ const checkStatus = (response)=>{
console.log("===========")
console.log(response);
if (response.status >= 200 && response.status < 300) { if (response.status >= 200 && response.status < 300) {
return response; return response;
} }
...@@ -25,61 +19,50 @@ const checkStatus = (response)=>{ ...@@ -25,61 +19,50 @@ const checkStatus = (response)=>{
error.response = response; error.response = response;
throw error; throw error;
}; };
const parseJson = (response)=>{ const parseJson = (response)=>{
console.log("socket response ",response); console.log("socket response ",response);
return response.json(); return response.json();
}; };
const getIp = ()=>{ socket.getIp = ()=>{
console.log("getIp : ",initIndex) console.log("getIp ",getIpING,currentIpIndex);
for(let i = initIndex; i < (totalIndex + initIndex) ; i++){ if(!getIpING){
let url = 'http://' + domainIp + i + ':9999/getmyid'; getIpING = true;
console.log(url) for(let i = 0; i < 255 ; i ++){
fetch( url ,{ let url = 'http://' + domainIp + i + ':9999/getmyid';
// credentials : 'include', if(currentIpIndex === 255 && getIpING){
method : 'GET', fetch( url ,{
mode : 'cors', // credentials : 'include',
}).then(checkStatus) method : 'GET',
.then(parseJson) mode : 'cors',
.then((data) => { }).then(checkStatus)
console.log("data :",data); .then(parseJson)
if(data.storeId){ .then((data) => {
let wsUrl = 'ws://' + domainIp + i + ':7788/notify?from=plant&storeId='+data.storeId; console.error("data :",data,currentIpIndex,i);
currentIpIndex = i; if(data.storeId && currentIpIndex === 255){
init(i,data.storeId); init(i,data.storeId);
} }
})
}) .catch((err) =>{
.catch((err) =>{ console.error("Err:",err);
console.error("Err:",err); totalCount++;
failTotal++; if(totalCount >= 255){
if(failTotal >= totalIndex + initIndex){ totalCount = 0;
initIndex = totalIndex + initIndex; currentIpIndex = 255;
if(initIndex > 255){ getIpING = false;
inteval = setTimeout(function () { socket.getIp();
console.log("------"); }
window.clearTimeout(inteval);
inteval = null; });
initIndex = 1; }
failTotal = 1;
getIp(); }
},intevalTime)
}else{
getIp();
}
}
});
} }
};
const testIp = (ip) => { };
console.log("testIp : ",ip);
socket.getStoreIdByIp = (ip) => {
let url = 'http://' + domainIp + ip + ':9999/getmyid'; let url = 'http://' + domainIp + ip + ':9999/getmyid';
console.log("testIp url : ",url);
fetch( url ,{ fetch( url ,{
// credentials : 'include', // credentials : 'include',
method : 'GET', method : 'GET',
...@@ -89,46 +72,60 @@ const testIp = (ip) => { ...@@ -89,46 +72,60 @@ const testIp = (ip) => {
.then((data) => { .then((data) => {
console.log("socket data ",data); console.log("socket data ",data);
if(data.storeId){ if(data.storeId){
window.storeId = data.storeId; init(ip,data.storeId,true);
window.localIp = domainIp + ip;
let wsUrl = 'ws://' + domainIp + ip + ':7788/notify?from=plant&storeId='+data.storeId;
currentIpIndex = ip;
init(ip,data.storeId);
} }
}) })
.catch((err) =>{ .catch((err) =>{
console.error("Err:",err); console.error("Err:",err);
currentIpIndex = 0; if(!getIpING){
window.clearTimeout(inteval); console.log("getStoreIdByIp=======")
inteval = null; currentIpIndex = 255;
initIndex = 1; totalCount = 0;
failTotal = 1; socket.getIp();
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; let wsUrl = 'ws://' + domainIp + ip + ':7788/notify?from=plant&storeId=' + storeid;
console.log("init :" ,wsUrl); 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 = new WebSocket(wsUrl);
ws.onopen = function() ws.onopen = function()
{ {
// Web Socket 已连接上,使用 send() 方法发送数据 // Web Socket 已连接上,使用 send() 方法发送数据
// ws.send("发送数据"); // ws.send("发送数据");
clearInterval(timer); currentIpIndex = ip;
timer = null; getIpING = false;
window.clearTimeout(inteval);
inteval = null;
initIndex = 1;
failTotal = 1;
console.log("open"); console.log("open");
isFalse = false;
emit(CONFIG.socketType.GETSTOREINFO,{ip:domainIp+ip,storeId:storeid}) emit(CONFIG.socketType.GETSTOREINFO,{ip:domainIp+ip,storeId:storeid})
}; };
...@@ -143,10 +140,12 @@ const init = (ip,storeid)=>{ ...@@ -143,10 +140,12 @@ const init = (ip,storeid)=>{
{ {
// 关闭 websocket // 关闭 websocket
console.log("close : ",err); console.log("close : ",err);
getError(); emit(CONFIG.socketType.SOCKETERROR,"");
getClose();
}; };
ws.onerror = function (err) { ws.onerror = function (err) {
console.error("error : ",err); console.error("error : ",err);
emit(CONFIG.socketType.SOCKETERROR,"");
getError(); getError();
} }
...@@ -154,29 +153,8 @@ const init = (ip,storeid)=>{ ...@@ -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)
// }
} socket.sendMsg = (msg) => {
const sendMsg = (msg) => {
if(ws){ if(ws){
try{ try{
ws.send(msg); ws.send(msg);
...@@ -197,5 +175,4 @@ const closeSocket = ()=>{ ...@@ -197,5 +175,4 @@ const closeSocket = ()=>{
} }
} }
} }
module.exports = socket;
module.exports = {getIp,sendMsg,init,testIp}; \ No newline at end of file
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment