Commit 56945370 by Zhang Xin

重构socket

parent bbeac87c
......@@ -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 {
......
const socketType = {
GETSTOREINFO:'GETSTOREINFO',
GETSOCKETMSG:'GETSOCKETMSG'
GETSOCKETMSG:'GETSOCKETMSG',
SOCKETERROR : 'SOCKETERROR'
}
module.exports = socketType;
\ No newline at end of file
......@@ -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{
......
......@@ -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
......@@ -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);
......
......@@ -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,61 +19,50 @@ 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++){
let url = 'http://' + domainIp + i + ':9999/getmyid';
console.log(url)
fetch( url ,{
// credentials : 'include',
method : 'GET',
mode : 'cors',
}).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;
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();
}
}
});
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();
}
});
}
}
}
};
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
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