生产看板视频修改

This commit is contained in:
朱菊兰 2025-04-25 10:59:36 +08:00
parent 446fa9a0d1
commit f24b769305
2 changed files with 70 additions and 31 deletions

Binary file not shown.

View File

@ -37,14 +37,17 @@ export default {
return {
isFullScreen: false,
scaleNum: 0.8,
dataObj:{}
dataObj:{},
sseReader: null, //
abortController: null, // fetch
retryCount: 0, //
isDestroyed: false //
};
},
created() {
this.init()
},
mounted() {
console.log('dataBoard mounted')
this.boxReset = debounce(() => {
this.resetSize()
}, 300)
@ -54,8 +57,8 @@ export default {
})
this.getData()
},
destroyed() {
console.log('dataBoard destroyed')
beforeDestroy() {
this.closeSSE();
},
computed: {
sidebarOpened() {
@ -68,39 +71,75 @@ export default {
}
},
methods: {
getData() {
async getData() {
let _this = this;
if (_this.isDestroyed) return;
const url = process.env.VUE_APP_BASE_API+'/admin-api/monitoring/message/subscribe/'+store.getters.userId+'-'+Date.now();
const token = getAccessToken()
const headers = new Headers({
'Authorization': `Bearer ${token}`,
'Content-Type': 'text/event-stream'
});
fetch(url, { headers })
.then(response => {
const reader = response.body.getReader();
try {
//
this.abortController = new AbortController();
// fetch
const response = await fetch(url, {
method: 'GET',
headers: headers,
signal: _this.abortController.signal //
});
//
_this.sseReader = response.body.getReader();
const decoder = new TextDecoder();
const readStream = () => {
reader.read().then(({ done, value }) => {
//
while (true) {
const { done, value } = await _this.sseReader.read();
if (done) {
console.log('SSE 连接关闭');
return;
console.log('SSE 连接正常关闭');
_this.handleReconnect(); //
break;
}
// SSE
const data = decoder.decode(value);
console.log('收到消息:', data);
if (_this.isValidData(data)){
_this.upDateMsg(data);
}
readStream(); //
}).catch(error => {
console.error('SSE 读取错误:', error);
});
};
readStream();
})
.catch(error => {
console.error('SSE 连接失败:', error);
});
}
} catch (error) {
//
if (error.name === 'AbortError') return;
console.error('SSE 连接异常:', error);
_this.handleReconnect(); //
}
},
closeSSE() {
this.isDestroyed = true; //
if (this.abortController) {
this.abortController.abort(); // fetch
}
if (this.sseReader) {
this.sseReader.cancel(); //
this.sseReader = null;
}
console.log('SSE 连接已强制关闭');
},
handleReconnect() {
if (this.isDestroyed) return;
// 退 5
const maxRetries = 5;
if (this.retryCount < maxRetries) {
const delay = Math.pow(2, this.retryCount) * 1000;
setTimeout(() => {
this.retryCount++;
this.initSSE();
}, delay);
} else {
console.error('SSE 重连次数已达上限');
}
},
isValidData (data) {
return data.trim().startsWith('data:{') && !data.includes('heartbeat');