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

sse

parent dd1b105b
...@@ -4323,6 +4323,11 @@ ...@@ -4323,6 +4323,11 @@
"integrity": "sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc=", "integrity": "sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc=",
"dev": true "dev": true
}, },
"event-source-polyfill": {
"version": "1.0.31",
"resolved": "https://registry.npmmirror.com/event-source-polyfill/-/event-source-polyfill-1.0.31.tgz",
"integrity": "sha512-4IJSItgS/41IxN5UVAVuAyczwZF7ZIEsM1XAoUzIHA6A+xzusEZUutdXz2Nr+MQPLxfTiCvqE79/C8HT8fKFvA=="
},
"eventemitter3": { "eventemitter3": {
"version": "4.0.7", "version": "4.0.7",
"resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-4.0.7.tgz", "resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-4.0.7.tgz",
......
...@@ -7,6 +7,8 @@ import { ...@@ -7,6 +7,8 @@ import {
saveAs saveAs
} from 'file-saver'; } from 'file-saver';
import { ACCESS_TOKEN, ACCESS_USER, HTTP_STATUS } from "../constant/user"; import { ACCESS_TOKEN, ACCESS_USER, HTTP_STATUS } from "../constant/user";
import { EventSourcePolyfill } from "event-source-polyfill";
/** /**
...@@ -844,6 +846,29 @@ let httpSSERequest = (url, success, error) => { ...@@ -844,6 +846,29 @@ let httpSSERequest = (url, success, error) => {
} }
} }
/**
* 支持请求头的
* @param {*} url
* @param {*} success
* @param {*} error
*/
let httpSSERequestPoly = (url, success, error) => {
const evtSource = new EventSourcePolyfill(url, {
headers: addHeaders({})
})
evtSource.onmessage = function (event) {
let result = JSON.parse(event.data);
if (result.code == 9000) {
success(result.data);
} else {
console.log(event.message);
}
}
evtSource.onerror = function (event) {
error(event);
}
}
/** /**
* 通过fetch去流式读取 * 通过fetch去流式读取
* @param {*} url * @param {*} url
...@@ -893,6 +918,7 @@ let fetchEventSource = (url, data, options = {}) => { ...@@ -893,6 +918,7 @@ let fetchEventSource = (url, data, options = {}) => {
}); });
}); });
} }
//#endregion //#endregion
export { export {
...@@ -935,6 +961,7 @@ export { ...@@ -935,6 +961,7 @@ export {
fileUploadProgress, fileUploadProgress,
post_machineWriting, post_machineWriting,
httpSSERequest, httpSSERequest,
httpSSERequestPoly,
uploadOSS, uploadOSS,
uploadOSSProgress, uploadOSSProgress,
fetchEventSource fetchEventSource
......
...@@ -77,7 +77,7 @@ ...@@ -77,7 +77,7 @@
import "../assets/css/home.css"; import "../assets/css/home.css";
// import jh_bg from "../assets/img/main/logo-bg.png"; // import jh_bg from "../assets/img/main/logo-bg.png";
// import jh from "/static/img/main/logo.png"; // import jh from "/static/img/main/logo.png";
import { endLoading, get, post, httpSSERequest, fetchEventSource } from "../util/http_util"; import { endLoading, get, post, httpSSERequest, fetchEventSource, httpSSERequestPoly } from "../util/http_util";
import { queryDictItem } from '../api/dictitem'; import { queryDictItem } from '../api/dictitem';
import Push from 'push.js' import Push from 'push.js'
import dayjs from 'dayjs'; import dayjs from 'dayjs';
...@@ -272,36 +272,46 @@ export default { ...@@ -272,36 +272,46 @@ export default {
const weekList = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']; const weekList = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
this.myDate.week = weekList[dayjs().get('day')] this.myDate.week = weekList[dayjs().get('day')]
fetchEventSource("api/api_ws/connect", {}, { // fetchEventSource("api/api_ws/connect", {}, {
onopen: () => { // onopen: () => {
console.log("打开"); // console.log("打开");
}, // },
onclose: () => { // onclose: () => {
console.log("关闭"); // console.log("关闭");
}, // },
onmessage: (value) => { // onmessage: (value) => {
console.log(value); // console.log(value);
value = value.replace("data:", ""); // value = value.replace("data:", "");
if (!value) { // if (!value) {
return; // return;
} // }
this.$notify({ // this.$notify({
title: '成功', // title: '成功',
message: value, // message: value,
type: 'success' // type: 'success'
}); // });
} // }
}).then(res => { // }).then(res => {
}).catch(err => { // }).catch(err => {
}); // });
// httpSSERequest("api/api_ws/connect", (data) => { // httpSSERequest("api/api_ws/connect", (data) => {
// console.log(data) // console.log(data)
// }, (err) => { // }, (err) => {
// console.log(err) // console.log(err)
// }); // });
httpSSERequestPoly("api/api_ws/connect", (data) => {
this.$notify({
title: '消息',
message: data,
type: 'success'
});
}, (err) => {
console.log(err)
});
}, },
computed: { computed: {
userInfo() { userInfo() {
......
...@@ -113,7 +113,7 @@ module.exports = { ...@@ -113,7 +113,7 @@ module.exports = {
//后台代理 //后台代理
proxy: { proxy: {
'/api/': { '/api/': {
target: 'http://localhost:8081', target: 'http://192.168.168.213:8081',
ws: true, ws: true,
secure: false, secure: false,
changeOrigin: true, changeOrigin: true,
......
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