@omni-vue/stomp-websocket 1.0.0 → 1.0.1
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 +1 -1
- package/readme.md +119 -179
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -1,228 +1,168 @@
|
|
|
1
|
-
# @
|
|
1
|
+
# @omni-vue/stomp-websocket
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
一个简洁的 Vue 3 STOMP WebSocket 库,支持自动重连、订阅管理和轮询发送。
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
### useStomp.ts - 核心 STOMP 连接管理
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
# or
|
|
10
|
-
pnpm add @lejurobot/stomp-websocket
|
|
11
|
-
```
|
|
7
|
+
主要功能:
|
|
8
|
+
管理 WebSocket 连接的生命周期,提供基础的连接、订阅、发送功能。
|
|
12
9
|
|
|
13
|
-
|
|
10
|
+
### useStompChannel.ts - 高级通道封装
|
|
14
11
|
|
|
15
|
-
|
|
12
|
+
主要功能:
|
|
13
|
+
在 useStomp 基础上封装,提供轮询发送和自动生命周期管理。
|
|
14
|
+
|
|
15
|
+
## 安装
|
|
16
16
|
|
|
17
17
|
```bash
|
|
18
|
-
npm install vue
|
|
18
|
+
npm install @omni-vue/stomp-websocket
|
|
19
|
+
# 或
|
|
20
|
+
pnpm add @omni-vue/stomp-websocket
|
|
19
21
|
```
|
|
20
22
|
|
|
21
|
-
##
|
|
22
|
-
|
|
23
|
-
This package provides two core composable functions:
|
|
24
|
-
|
|
25
|
-
### 1. `useStomp` - Core STOMP Connection Management
|
|
26
|
-
|
|
27
|
-
Manages WebSocket connection, subscriptions, and message sending.
|
|
28
|
-
|
|
29
|
-
**API:**
|
|
30
|
-
|
|
31
|
-
| Property/Method | Type | Description |
|
|
32
|
-
|----------------|------|-------------|
|
|
33
|
-
| `connectionStatus` | `Ref<ConnectionStatus>` | Connection state: `disconnected` \| `connecting` \| `connected` \| `error` |
|
|
34
|
-
| `messages` | `Ref<any[]>` | Message history (max 50 messages) |
|
|
35
|
-
| `connect` | `() => void` | Establish connection |
|
|
36
|
-
| `disconnect` | `() => void` | Close connection |
|
|
37
|
-
| `subscribe` | `(topic: string, callback: Function) => string` | Subscribe to a topic, returns topic ID |
|
|
38
|
-
| `unsubscribe` | `(topic: string) => void` | Unsubscribe from a topic |
|
|
39
|
-
| `send` | `(destination: string, body: object) => void` | Send a message |
|
|
40
|
-
| `isConnected` | `() => boolean` | Check if connected |
|
|
41
|
-
|
|
42
|
-
### 2. `useStompChannel` - High-Level Channel Abstraction
|
|
43
|
-
|
|
44
|
-
Encapsulates channel-level polling and subscription with automatic lifecycle management.
|
|
45
|
-
|
|
46
|
-
**API:**
|
|
47
|
-
|
|
48
|
-
| Property/Method | Type | Description |
|
|
49
|
-
|----------------|------|-------------|
|
|
50
|
-
| `isConnected` | `Ref<boolean>` | Connection status |
|
|
51
|
-
| `lastMessage` | `Ref<any>` | Last received message |
|
|
52
|
-
| `send` | `(destination: string, payload?: any) => boolean` | Send a message manually |
|
|
53
|
-
| `startAllSending` | `() => void` | Start all polling timers |
|
|
54
|
-
| `stopAllSending` | `() => void` | Stop all polling timers |
|
|
55
|
-
| `unsubscribe` | `() => void` | Unsubscribe from topic |
|
|
56
|
-
|
|
57
|
-
## Usage Examples
|
|
58
|
-
|
|
59
|
-
### Basic Connection
|
|
60
|
-
|
|
61
|
-
```typescript
|
|
62
|
-
import { useStomp } from '@lejurobot/stomp-websocket'
|
|
63
|
-
|
|
64
|
-
const stomp = useStomp('wss://your-server.com/ws')
|
|
65
|
-
|
|
66
|
-
// Connect
|
|
67
|
-
stomp.connect()
|
|
68
|
-
|
|
69
|
-
// Check connection status
|
|
70
|
-
console.log(stomp.connectionStatus.value) // 'connected'
|
|
71
|
-
```
|
|
23
|
+
## 快速开始
|
|
72
24
|
|
|
73
|
-
###
|
|
25
|
+
### 第一步:配置 Pinia Store
|
|
74
26
|
|
|
75
|
-
|
|
76
|
-
import { useStomp } from '@lejurobot/stomp-websocket'
|
|
27
|
+
将 `websocket.ts` 文件放到你的 Pinia stores 目录中:
|
|
77
28
|
|
|
78
|
-
|
|
79
|
-
stomp.connect()
|
|
29
|
+
### 第二步:创建 StompProvider 组件
|
|
80
30
|
|
|
81
|
-
|
|
82
|
-
stomp.subscribe('/topic/notifications', (message) => {
|
|
83
|
-
console.log('Notification:', message)
|
|
84
|
-
})
|
|
31
|
+
创建 `StompProvider.vue` 组件:
|
|
85
32
|
|
|
86
|
-
|
|
87
|
-
stomp.subscribe('/topic/updates', (message) => {
|
|
88
|
-
console.log('Update:', message)
|
|
89
|
-
})
|
|
33
|
+
### 第三步:在 App.vue 中使用
|
|
90
34
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
35
|
+
```vue
|
|
36
|
+
<!-- App.vue -->
|
|
37
|
+
<template>
|
|
38
|
+
<StompProvider>
|
|
39
|
+
<router-view />
|
|
40
|
+
</StompProvider>
|
|
41
|
+
</template>
|
|
95
42
|
|
|
96
|
-
|
|
97
|
-
|
|
43
|
+
<script setup lang="ts">
|
|
44
|
+
import StompProvider from "@/components/Stomp/StompProvider.vue";
|
|
45
|
+
</script>
|
|
98
46
|
```
|
|
99
47
|
|
|
100
|
-
|
|
48
|
+
## 使用示例
|
|
101
49
|
|
|
102
|
-
|
|
103
|
-
// Send to destination
|
|
104
|
-
stomp.send('/app/chat', {
|
|
105
|
-
content: 'Hello World',
|
|
106
|
-
timestamp: new Date().toISOString()
|
|
107
|
-
})
|
|
108
|
-
```
|
|
50
|
+
### 基础用法:轮询获取数据
|
|
109
51
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
import { useStompChannel } from '@lejurobot/stomp-websocket'
|
|
52
|
+
```vue
|
|
53
|
+
<script setup lang="ts">
|
|
54
|
+
import { useStompChannel } from "@lejurobot/stomp-websocket";
|
|
114
55
|
|
|
115
|
-
|
|
116
|
-
const
|
|
117
|
-
|
|
56
|
+
const stompInstance = inject<any>("STOMP_INSTANCE");
|
|
57
|
+
const taskList = ref([]);
|
|
58
|
+
const pagination = ref({ page: 1, pageSize: 10, total: 0 });
|
|
118
59
|
|
|
119
|
-
const
|
|
120
|
-
subscribeTopic:
|
|
60
|
+
const { send: sendTaskListRequest } = useStompChannel(stompInstance, {
|
|
61
|
+
subscribeTopic: "/user/topic/all",
|
|
121
62
|
sends: [
|
|
122
63
|
{
|
|
123
|
-
destination:
|
|
124
|
-
interval:
|
|
125
|
-
|
|
126
|
-
|
|
64
|
+
destination: "/web/task.send",
|
|
65
|
+
interval: 2000, // 每 2 秒轮询一次
|
|
66
|
+
// 传入回调函数,用于动态生成载荷参数
|
|
67
|
+
payload: () => ({
|
|
68
|
+
from: "serve",
|
|
69
|
+
content: {
|
|
70
|
+
page: pagination.value.page,
|
|
71
|
+
size: pagination.value.pageSize,
|
|
72
|
+
},
|
|
73
|
+
}),
|
|
74
|
+
},
|
|
127
75
|
],
|
|
128
|
-
onMessage: (payload) => {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
76
|
+
onMessage: (payload: any) => {
|
|
77
|
+
const data = payload.content;
|
|
78
|
+
taskList.value = data.rows;
|
|
79
|
+
pagination.value.total = data.total;
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
// 手动刷新数据
|
|
84
|
+
const refreshTaskList = () => {
|
|
85
|
+
send("/web/task.send", {
|
|
86
|
+
content: {
|
|
87
|
+
page: pagination.value.page,
|
|
88
|
+
size: pagination.value.pageSize,
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
};
|
|
135
92
|
|
|
136
|
-
|
|
137
|
-
const
|
|
138
|
-
subscribeTopic:
|
|
93
|
+
// 可订阅多个主题:设备状态轮询
|
|
94
|
+
const { send: sendDeviceStatus } = useStompChannel(stompInstance, {
|
|
95
|
+
subscribeTopic: "/user/device/status",
|
|
139
96
|
sends: [
|
|
140
97
|
{
|
|
141
|
-
destination:
|
|
98
|
+
destination: "/app/device-status",
|
|
142
99
|
interval: 2000,
|
|
143
|
-
payload: () => ({ deviceId:
|
|
100
|
+
payload: () => ({ deviceId: "001" }),
|
|
144
101
|
},
|
|
145
|
-
{
|
|
146
|
-
destination: '/app/alarms',
|
|
147
|
-
interval: 5000,
|
|
148
|
-
payload: () => ({ level: 'high' })
|
|
149
|
-
}
|
|
150
102
|
],
|
|
151
103
|
onMessage: (payload) => {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
104
|
+
console.log("设备状态:", payload);
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
</script>
|
|
156
108
|
```
|
|
157
109
|
|
|
158
|
-
|
|
110
|
+
## API 文档
|
|
159
111
|
|
|
160
|
-
|
|
112
|
+
### useStomp
|
|
161
113
|
|
|
162
|
-
|
|
163
|
-
<template>
|
|
164
|
-
<slot />
|
|
165
|
-
</template>
|
|
166
|
-
|
|
167
|
-
<script setup lang="ts">
|
|
168
|
-
import { useStomp } from '@lejurobot/stomp-websocket'
|
|
169
|
-
import { useWebsocketStore } from '@/stores/websocket'
|
|
170
|
-
import { storeToRefs } from 'pinia'
|
|
171
|
-
|
|
172
|
-
const wsStore = useWebsocketStore()
|
|
173
|
-
const { finalWsUrl } = storeToRefs(wsStore)
|
|
174
|
-
|
|
175
|
-
const stomp = useStomp(finalWsUrl)
|
|
176
|
-
stomp.connect()
|
|
177
|
-
|
|
178
|
-
// Auto-reconnect on URL change
|
|
179
|
-
watch(finalWsUrl, (newUrl, oldUrl) => {
|
|
180
|
-
if (newUrl !== oldUrl && oldUrl !== undefined) {
|
|
181
|
-
stomp.disconnect()
|
|
182
|
-
setTimeout(() => stomp.connect(), 100)
|
|
183
|
-
}
|
|
184
|
-
})
|
|
114
|
+
核心连接管理函数。
|
|
185
115
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
116
|
+
```typescript
|
|
117
|
+
const stomp = useStomp(url: string | Ref<string>)
|
|
118
|
+
|
|
119
|
+
// 返回值
|
|
120
|
+
{
|
|
121
|
+
connectionStatus, // 连接状态
|
|
122
|
+
messages, // 消息历史
|
|
123
|
+
connect, // 建立连接
|
|
124
|
+
disconnect, // 断开连接
|
|
125
|
+
subscribe, // 订阅主题
|
|
126
|
+
unsubscribe, // 取消订阅
|
|
127
|
+
send, // 发送消息
|
|
128
|
+
isConnected, // 检查连接
|
|
129
|
+
}
|
|
193
130
|
```
|
|
194
131
|
|
|
195
|
-
|
|
132
|
+
### useStompChannel
|
|
196
133
|
|
|
197
|
-
|
|
198
|
-
<script setup lang="ts">
|
|
199
|
-
const stomp = inject<any>('STOMP_INSTANCE')
|
|
200
|
-
|
|
201
|
-
// Subscribe to multiple topics
|
|
202
|
-
const topic1 = stomp.subscribe('/topic/news', (msg) => {
|
|
203
|
-
console.log('News:', msg)
|
|
204
|
-
})
|
|
134
|
+
高级通道封装,支持轮询和自动管理。
|
|
205
135
|
|
|
206
|
-
|
|
207
|
-
|
|
136
|
+
```typescript
|
|
137
|
+
const channel = useStompChannel(stompInstance, {
|
|
138
|
+
subscribeTopic: string, // 订阅的主题
|
|
139
|
+
sends: [{ // 轮询配置数组
|
|
140
|
+
destination: string, // 发送目标
|
|
141
|
+
interval: number, // 轮询间隔(毫秒)
|
|
142
|
+
payload: () => any, // 载荷生成函数
|
|
143
|
+
}],
|
|
144
|
+
onMessage: (payload) => {}, // 消息回调
|
|
145
|
+
onConnected: (sendFn) => {}, // 连接成功回调
|
|
208
146
|
})
|
|
209
147
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
148
|
+
// 返回值
|
|
149
|
+
{
|
|
150
|
+
isConnected, // 连接状态
|
|
151
|
+
lastMessage, // 最后一条消息
|
|
152
|
+
send, // 手动发送消息
|
|
153
|
+
startAllSending, // 启动所有轮询
|
|
154
|
+
stopAllSending, // 停止所有轮询
|
|
155
|
+
unsubscribe, // 取消订阅
|
|
156
|
+
}
|
|
215
157
|
```
|
|
216
158
|
|
|
217
|
-
##
|
|
159
|
+
## 特性
|
|
218
160
|
|
|
219
|
-
- ✅
|
|
220
|
-
- ✅
|
|
221
|
-
- ✅
|
|
222
|
-
- ✅
|
|
223
|
-
- ✅
|
|
224
|
-
- ✅ **Vue 3 Composable** - Designed for Vue 3 composition API
|
|
225
|
-
- ✅ **Lifecycle Management** - Automatic cleanup on component unmount
|
|
161
|
+
- ✅ 自动重连 - 断线后自动重连,恢复订阅
|
|
162
|
+
- ✅ 轮询支持 - 内置定时轮询发送
|
|
163
|
+
- ✅ 多主题订阅 - 支持订阅多个主题
|
|
164
|
+
- ✅ TypeScript - 完整类型定义
|
|
165
|
+
- ✅ 生命周期管理 - 组件卸载自动清理
|
|
226
166
|
|
|
227
167
|
## License
|
|
228
168
|
|