网站原型图设计软件,广东手机网站建设报价表,杭州做兼职网站,贵州做网站的公司有哪些前言
uniapp默认是不支持event-source#xff0c;这里是借助renderjs进行SSE连接
正文
引入event-source-polyfill 这里演示的是直接将代码下载到本地进行引入 下载地址 把里面的eventsource.min.js文件放到项目中的static文件夹 项目封装event-source.vue组件
templ…前言
uniapp默认是不支持event-source这里是借助renderjs进行SSE连接
正文
引入event-source-polyfill 这里演示的是直接将代码下载到本地进行引入 下载地址 把里面的eventsource.min.js文件放到项目中的static文件夹 项目封装event-source.vue组件
templateview :propinfo :change:propsse.onChange idrenderjs-view /
/templatescript
import { BASE_URL } from /utils/config
import { getToken } from /utils/auth
import { storage } from /utilsexport default {data() {return {info: {}}},created() {this.setInfo()},methods: {setInfo() {this.info { url: BASE_URL, token: getToken(), userId: storage.getItem(userId) }},onMessage(e) {this.$emit(message, e)}}
}
/scriptscript modulesse langrenderjsexport default {data() {return { sseInfo:{}}},mounted() {this.setSSE()},methods: {setSSE() {if (typeof EventSourcePolyfill function) {this.initSSE()} else {const script document.createElement(script)script.src static/js/eventsource.min.jsscript.type text/javascriptscript.onload this.initSSE.bind(this)document.head.appendChild(script)}},initSSE() {const sse new EventSourcePolyfill(${this.sseInfo.url}/sse/system-sse/sse-server?userId${this.sseInfo.userId}_${Date.now()}, {headers: {Authorization: Bearer this.sseInfo.token,Tenant-Id: 1}})sse.onmessage (e) {this.$ownerInstance.callMethod(onMessage, e)}},onChange(value) {this.sseInfo value},}}
/script外部使用
EventSource messageonMessage/script langts setupconst onMessage (e) {// uni.showModal({// title: sse消息,// content: e.data,// })console.log(sse消息,e)}
/script