Commit dd1b105b authored by 以墨为白's avatar 以墨为白 🎧

sse

parent 5034cbdb
......@@ -862,7 +862,7 @@ let fetchEventSource = (url, data, options = {}) => {
}).then(response => {
// 检查响应是否成功
if (!response.ok) {
throw new Error('Network response was not ok');
reject('Network response was not ok');
}
// 返回一个可读流
options.onopen && options.onopen();
......@@ -881,7 +881,7 @@ let fetchEventSource = (url, data, options = {}) => {
}
// 处理每个数据块
// console.log('收到的数据:', value);
options.onmessage && options.onmessage(new TextDecoder().decode(value));
options.onmessage && options.onmessage(new TextDecoder().decode(value).replace(/[\t\r\f\n\s]*/g, ''));
// 继续读取下一个数据块
return push();
});
......@@ -889,7 +889,6 @@ let fetchEventSource = (url, data, options = {}) => {
// 开始读取数据流
return push();
}).catch(error => {
return fetchEventSource(url, data, options);
options.error && options.error(error);
});
});
......
......@@ -272,7 +272,7 @@ export default {
const weekList = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
this.myDate.week = weekList[dayjs().get('day')]
fetchEventSource("api/api_ws/getMsg1", {}, {
fetchEventSource("api/api_ws/connect", {}, {
onopen: () => {
console.log("打开");
},
......@@ -280,12 +280,24 @@ export default {
console.log("关闭");
},
onmessage: (value) => {
let msg = value.split('\n');
console.log(value);
value = value.replace("data:", "");
if (!value) {
return;
}
this.$notify({
title: '成功',
message: value,
type: 'success'
});
}
}).then(res => {
}).catch(err => {
});
// httpSSERequest("api/api_ws/stream-sse", (data) => {
// httpSSERequest("api/api_ws/connect", (data) => {
// console.log(data)
// }, (err) => {
// console.log(err)
......
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