diff --git a/client/containers/PageContainer/PageContainer.jsx b/client/containers/PageContainer/PageContainer.jsx index e8581e9..eaad2d4 100755 --- a/client/containers/PageContainer/PageContainer.jsx +++ b/client/containers/PageContainer/PageContainer.jsx @@ -595,7 +595,7 @@ class PageContainer extends React.Component { if(userInfo.employeeId && storeInfo.id){ this.changePages(CONFIG.showPage[page]); } else{ - this.showError("没有用户信息或门店信息 ip:"+commonInfo.ip); + this.showError("没有用户信息或门店信息 ip:"+commonInfo.ip+" storeId:"+(commonInfo.storeInfo ? commonInfo.storeInfo.id : 0)); } }else{ diff --git a/client/index.js b/client/index.js index 1218c49..4b1a6e0 100755 --- a/client/index.js +++ b/client/index.js @@ -29,7 +29,7 @@ function renderPage(store) { let store = activateVendor(); // UTILPATH.socket.init(129,17); -UTILPATH.socket.getIp(); +UTILPATH.socket.getIp1(); // UTILPATH.socket.getIp(); renderPage(store); \ No newline at end of file diff --git a/client/util/socket.js b/client/util/socket.js index f37112e..b7c2d19 100644 --- a/client/util/socket.js +++ b/client/util/socket.js @@ -10,6 +10,7 @@ let totalCount = 0; let getIpING = false; //正在获取IP let currentIpIndex = 255; let ws = null; +let storeId = 0 const checkStatus = (response)=>{ if (response.status >= 200 && response.status < 300) { @@ -24,89 +25,129 @@ const parseJson = (response)=>{ return response.json(); }; -socket.getIp = ()=>{ - console.log("getIp ",getIpING,currentIpIndex); - if(!getIpING){ +// 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'; +// if(currentIpIndex === 255 && getIpING){ +// fetch( url ,{ +// // credentials : 'include', +// method : 'GET', +// mode : 'cors', +// }).then(checkStatus) +// .then(parseJson) +// .then((data) => { +// console.error("data :",data,currentIpIndex,i); +// if(data.storeId && currentIpIndex === 255){ +// init(i,data.storeId); +// } +// }) +// .catch((err) =>{ +// console.error("Err:",err); +// totalCount++; +// if(totalCount >= 255){ +// totalCount = 0; +// currentIpIndex = 255; +// getIpING = false; +// socket.getIp(); +// } +// +// }); +// } +// +// } +// } +// +// }; + +socket.getIp1 = ()=>{ + if(!getIpING) { getIpING = true; - for(let i = 0; i < 255 ; i ++){ - let url = 'http://' + domainIp + i + ':9999/getmyid'; - if(currentIpIndex === 255 && getIpING){ - fetch( url ,{ - // credentials : 'include', - method : 'GET', - mode : 'cors', - }).then(checkStatus) - .then(parseJson) - .then((data) => { - console.error("data :",data,currentIpIndex,i); - if(data.storeId && currentIpIndex === 255){ - init(i,data.storeId); - } - }) - .catch((err) =>{ - console.error("Err:",err); - totalCount++; - if(totalCount >= 255){ - totalCount = 0; - currentIpIndex = 255; - getIpING = false; - socket.getIp(); - } - - }); + for(let j = 0; j< 255 ; j ++){ + let wsUrl = 'ws://' + domainIp + j + ':7788/notify?from=plant'; + let ws1 = new WebSocket(wsUrl); + ws1.onopen = function() + { + console.log("ok ") + if(currentIpIndex === 255){ + console.log("ip ",j); + currentIpIndex = j; + // ws1.close(); + }else{ + ws1.close(); + } + + }; + ws1.onmessage = (evt)=>{ + if(currentIpIndex !== 255){ + console.log('onmessage ',currentIpIndex,evt.data); + let info = JSON.parse(evt.data); + if(info.type === 'GET_STORE_ID' && info.data){ + ws1.close(); + init(currentIpIndex,info.data,true); + } + } + + }; + ws1.onerror = function (err) { + totalCount++; + console.log("totalCount ",totalCount) + if(totalCount >= 255){ + console.log("totalCount inin") + totalCount = 0; + currentIpIndex = 255; + getIpING = false; + socket.getIp1(); + } } - } } -}; - -socket.getStoreIdByIp = (ip) => { - let url = 'http://' + domainIp + ip + ':9999/getmyid'; - fetch( url ,{ - // credentials : 'include', - method : 'GET', - mode : 'cors', - }).then(checkStatus) - .then(parseJson) - .then((data) => { - console.log("socket data ",data); - if(data.storeId){ - init(ip,data.storeId,true); - } - }) - .catch((err) =>{ - console.error("Err:",err); - if(!getIpING){ - console.log("getStoreIdByIp=======") - currentIpIndex = 255; - totalCount = 0; - socket.getIp(); - } - }); } +// socket.getStoreIdByIp = (ip) => { +// let url = 'http://' + domainIp + ip + ':9999/getmyid'; +// fetch( url ,{ +// // credentials : 'include', +// method : 'GET', +// mode : 'cors', +// }).then(checkStatus) +// .then(parseJson) +// .then((data) => { +// console.log("socket data ",data); +// if(data.storeId){ +// init(ip,data.storeId,true); +// } +// }) +// .catch((err) =>{ +// console.error("Err:",err); +// if(!getIpING){ +// console.log("getStoreIdByIp=======") +// currentIpIndex = 255; +// totalCount = 0; +// socket.getIp1(); +// } +// }); +// } + const getError = ()=>{ console.error("socket error"); getClose(); } -const 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(); - } + if(!getIpING){ + console.log("socket close========"); + currentIpIndex = 255; + totalCount = 0; + storeId = 0; + socket.getIp1(); } } @@ -116,7 +157,9 @@ const init = (ip,storeid,from = false)=>{ let wsUrl = 'ws://' + domainIp + ip + ':7788/notify?from=plant&storeId=' + storeid; console.log("init :" ,wsUrl); + console.log("========= ","WebSocket" in window,ws,ip,storeid,currentIpIndex,from) if("WebSocket" in window && !ws && ip && storeid && (currentIpIndex === 255 || from)){ + console.log("in websocket"); ws = new WebSocket(wsUrl); ws.onopen = function() @@ -124,6 +167,7 @@ const init = (ip,storeid,from = false)=>{ // Web Socket 已连接上,使用 send() 方法发送数据 // ws.send("发送数据"); currentIpIndex = ip; + storeId = storeid; getIpING = false; console.log("open"); emit(CONFIG.socketType.GETSTOREINFO,{ip:domainIp+ip,storeId:storeid})