|
@@ -12,18 +12,18 @@
|
|
|
</div>
|
|
|
</template>
|
|
|
<ul>
|
|
|
- <li v-for="(item, idx) in receivedData" :key="idx"> {{ item }}</li>
|
|
|
+ <li v-for="(item, idx) in receivedData" :key="idx">{{ item }}</li>
|
|
|
</ul>
|
|
|
</el-popover>
|
|
|
</template>
|
|
|
<script setup>
|
|
|
-import { onMounted, ref, onBeforeUnmount } from 'vue';
|
|
|
-import {ls} from "@/utils";
|
|
|
+import { onMounted, ref, onBeforeUnmount } from 'vue'
|
|
|
+import { ls } from '@/utils'
|
|
|
const token = ls.get('token')
|
|
|
-const { VITE_APP_BASE_API } = import.meta.env;
|
|
|
+const { VITE_APP_BASE_API } = import.meta.env
|
|
|
// const serviceUrlApi = ref(`${VITE_APP_BASE_API}/sys/sse/connect`);
|
|
|
-const serviceUrlApi = ref(`http://localhost:3000/events`);
|
|
|
-let sseService = ref(null);
|
|
|
+const serviceUrlApi = ref(`http://localhost:3000/events`)
|
|
|
+let sseService = ref(null)
|
|
|
let receivedData = ref([])
|
|
|
|
|
|
// 创建一个新的 EventSource,但首先使用 fetch API 发送带有 Authorization 头的请求
|
|
@@ -35,49 +35,36 @@ function connectSSEWithHeaders(url, headers = {}) {
|
|
|
mode: 'cors', // no-cors, cors, *same-origin
|
|
|
credentials: 'same-origin', // include, *same-origin, omit
|
|
|
redirect: 'follow', // manual, *follow, error
|
|
|
- referrerPolicy: 'no-referrer', // no-referrer, *client
|
|
|
+ referrerPolicy: 'no-referrer' // no-referrer, *client
|
|
|
})
|
|
|
.then(response => {
|
|
|
if (!response.ok) {
|
|
|
- throw new Error('Network response was not ok');
|
|
|
+ throw new Error('Network response was not ok')
|
|
|
}
|
|
|
- return new EventSource(url);
|
|
|
- // 读取响应体作为文本,然后解析URL来创建 EventSource
|
|
|
- // const t = response.json()
|
|
|
- // console.log(t);
|
|
|
- // return response.text().then(text => {
|
|
|
- // console.info('text', text)
|
|
|
- // // 这里通常不需要解析text,因为我们只是要确认连接成功。
|
|
|
- // // 但如果SSE URL在响应体中,你可能需要处理它。
|
|
|
- // // 假设服务器直接支持SSE在原始URL
|
|
|
- //
|
|
|
- // });
|
|
|
+ return new EventSource(url)
|
|
|
})
|
|
|
.catch(error => {
|
|
|
- console.error('There was a problem with your fetch operation:', error);
|
|
|
- });
|
|
|
+ console.error('There was a problem with your fetch operation:', error)
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
-
|
|
|
onMounted(() => {
|
|
|
- connectSSEWithHeaders(serviceUrlApi.value, { 'accessToken': token })
|
|
|
- .then(eventSource => {
|
|
|
- sseService = eventSource
|
|
|
- eventSource.onmessage = function(e) {
|
|
|
- console.log(e, '----====----');
|
|
|
- console.log('后端返回的数据:', e.data);
|
|
|
- receivedData.value.push(e.data)
|
|
|
- }
|
|
|
- eventSource.onerror = function(err) {
|
|
|
- console.error('EventSource failed:', err);
|
|
|
- };
|
|
|
- })
|
|
|
+ connectSSEWithHeaders(serviceUrlApi.value, { accessToken: token }).then(eventSource => {
|
|
|
+ sseService.value = eventSource
|
|
|
+ eventSource.onmessage = function (e) {
|
|
|
+ console.log('后端返回的数据:', e.data)
|
|
|
+ receivedData.value.push(e.data)
|
|
|
+ }
|
|
|
+ eventSource.onerror = function (err) {
|
|
|
+ console.error('EventSource failed:', err)
|
|
|
+ }
|
|
|
+ })
|
|
|
})
|
|
|
|
|
|
onBeforeUnmount(() => {
|
|
|
- if (sseService) {
|
|
|
- sseService.close();
|
|
|
- sseService = null;
|
|
|
+ if (sseService.value) {
|
|
|
+ sseService.value.close()
|
|
|
+ sseService.value = null
|
|
|
}
|
|
|
-});
|
|
|
-</script>
|
|
|
+})
|
|
|
+</script>
|