更新记录
1.0.0(2026-07-31) 下载此版本
- 首次发布:STOMP over WebSocket 消息总线,App/H5/小程序全端兼容
- 内置 EventBus,支持全局监听与页面级自动清理(useMQBus)
- core / eventBus / config 三个模块可独立使用
平台兼容性
uni-app(3.7.11)
| Vue2 | Vue3 | Chrome | Safari | app-vue | app-nvue | Android | iOS | 鸿蒙 |
|---|---|---|---|---|---|---|---|---|
| - | √ | - | - | √ | - | √ | - | √ |
| 微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 小红书小程序 | 快应用-华为 | 快应用-联盟 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| √ | √ | √ | √ | √ | √ | - | √ | √ | √ | - | - |
yolo-mq
STOMP over WebSocket 消息总线,uni-app 全端兼容(App / H5 / 小程序)。
文件结构
uni_modules/yolo-mq/
├── index.js # 组合入口(MQ + EventBus + Config)
├── core.js # MQ 连接核心(可单独用)
├── eventBus.js # 独立 EventBus(可单独用)
├── config.js # 订阅配置 SUBS(可单独用)
└── libs/ # STOMP 底层实现
全局配置
config.js 中维护所有订阅的 topic、event、auto(示例 topic 为占位符,接入时替换为实际 MQ 路径):
// uni_modules/yolo-mq/config.js
const SUBS = {
// -- 广播(所有客户端都收到) --
ALARM_PUSH: {
topic: '/topic/ALARM.PUSH',
event: 'alarm:push',
auto: true,
},
LIVE_STATUS: {
topic: '/topic/LIVE.STATUS',
event: 'live:status',
auto: true,
},
// -- 点对点(一个消息一个消费者) --
// PRIVATE_MSG: {
// topic: '/queue/PRIVATE.MESSAGE',
// event: 'private:msg',
// auto: false,
// },
}
| 字段 | 类型 | 说明 |
|---|---|---|
topic |
String | MQ 路径,/topic/ 广播(所有客户端都收到),/queue/ 点对点(一个消息一个消费者) |
event |
String | EventBus 事件名 |
auto |
Boolean | true → register() 自动订阅;false → 手动 mq.subscribe() |
运行时配置
mq.config({
debug: true, // 日志开关,默认 true
heartbeat: 20000, // 心跳间隔(ms)
onConnected: () => { useConfigStore().setMQConnected(true) },
onDisconnected: () => { useConfigStore().setMQConnected(false) },
})
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
debug |
Boolean | true |
打印 info/warn 日志,关闭后只打印 error |
heartbeat |
Number | 20000 |
STOMP 心跳间隔 (ms) |
onConnected |
Function | — | 连接成功后回调 |
onDisconnected |
Function | — | 断开连接后回调 |
组合使用
import mq from '@/uni_modules/yolo-mq'
// 配置
mq.config({
debug: true,
onConnected: () => useConfigStore().setMQConnected(true),
onDisconnected: () => useConfigStore().setMQConnected(false),
})
// 连接(从系统配置接口获取 alarmUrl / alarmUser / alarmPassWord)
mq.connect('ws://host:61614', 'user', 'pass')
// 批量注册 config.js 中 auto=true 的订阅
import { SUBS } from '@/uni_modules/yolo-mq/config'
mq.register(SUBS)
// 或不依赖 config.js,直接传入
mq.register({
ALARM_PUSH: { topic: '/queue/ALARM', event: 'alarm:push', auto: true },
})
// 手动订阅(auto=false 的,或动态订阅)
mq.subscribe('/queue/SOME.PATH', 'custom:event')
完整 API
mq.connect(url, login, passcode)
| 参数 | 默认值 | 说明 |
|---|---|---|
url |
— | WebSocket 地址 |
login |
'guest' |
STOMP 账号 |
passcode |
'guest' |
STOMP 密码 |
mq.disconnect()
断开连接,清空所有订阅和全局监听。
mq.getStatus()
返回 true / false。
mq.subscribe(topic, eventName, auto)
| 参数 | 默认值 | 说明 |
|---|---|---|
topic |
— | MQ 路径 |
eventName |
— | EventBus 事件名 |
auto |
false |
是否在 register() 中自动注册 |
mq.register(subs)
| 参数 | 类型 | 说明 |
|---|---|---|
subs |
Object | 订阅配置 { KEY: { topic, event, auto } } |
批量注册订阅,auto=true 的项自动订阅:
import { SUBS } from '@/uni_modules/yolo-mq/config'
mq.register(SUBS)
// 或直接传入
mq.register({
ALARM_PUSH: { topic: '/queue/ALARM', event: 'alarm:push', auto: true },
})
mq.parse(fn)
自定义消息解构:
mq.parse((event, body) => {
if (event === 'alarm:push') {
return { id: body.alarmId, title: body.alarmTitle }
}
return body
})
mq.on(eventName, callback) / mq.off(eventName, [callback])
EventBus 监听,全局级不自动清理。
mq.emit(eventName, data)
手动触发 EventBus 事件。
全局级监听
在 App.vue 中挂载,登出时自动清理:
// App.vue
import mq from '@/uni_modules/yolo-mq'
uni.$mq = mq // 在所有页面无需 import 即可使用
// 任意全局模块
uni.$mq.on('alarm:push', (data) => {
uni.showToast({ title: `新告警: ${data.title}` })
})
uni.$mq.on('live:status', (data) => {
// 处理实时状态
})
注意:全局监听在登出时 disconnect() 自动清空,无需手动 off。
页面级监听
使用 useMQBus() composable,页面销毁自动取消监听,无需写 off:
import { useMQBus } from '@/uni_modules/yolo-mq/eventBus'
const { on, off } = useMQBus()
on('alarm:push', (data) => {
console.log(data) // 页面销毁时自动清理
})
项目集成
// utils/system/systemCache.js — 登录后自动连接
import mq from '@/uni_modules/yolo-mq'
export async function initCache() {
const res = await listConfig({ pageNum: 1, pageSize: 100 })
const cache = {}
res.rows.forEach(i => cache[i.configKey] = i)
const url = cache['alarmUrl']?.configValue || config.wsUrl
const user = cache['alarmUser']?.configValue || config.wsUsername
const password = cache['alarmPassWord']?.configValue || config.wsPassword
mq.config({
onConnected: () => useConfigStore().setMQConnected(true),
onDisconnected: () => useConfigStore().setMQConnected(false),
})
mq.register(SUBS)
mq.connect(url, user, password)
}
// store/modules/user.js — 登出时断开
import mq from '@/uni_modules/yolo-mq'
const logOutAction = () => {
logout(token).then(() => {
// ... 清理 storage ...
mq.disconnect()
})
}
单独使用
三个模块可独立引用,互不依赖:
只用 MQ 连接(core.js)
import mqCore from '@/uni_modules/yolo-mq/core'
mqCore.config({ debug: true })
mqCore.connect('ws://host:61614', 'user', 'pass')
mqCore.subscribe('/queue/PATH', (body) => {
console.log('raw message:', body) // 不经 EventBus
})
mqCore.disconnect()
API: config, connect, disconnect, getStatus, subscribe, unsubscribe, parse
只用 EventBus(eventBus.js)
import bus from '@/uni_modules/yolo-mq/eventBus'
bus.on('custom', (data) => { })
bus.emit('custom', { hello: 'world' })
bus.off('custom')
bus.clear() // 清空所有
API: on, off, emit, clear, useMQBus
只用配置(config.js)
import { SUBS } from '@/uni_modules/yolo-mq/config'
console.log(SUBS.ALARM_PUSH.topic) // '/queue/...'
连接流程
登录成功
→ listConfig() 获取 alarmUrl / alarmUser / alarmPassWord
→ mq.config({ onConnected, onDisconnected })
→ mq.register(SUBS) 注册 config.js auto=true 的订阅
→ mq.connect(url, user, pwd)
→ UniWebSocket(url, ['stomp']) → Stomp.over(ws).connect()
→ 连接成功 → _connected=true → onConnected → 图标变绿
→ 收到消息 → parse() → emit(event, data) → on() 回调
登出
→ mq.disconnect() → clear events + subscriptions → 断开 WS → 图标变红
导航栏状态图标
useConfigStore().mqConnected:
| 颜色 | 状态 |
|---|---|
🟢 #27CA93 |
MQ 已连接 |
🔴 #F5502C |
MQ 未连接 |
注意事项
config()必须在connect()前调用register()在connect()之前调用,连接成功后自动恢复订阅- debug 默认
true,生产建议mq.config({ debug: false }) /topic/广播所有客户端,/queue/点对点一个消费者- 全局监听登出时自动清空,页面级
useMQBus()卸载时自动清空

收藏人数:
下载插件并导入HBuilderX
下载插件ZIP
赞赏(0)
下载 0
赞赏 0
下载 12472858
赞赏 1936
赞赏
京公网安备:11010802035340号