@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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/readme.md +119 -179
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omni-vue/stomp-websocket",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "A Vue 3 STOMP WebSocket library with composable hooks",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
package/readme.md CHANGED
@@ -1,228 +1,168 @@
1
- # @lejurobot/stomp-websocket
1
+ # @omni-vue/stomp-websocket
2
2
 
3
- A Vue 3 STOMP WebSocket library with composable hooks for real-time messaging.
3
+ 一个简洁的 Vue 3 STOMP WebSocket 库,支持自动重连、订阅管理和轮询发送。
4
4
 
5
- ## Installation
5
+ ### useStomp.ts - 核心 STOMP 连接管理
6
6
 
7
- ```bash
8
- npm install @lejurobot/stomp-websocket
9
- # or
10
- pnpm add @lejurobot/stomp-websocket
11
- ```
7
+ 主要功能:
8
+ 管理 WebSocket 连接的生命周期,提供基础的连接、订阅、发送功能。
12
9
 
13
- ## Peer Dependencies
10
+ ### useStompChannel.ts - 高级通道封装
14
11
 
15
- Make sure you have these installed:
12
+ 主要功能:
13
+ 在 useStomp 基础上封装,提供轮询发送和自动生命周期管理。
14
+
15
+ ## 安装
16
16
 
17
17
  ```bash
18
- npm install vue @stomp/stompjs
18
+ npm install @omni-vue/stomp-websocket
19
+ # 或
20
+ pnpm add @omni-vue/stomp-websocket
19
21
  ```
20
22
 
21
- ## What This Package Provides
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
- ### Subscribe to Multiple Topics
25
+ ### 第一步:配置 Pinia Store
74
26
 
75
- ```typescript
76
- import { useStomp } from '@lejurobot/stomp-websocket'
27
+ 将 `websocket.ts` 文件放到你的 Pinia stores 目录中:
77
28
 
78
- const stomp = useStomp('wss://your-server.com/ws')
79
- stomp.connect()
29
+ ### 第二步:创建 StompProvider 组件
80
30
 
81
- // Subscribe to topic 1
82
- stomp.subscribe('/topic/notifications', (message) => {
83
- console.log('Notification:', message)
84
- })
31
+ 创建 `StompProvider.vue` 组件:
85
32
 
86
- // Subscribe to topic 2
87
- stomp.subscribe('/topic/updates', (message) => {
88
- console.log('Update:', message)
89
- })
33
+ ### 第三步:在 App.vue 中使用
90
34
 
91
- // Subscribe to topic 3
92
- stomp.subscribe('/user/queue/messages', (message) => {
93
- console.log('Private message:', message)
94
- })
35
+ ```vue
36
+ <!-- App.vue -->
37
+ <template>
38
+ <StompProvider>
39
+ <router-view />
40
+ </StompProvider>
41
+ </template>
95
42
 
96
- // Unsubscribe from a specific topic
97
- stomp.unsubscribe('/topic/notifications')
43
+ <script setup lang="ts">
44
+ import StompProvider from "@/components/Stomp/StompProvider.vue";
45
+ </script>
98
46
  ```
99
47
 
100
- ### Send Messages
48
+ ## 使用示例
101
49
 
102
- ```typescript
103
- // Send to destination
104
- stomp.send('/app/chat', {
105
- content: 'Hello World',
106
- timestamp: new Date().toISOString()
107
- })
108
- ```
50
+ ### 基础用法:轮询获取数据
109
51
 
110
- ### Channel with Polling
111
-
112
- ```typescript
113
- import { useStompChannel } from '@lejurobot/stomp-websocket'
52
+ ```vue
53
+ <script setup lang="ts">
54
+ import { useStompChannel } from "@lejurobot/stomp-websocket";
114
55
 
115
- // Assuming you have a stomp instance from useStomp
116
- const stomp = useStomp('wss://your-server.com/ws')
117
- stomp.connect()
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 channel = useStompChannel(stomp, {
120
- subscribeTopic: '/user/queue/data',
60
+ const { send: sendTaskListRequest } = useStompChannel(stompInstance, {
61
+ subscribeTopic: "/user/topic/all",
121
62
  sends: [
122
63
  {
123
- destination: '/app/request-data',
124
- interval: 3000, // Poll every 3 seconds
125
- payload: () => ({ page: 1, size: 10 })
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
- console.log('Received:', payload)
130
- }
131
- })
132
- ```
133
-
134
- ### Multiple Polling Destinations
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
- ```typescript
137
- const channel = useStompChannel(stomp, {
138
- subscribeTopic: '/user/queue/responses',
93
+ // 可订阅多个主题:设备状态轮询
94
+ const { send: sendDeviceStatus } = useStompChannel(stompInstance, {
95
+ subscribeTopic: "/user/device/status",
139
96
  sends: [
140
97
  {
141
- destination: '/app/device-status',
98
+ destination: "/app/device-status",
142
99
  interval: 2000,
143
- payload: () => ({ deviceId: '001' })
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
- // Handle all messages from the subscribed topic
153
- console.log('Message:', payload)
154
- }
155
- })
104
+ console.log("设备状态:", payload);
105
+ },
106
+ });
107
+ </script>
156
108
  ```
157
109
 
158
- ### Integration with Vue Component
110
+ ## API 文档
159
111
 
160
- Create your own provider component:
112
+ ### useStomp
161
113
 
162
- ```vue
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
- onUnmounted(() => {
187
- stomp.disconnect()
188
- })
189
-
190
- // Inject for child components
191
- provide('STOMP_INSTANCE', stomp)
192
- </script>
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
- Use in child components:
132
+ ### useStompChannel
196
133
 
197
- ```vue
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
- const topic2 = stomp.subscribe('/topic/alerts', (msg) => {
207
- console.log('Alert:', msg)
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
- onUnmounted(() => {
211
- stomp.unsubscribe(topic1)
212
- stomp.unsubscribe(topic2)
213
- })
214
- </script>
148
+ // 返回值
149
+ {
150
+ isConnected, // 连接状态
151
+ lastMessage, // 最后一条消息
152
+ send, // 手动发送消息
153
+ startAllSending, // 启动所有轮询
154
+ stopAllSending, // 停止所有轮询
155
+ unsubscribe, // 取消订阅
156
+ }
215
157
  ```
216
158
 
217
- ## Features
159
+ ## 特性
218
160
 
219
- - ✅ **Auto Reconnection** - Automatically reconnects on disconnection with configurable delay
220
- - ✅ **Subscription Recovery** - Automatically restores subscriptions after reconnection
221
- - ✅ **Multiple Subscriptions** - Subscribe to unlimited topics
222
- - ✅ **Polling Support** - Built-in interval-based message sending
223
- - ✅ **TypeScript** - Full TypeScript support with type definitions
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