1

1.创建eventsource.js文件(eventsource.js的代码在最后面)
2.引入 image.png

import { EventSourcePolyfill } from "@/utils/eventsource.js";

3.调用
image.png

getPresentMsg(){

              let _this = this;
              let url = 'http://192.168.110.183:90'
              this.source = new EventSourcePolyfill(
                url + "/worship/memPresentRecord/presentMsg?type=0&memorialId="+this.memorial.id,
                  headers: {
                    // Authorization: "Bearer " + getToken(),
                    "Content-Type": "text/event-stream;charset=UTF-8",
                    "Cache-Control": "no-cache",
                    Connection: "keep-alive",
                  //重连时间间隔,单位:毫秒,默认45000毫秒,这里设置为10分钟
                  heartbeatTimeout: 10 * 60 * 1000,
              this.source.onopen = () => {
                console.log(1, "NOTICE建立连接");
              this.source.onmessage = (e) => {
                // _this.scrollMessage = e.data;
                let presentId =JSON.parse(  e.data).presentId
                if(  presentId){
                  let obj=jipinList.find(item=>{
                    return item.id ==presentId
                  _this.toSacrifice(obj)
              this.source.onerror = (e) => {
                if (e.readyState == EventSource.CLOSED) {
                  console.log(3, "NOTICE连接关闭");
                } else if (this.source.readyState == EventSource.CONNECTING) {
                  console.log(4, "NOTICE正在重连");
                  //重新设置header
                  // this.source.headers = {
                  //   Authorization: "Bearer " + getToken(),
                  // };
                } else {
                  console.log(5, e);