websocket同步推送
需求: 要求websocket推送状态, 是一秒切换一次
对websocket传来的数据进行校验
export function evalPushData(val) {
const failList = [null, undefined, ""]
if (failList.includes(val)) return
try {
return JSON.parse(val)
} catch (error) {
console.error('eroor=>', error);
return val
}
}
store处理传入来的数据
GET_SOFT_VERSION_ROLLBACK(state, val) {
state.softVersionRollBackInfo = evalPushData(val)
},
/**
* @method 获取INFO-CHANGE 的推送
* @param {Any} storeData 获取的Info-Change推送数据
* @param {Array} tableData 表格数据
* @return {Array} 表格数据
*/
export async function getInfoChangePushData(storeData, tableData) {
if (storeData instanceof Array) {
for (let i = 0; i < tableData.length; i++) {
const tabelItem = tableData[i];
for (let j = 0; j < storeData.length; j++) {
const infoChangeItem = storeData[j];
if (tabelItem.serialNumber === infoChangeItem.serialNumber) {
tabelItem.serialNumber = infoChangeItem.serialNumber;
tabelItem.enbStatus = infoChangeItem.status;
tabelItem.enbIp = infoChangeItem.enbIp;
await delay()
}
}
}
} else if (storeData instanceof Object) {
for (let i = 0; i < tableData.length; i++) {
const item = tableData[i];
if (item.serialNumber === storeData.serialNumber) {
item.serialNumber = storeData.serialNumber;
item.enbStatus = storeData.status;
item.enbIp = storeData.enbIp;
await delay()
}
}
} else {
console.log('getInfoChangePushData==>', storeData);
}
return tableData
}
//延迟方法,延迟展示内容
async function delay(msg) {
return new Promise((resolve, reject) => {
setTimeout(resolve, 1000, msg)
})
}
method:{
async delay(msg) {
return new Promise((resolve, reject) => {
setTimeout(resolve, 1000, msg)
})
}
},
watch: {
async fileAsyncInfo(newValue, oldValue) {
if (newValue) {
const ids = []
newValue.forEach(item => {
if (item.data) {
ids.push({
serialNumber: item.data.split("_")[0],
syncStatus: item.data.split("_")[1]
})
}
})
for (let i = 0; i < this.tableData.length; i++) {
const item = this.tableData[i];
for (let j = 0; j < ids.length; j++) {
const val = ids[j];
if (item.serialNumber === val.serialNumber) {
this.$set(item, 'syncStatus', val.syncStatus)
this.$set(item, 'serialNumber', val.serialNumber)
await this.delay()
}
}
}
}
},
async enbInfoChageInfo(newValue, oldValue) {
if (newValue) {
this.tableData = await getInfoChangePushData(newValue, this.tableData)
}
}
}
本文来自博客园,作者:云霄紫潭,转载请注明原文链接:https://www.cnblogs.com/0520euv/p/17293250.html