推送模块

This commit is contained in:
2026-04-18 15:48:31 +08:00
parent e3f26ba1f7
commit 7d0f11fe43
19 changed files with 548 additions and 5 deletions

View File

@@ -0,0 +1,79 @@
/**
* 后台推送频道测试:加载官方 push.js 并订阅频道、监听文档约定事件
*/
const DOC_EVENTS = [
'period.tick',
'period.locked',
'period.opened',
'bet.accepted',
'wallet.changed',
'notice.popout',
'withdraw.review_required',
]
export async function loadPushJs(): Promise<void> {
if ((window as any).Push) {
return
}
await new Promise<void>((resolve, reject) => {
const script = document.createElement('script')
script.src = '/plugin/webman/push/push.js'
script.onload = () => resolve()
script.onerror = () => reject(new Error('load push.js failed'))
document.head.appendChild(script)
})
}
export type PushTestLogLine = { t: number; event: string; payload: string }
export function startPushChannelListener(options: {
url: string
app_key: string
channel: string
/** 私有频道需走 /plugin/webman/push/auth */
usePrivateAuth: boolean
onLog: (line: PushTestLogLine) => void
onConnected: (ok: boolean) => void
}): { disconnect: () => void } {
const PushCtor = (window as any).Push
const cfg: anyObj = { url: options.url, app_key: options.app_key }
if (options.usePrivateAuth) {
cfg.auth = '/plugin/webman/push/auth'
}
const client = new PushCtor(cfg)
const ch = client.subscribe(options.channel)
const pushLog = (event: string, data: unknown) => {
let payload = ''
try {
payload = typeof data === 'string' ? data : JSON.stringify(data)
} catch {
payload = String(data)
}
options.onLog({ t: Date.now(), event, payload })
}
ch.on('pusher:subscription_succeeded', () => {
options.onConnected(true)
pushLog('pusher:subscription_succeeded', {})
})
for (const ev of DOC_EVENTS) {
ch.on(ev, (data: unknown) => {
options.onConnected(true)
pushLog(ev, data)
})
}
return {
disconnect: () => {
try {
client.disconnect()
} catch {
/* ignore */
}
options.onConnected(false)
},
}
}