yuang-framework-ui-common 1.0.71 → 1.0.72
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
package/src/router/index.ts
CHANGED
@@ -1,42 +1,54 @@
|
|
1
1
|
<template>
|
2
2
|
<div>
|
3
3
|
|
4
|
-
<
|
4
|
+
<el-button @click="connect">连接</el-button>
|
5
5
|
<el-button @click="sendMessage">发送消息</el-button>
|
6
|
+
接收到的消息:
|
7
|
+
<div id="messageContainer"></div>
|
6
8
|
</div>
|
7
9
|
</template>
|
8
10
|
|
9
11
|
<script setup lang="ts">
|
10
12
|
import { onMounted, ref } from 'vue';
|
13
|
+
import { ElMessage } from 'element-plus'
|
11
14
|
import { FrameworkSseClient } from '../../../../lib/hooks/framework/frameworkSseClient';
|
12
15
|
import { http } from '../../../../lib/config/httpConfig';
|
13
16
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
container.
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
console.log('SSE 连接成功回调');
|
17
|
+
let frameworkSseClient = null;
|
18
|
+
|
19
|
+
let clientId = '';
|
20
|
+
let connect = () => {
|
21
|
+
// 初始化 SSE 客户端
|
22
|
+
frameworkSseClient = new FrameworkSseClient (
|
23
|
+
'/framework-api/standard/framework-sse/connect',
|
24
|
+
(data) => {
|
25
|
+
|
26
|
+
console.log('data', data);
|
27
|
+
clientId = localStorage.getItem('sseClientId');
|
28
|
+
// 处理接收到的消息
|
29
|
+
const container = document.getElementById('messageContainer');
|
30
|
+
if (container) {
|
31
|
+
const messageDiv = document.createElement('div');
|
32
|
+
messageDiv.className = 'message';
|
33
|
+
messageDiv.textContent = `[${new Date().toLocaleTimeString()}] ${data}`;
|
34
|
+
container.appendChild(messageDiv);
|
35
|
+
}
|
34
36
|
},
|
35
|
-
|
36
|
-
|
37
|
+
{
|
38
|
+
reconnectInterval: 5000, // 重连间隔 5 秒
|
39
|
+
maxReconnectAttempts: 15, // 最大重连 15 次
|
40
|
+
openHandler: () => {
|
41
|
+
console.log('SSE 连接成功回调');
|
42
|
+
ElMessage.success('连接成功');
|
43
|
+
},
|
44
|
+
errorHandler: (error) => {
|
45
|
+
console.error('自定义错误处理:', error);
|
46
|
+
ElMessage.success('出错了');
|
47
|
+
}
|
37
48
|
}
|
38
|
-
|
39
|
-
|
49
|
+
);
|
50
|
+
}
|
51
|
+
|
40
52
|
|
41
53
|
// 页面关闭时手动关闭连接
|
42
54
|
window.addEventListener('beforeunload', () => {
|
@@ -44,7 +56,10 @@ window.addEventListener('beforeunload', () => {
|
|
44
56
|
});
|
45
57
|
|
46
58
|
const sendMessage = ()=>{
|
47
|
-
http.post(`/framework-api/standard/framework-sse/
|
59
|
+
http.post(`/framework-api/standard/framework-sse/sendMessage`, {
|
60
|
+
clientId: clientId,
|
61
|
+
message: "测试"
|
62
|
+
}).then((res) => {
|
48
63
|
|
49
64
|
});
|
50
65
|
}
|