Commit 46daa5f4 by Zhang Xin

fix getip by socket not by http

parent d14515d4
...@@ -595,7 +595,7 @@ class PageContainer extends React.Component { ...@@ -595,7 +595,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("没有用户信息或门店信息 ip:"+commonInfo.ip); this.showError("没有用户信息或门店信息 ip:"+commonInfo.ip+" storeId:"+(commonInfo.storeInfo ? commonInfo.storeInfo.id : 0));
} }
}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.getIp(); UTILPATH.socket.getIp1();
// UTILPATH.socket.getIp(); // UTILPATH.socket.getIp();
renderPage(store); renderPage(store);
\ No newline at end of file
...@@ -10,6 +10,7 @@ let totalCount = 0; ...@@ -10,6 +10,7 @@ let totalCount = 0;
let getIpING = false; //正在获取IP let getIpING = false; //正在获取IP
let currentIpIndex = 255; let currentIpIndex = 255;
let ws = null; let ws = null;
let storeId = 0
const checkStatus = (response)=>{ const checkStatus = (response)=>{
if (response.status >= 200 && response.status < 300) { if (response.status >= 200 && response.status < 300) {
...@@ -24,89 +25,129 @@ const parseJson = (response)=>{ ...@@ -24,89 +25,129 @@ const parseJson = (response)=>{
return response.json(); return response.json();
}; };
socket.getIp = ()=>{ // socket.getIp = ()=>{
console.log("getIp ",getIpING,currentIpIndex); // console.log("getIp ",getIpING,currentIpIndex);
if(!getIpING){ // 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; getIpING = true;
for(let i = 0; i < 255 ; i ++){ for(let j = 0; j< 255 ; j ++){
let url = 'http://' + domainIp + i + ':9999/getmyid'; let wsUrl = 'ws://' + domainIp + j + ':7788/notify?from=plant';
if(currentIpIndex === 255 && getIpING){ let ws1 = new WebSocket(wsUrl);
fetch( url ,{ ws1.onopen = function()
// credentials : 'include', {
method : 'GET', console.log("ok ")
mode : 'cors', if(currentIpIndex === 255){
}).then(checkStatus) console.log("ip ",j);
.then(parseJson) currentIpIndex = j;
.then((data) => { // ws1.close();
console.error("data :",data,currentIpIndex,i); }else{
if(data.storeId && currentIpIndex === 255){ ws1.close();
init(i,data.storeId); }
}
}) };
.catch((err) =>{ ws1.onmessage = (evt)=>{
console.error("Err:",err); if(currentIpIndex !== 255){
totalCount++; console.log('onmessage ',currentIpIndex,evt.data);
if(totalCount >= 255){ let info = JSON.parse(evt.data);
totalCount = 0; if(info.type === 'GET_STORE_ID' && info.data){
currentIpIndex = 255; ws1.close();
getIpING = false; init(currentIpIndex,info.data,true);
socket.getIp(); }
} }
}); };
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 = ()=>{ const getError = ()=>{
console.error("socket error"); console.error("socket error");
getClose(); getClose();
} }
const getClose = ()=>{ const getClose = ()=>{
console.error("socket close ",currentIpIndex); console.error("socket close ",currentIpIndex);
//如果有currentIp就没有必要再次循环 //如果有currentIp就没有必要再次循环
ws = null; ws = null;
if(currentIpIndex !== 255){ if(!getIpING){
//只需再次获取storeId即可,以防storeId有变化 console.log("socket close========");
socket.getStoreIdByIp(currentIpIndex); currentIpIndex = 255;
}else{ totalCount = 0;
//重新获取IP storeId = 0;
if(!getIpING){ socket.getIp1();
console.log("socket close========")
currentIpIndex = 255;
totalCount = 0;
socket.getIp();
}
} }
} }
...@@ -116,7 +157,9 @@ const init = (ip,storeid,from = false)=>{ ...@@ -116,7 +157,9 @@ 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);
console.log("========= ","WebSocket" in window,ws,ip,storeid,currentIpIndex,from)
if("WebSocket" in window && !ws && ip && storeid && (currentIpIndex === 255 || from)){ if("WebSocket" in window && !ws && ip && storeid && (currentIpIndex === 255 || from)){
console.log("in websocket");
ws = new WebSocket(wsUrl); ws = new WebSocket(wsUrl);
ws.onopen = function() ws.onopen = function()
...@@ -124,6 +167,7 @@ const init = (ip,storeid,from = false)=>{ ...@@ -124,6 +167,7 @@ const init = (ip,storeid,from = false)=>{
// Web Socket 已连接上,使用 send() 方法发送数据 // Web Socket 已连接上,使用 send() 方法发送数据
// ws.send("发送数据"); // ws.send("发送数据");
currentIpIndex = ip; currentIpIndex = ip;
storeId = storeid;
getIpING = false; getIpING = false;
console.log("open"); console.log("open");
emit(CONFIG.socketType.GETSTOREINFO,{ip:domainIp+ip,storeId:storeid}) emit(CONFIG.socketType.GETSTOREINFO,{ip:domainIp+ip,storeId:storeid})
......
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