1.重构实时消息WebSocket连接
2.MySQL备份
This commit is contained in:
@@ -9,8 +9,6 @@ export default {
|
||||
'/': ['./frontend/${lang}/index.ts'],
|
||||
[adminBaseRoutePath + '/moduleStore']: ['./backend/${lang}/module.ts'],
|
||||
[adminBaseRoutePath + '/crud/crud']: ['./backend/${lang}/crud/log.ts', './backend/${lang}/crud/state.ts'],
|
||||
/** 推送测试三页:共享 test.push.* 文案(见 PushChannelTestPage.vue) */
|
||||
[adminBaseRoutePath + '/test/pushGamePeriod']: ['./backend/${lang}/test/push.ts'],
|
||||
[adminBaseRoutePath + '/test/pushOperationNotice']: ['./backend/${lang}/test/push.ts'],
|
||||
[adminBaseRoutePath + '/test/pushPrivateUser']: ['./backend/${lang}/test/push.ts'],
|
||||
/** 测试页:WebSocket 单入口(见 WebSocketTestPage.vue) */
|
||||
[adminBaseRoutePath + '/test/gameCurrentStatus']: ['./backend/${lang}/test/ws.ts'],
|
||||
}
|
||||
|
||||
@@ -15,8 +15,8 @@ export default {
|
||||
btn_draw: 'Schedule draw',
|
||||
calc_result_number: 'Calculated number',
|
||||
calc_estimated_loss: 'Estimated payout',
|
||||
push_connected: 'Push connected, realtime updates running',
|
||||
push_disconnected: 'Push disconnected, please check service status',
|
||||
push_connected: 'Realtime connection established',
|
||||
push_disconnected: 'Polling mode enabled (push removed)',
|
||||
candidate_title: 'Candidate payout estimates',
|
||||
number: 'Number',
|
||||
estimated_loss: 'Estimated payout',
|
||||
|
||||
3
web/src/lang/backend/en/test/gameCurrentStatus.ts
Normal file
3
web/src/lang/backend/en/test/gameCurrentStatus.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export default {
|
||||
tip: 'WebSocket connection test for status stream: listen to period.tick / period.opened events.',
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
export default {
|
||||
connected: 'Push connected (subscribed)',
|
||||
disconnected: 'Disconnected or idle',
|
||||
user_uuid: 'User uuid',
|
||||
user_uuid_placeholder: '10-char public id as in mobile profile',
|
||||
btn_connect: 'Connect & subscribe',
|
||||
btn_disconnect: 'Disconnect',
|
||||
btn_clear: 'Clear log',
|
||||
channel_label: 'Channel',
|
||||
log_title: 'Event log',
|
||||
log_empty: 'No messages yet. Ensure the push worker is running and the server publishes to this channel.',
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
export default {
|
||||
tip: 'Subscribe to public-game-period (global period channel) for period.tick / period.locked / period.opened / period.payout / jackpot.hit. The server must publish to this channel.',
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
export default {
|
||||
tip: 'Subscribe to public-operation-notice for broadcast notices such as notice.popout. The server must publish to this channel.',
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
export default {
|
||||
tip: 'Subscribe to private-user-{uuid}: enter the player uuid; auth uses /plugin/webman/push/auth like the mobile client. For bet.accepted, wallet.changed, etc.',
|
||||
}
|
||||
16
web/src/lang/backend/en/test/ws.ts
Normal file
16
web/src/lang/backend/en/test/ws.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
export default {
|
||||
ws_connected: 'WebSocket connected',
|
||||
ws_disconnected: 'WebSocket disconnected',
|
||||
config_api: 'Config API',
|
||||
ws_url: 'WebSocket URL',
|
||||
ws_url_placeholder: 'Input ws:// or wss:// URL',
|
||||
send_message: 'Send message',
|
||||
btn_load_config: 'Load config',
|
||||
btn_connect: 'Connect',
|
||||
btn_disconnect: 'Disconnect',
|
||||
btn_send: 'Send message',
|
||||
btn_clear: 'Clear log',
|
||||
log_title: 'WebSocket log',
|
||||
log_empty: 'No logs yet. Connect first and then send a message.',
|
||||
}
|
||||
|
||||
@@ -15,8 +15,8 @@ export default {
|
||||
btn_draw: '预约开奖',
|
||||
calc_result_number: '计算开奖号码',
|
||||
calc_estimated_loss: '计算预估赔付',
|
||||
push_connected: '推送服务已连接,页面数据实时更新中',
|
||||
push_disconnected: '推送服务连接中断,请检查服务是否启动',
|
||||
push_connected: '实时连接已建立',
|
||||
push_disconnected: '已切换为轮询模式(无推送)',
|
||||
candidate_title: '候选号码赔付预估',
|
||||
number: '号码',
|
||||
estimated_loss: '预估赔付',
|
||||
|
||||
3
web/src/lang/backend/zh-cn/test/gameCurrentStatus.ts
Normal file
3
web/src/lang/backend/zh-cn/test/gameCurrentStatus.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export default {
|
||||
tip: 'WebSocket 连接测试(状态流):按文档监听 period.tick / period.opened 等事件。',
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
export default {
|
||||
connected: '推送服务已连接(已订阅频道)',
|
||||
disconnected: '未连接或已断开',
|
||||
user_uuid: '用户 uuid',
|
||||
user_uuid_placeholder: '与移动端档案一致的 10 位对外标识',
|
||||
btn_connect: '连接并订阅',
|
||||
btn_disconnect: '断开',
|
||||
btn_clear: '清空日志',
|
||||
channel_label: '当前频道',
|
||||
log_title: '事件日志',
|
||||
log_empty: '暂无推送,请确认 push 进程已启动且服务端会向对应频道发消息。',
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
export default {
|
||||
tip: '订阅文档中的「全局对局频道」public-game-period,用于验证 period.tick / period.locked / period.opened / period.payout / jackpot.hit 等公共事件(需服务端向该频道推送)。',
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
export default {
|
||||
tip: '订阅「公告广播频道」public-operation-notice:全站公告 notice.popout 与本游戏 jackpot.hit(连胜大奖广播)均会发往此频道;需服务端推送。',
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
export default {
|
||||
tip: '订阅「用户私有频道」private-user-{uuid}:请输入玩家 uuid,连接后将走 /plugin/webman/push/auth 鉴权(与移动端一致)。用于验证 bet.accepted、wallet.changed 等私有事件。',
|
||||
}
|
||||
16
web/src/lang/backend/zh-cn/test/ws.ts
Normal file
16
web/src/lang/backend/zh-cn/test/ws.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
export default {
|
||||
ws_connected: 'WebSocket 已连接',
|
||||
ws_disconnected: 'WebSocket 未连接',
|
||||
config_api: '配置接口',
|
||||
ws_url: 'WebSocket 地址',
|
||||
ws_url_placeholder: '请输入 ws:// 或 wss:// 地址',
|
||||
send_message: '发送消息',
|
||||
btn_load_config: '加载测试配置',
|
||||
btn_connect: '连接',
|
||||
btn_disconnect: '断开',
|
||||
btn_send: '发送消息',
|
||||
btn_clear: '清空日志',
|
||||
log_title: 'WebSocket 日志',
|
||||
log_empty: '暂无日志,请先连接后发送消息。',
|
||||
}
|
||||
|
||||
@@ -35,33 +35,6 @@ export const getUrl = (): string => {
|
||||
return value == 'getCurrentDomain' ? window.location.protocol + '//' + window.location.host : value
|
||||
}
|
||||
|
||||
/**
|
||||
* Webman Push 在 API 上的 HTTP 根地址(私有频道 POST /plugin/webman/push/auth 等)。
|
||||
* 与 VITE_AXIOS_BASE_URL 一致;开发环境为空时用当前页面源,配合 Vite 对 /plugin 的代理。
|
||||
*/
|
||||
export function getPushHttpBase(): string {
|
||||
const api = getUrl()
|
||||
if (api) {
|
||||
return api.replace(/\/$/, '')
|
||||
}
|
||||
return window.location.protocol + '//' + window.location.host
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载 push.js 的绝对 URL:文件放在 web/public/plugin/webman/push/,随 dist 与后台同源发布,
|
||||
* 不依赖 API 站点是否暴露 /plugin/webman/push/push.js。
|
||||
*/
|
||||
export function getPushScriptUrl(): string {
|
||||
let base = import.meta.env.BASE_URL
|
||||
if (typeof base !== 'string' || base === '') {
|
||||
base = '/'
|
||||
}
|
||||
if (!base.endsWith('/')) {
|
||||
base = base + '/'
|
||||
}
|
||||
return window.location.origin + base + 'plugin/webman/push/push.js'
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据运行环境获取基础请求URL的端口
|
||||
*/
|
||||
|
||||
@@ -1,84 +0,0 @@
|
||||
/**
|
||||
* 后台推送频道测试:加载官方 push.js 并订阅频道、监听文档约定事件
|
||||
*/
|
||||
|
||||
import { getPushHttpBase, getPushScriptUrl } from '/@/utils/axios'
|
||||
|
||||
const DOC_EVENTS = [
|
||||
'period.tick',
|
||||
'period.locked',
|
||||
'period.opened',
|
||||
'period.payout',
|
||||
'jackpot.hit',
|
||||
'bet.accepted',
|
||||
'bet.settled',
|
||||
'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 = getPushScriptUrl()
|
||||
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 = `${getPushHttpBase()}/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)
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
<template>
|
||||
<div class="default-main">
|
||||
<el-alert type="info" :title="t('game.live.tip')" show-icon class="mb-12" />
|
||||
<el-alert :type="pushConnected ? 'success' : 'error'" :title="pushConnected ? t('game.live.push_connected') : t('game.live.push_disconnected')" show-icon class="mb-12" />
|
||||
<el-alert type="info" :title="t('game.live.push_disconnected')" show-icon class="mb-12" />
|
||||
<el-alert
|
||||
v-if="snapshot.runtime_enabled === false && !snapshot.maintenance_ui"
|
||||
type="warning"
|
||||
@@ -190,7 +190,7 @@
|
||||
import { computed, onMounted, onUnmounted, reactive, ref } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import createAxios, { getPushScriptUrl } from '/@/utils/axios'
|
||||
import createAxios from '/@/utils/axios'
|
||||
|
||||
interface Snapshot {
|
||||
record: anyObj | null
|
||||
@@ -219,7 +219,6 @@ interface Snapshot {
|
||||
|
||||
const { t } = useI18n()
|
||||
const loading = ref(false)
|
||||
const pushConnected = ref(false)
|
||||
const snapshot = reactive<Snapshot>({
|
||||
record: null,
|
||||
bets: [],
|
||||
@@ -256,10 +255,7 @@ const serverSkewSeconds = ref(0)
|
||||
const clockTick = ref(0)
|
||||
let clockTimer: number | null = null
|
||||
|
||||
let pushClient: any = null
|
||||
let pushChannel: any = null
|
||||
let pollTimer: number | null = null
|
||||
let pushWatchdogTimer: number | null = null
|
||||
|
||||
function formatPicks(v: unknown): string {
|
||||
if (Array.isArray(v)) return JSON.stringify(v)
|
||||
@@ -417,59 +413,6 @@ async function submitVoidPeriod(): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
async function initPush() {
|
||||
const cfgRes = await createAxios({ url: '/admin/game.Live/pushConfig', method: 'get', showCodeMessage: false })
|
||||
if (cfgRes.code !== 1 || !cfgRes.data) {
|
||||
pushConnected.value = false
|
||||
return
|
||||
}
|
||||
const { url, app_key, channel, event } = cfgRes.data
|
||||
|
||||
try {
|
||||
await loadPushJs()
|
||||
} catch {
|
||||
pushConnected.value = false
|
||||
startPolling()
|
||||
return
|
||||
}
|
||||
const PushCtor = (window as any).Push
|
||||
if (!PushCtor) {
|
||||
pushConnected.value = false
|
||||
startPolling()
|
||||
return
|
||||
}
|
||||
try {
|
||||
pushClient = new PushCtor({ url, app_key })
|
||||
pushChannel = pushClient.subscribe(channel)
|
||||
pushConnected.value = false
|
||||
pushChannel.on('pusher:subscription_succeeded', () => {
|
||||
pushConnected.value = true
|
||||
})
|
||||
startPushWatchdog()
|
||||
stopPolling()
|
||||
pushChannel.on(event, (payload: anyObj) => {
|
||||
pushConnected.value = true
|
||||
mergeLiveSnapshot(payload)
|
||||
})
|
||||
} catch {
|
||||
pushConnected.value = false
|
||||
startPolling()
|
||||
}
|
||||
}
|
||||
|
||||
async function loadPushJs() {
|
||||
if ((window as any).Push) {
|
||||
return
|
||||
}
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const script = document.createElement('script')
|
||||
script.src = getPushScriptUrl()
|
||||
script.onload = () => resolve()
|
||||
script.onerror = () => reject(new Error('load push.js failed'))
|
||||
document.head.appendChild(script)
|
||||
})
|
||||
}
|
||||
|
||||
async function onCalculate() {
|
||||
if (!snapshot.record) return
|
||||
calcLoading.value = true
|
||||
@@ -528,24 +471,11 @@ onMounted(async () => {
|
||||
clockTick.value++
|
||||
}, 1000)
|
||||
await loadSnapshot()
|
||||
try {
|
||||
await initPush()
|
||||
} catch {
|
||||
pushConnected.value = false
|
||||
startPolling()
|
||||
}
|
||||
startPolling()
|
||||
})
|
||||
|
||||
onUnmounted(() => {
|
||||
try {
|
||||
if (pushClient && typeof pushClient.disconnect === 'function') {
|
||||
pushClient.disconnect()
|
||||
}
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
stopPolling()
|
||||
stopPushWatchdog()
|
||||
if (clockTimer !== null) {
|
||||
window.clearInterval(clockTimer)
|
||||
clockTimer = null
|
||||
@@ -568,30 +498,6 @@ function stopPolling() {
|
||||
}
|
||||
}
|
||||
|
||||
function startPushWatchdog() {
|
||||
if (pushWatchdogTimer !== null) {
|
||||
return
|
||||
}
|
||||
pushWatchdogTimer = window.setInterval(() => {
|
||||
if (!pushClient || !pushClient.connection) {
|
||||
pushConnected.value = false
|
||||
return
|
||||
}
|
||||
const state = String(pushClient.connection.state || '')
|
||||
// push.js:onOpen 后长期为 connecting,必须收到 pusher:connection_established 才变 connected。
|
||||
// 把 connecting 当「已连接」会误报绿条(Nginx 只完成握手、帧未透传时 DevTools 消息列表为空)。
|
||||
if (state === 'disconnected') {
|
||||
pushConnected.value = false
|
||||
}
|
||||
}, 1000)
|
||||
}
|
||||
|
||||
function stopPushWatchdog() {
|
||||
if (pushWatchdogTimer !== null) {
|
||||
window.clearInterval(pushWatchdogTimer)
|
||||
pushWatchdogTimer = null
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped lang="scss">
|
||||
|
||||
@@ -1,142 +0,0 @@
|
||||
<template>
|
||||
<div class="default-main">
|
||||
<el-alert type="info" :title="tip" show-icon class="mb-12" />
|
||||
<el-alert :type="connected ? 'success' : 'warning'" :title="connected ? t('test.push.connected') : t('test.push.disconnected')" show-icon class="mb-12" />
|
||||
|
||||
<el-card shadow="never" class="mb-12">
|
||||
<el-form :inline="true" @submit.prevent>
|
||||
<el-form-item v-if="useUuid" :label="t('test.push.user_uuid')">
|
||||
<el-input v-model="userUuid" :placeholder="t('test.push.user_uuid_placeholder')" clearable style="width: 280px" />
|
||||
</el-form-item>
|
||||
<el-form-item>
|
||||
<el-button type="primary" :loading="loading" @click="connect">{{ t('test.push.btn_connect') }}</el-button>
|
||||
<el-button :disabled="!session" @click="disconnect">{{ t('test.push.btn_disconnect') }}</el-button>
|
||||
<el-button @click="clearLogs">{{ t('test.push.btn_clear') }}</el-button>
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
<div class="text-muted mb-8">{{ t('test.push.channel_label') }}: {{ channelName || '-' }}</div>
|
||||
</el-card>
|
||||
|
||||
<el-card shadow="never">
|
||||
<template #header>{{ t('test.push.log_title') }}</template>
|
||||
<el-scrollbar max-height="480">
|
||||
<pre class="push-log">{{ logText }}</pre>
|
||||
</el-scrollbar>
|
||||
</el-card>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onUnmounted, ref } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import createAxios from '/@/utils/axios'
|
||||
import { loadPushJs, startPushChannelListener, type PushTestLogLine } from '/@/utils/backend/pushChannelTest'
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
/** 如 /admin/test.PushGamePeriod/pushConfig */
|
||||
configApi: string
|
||||
useUuid?: boolean
|
||||
tip: string
|
||||
}>(),
|
||||
{
|
||||
useUuid: false,
|
||||
}
|
||||
)
|
||||
|
||||
const { t } = useI18n()
|
||||
const loading = ref(false)
|
||||
const connected = ref(false)
|
||||
const channelName = ref('')
|
||||
const userUuid = ref('')
|
||||
const logs = ref<PushTestLogLine[]>([])
|
||||
const session = ref<{ disconnect: () => void } | null>(null)
|
||||
|
||||
const logText = computed(() => {
|
||||
if (!logs.value.length) return t('test.push.log_empty')
|
||||
return logs.value
|
||||
.map((row) => {
|
||||
const time = new Date(row.t).toLocaleString()
|
||||
return `[${time}] ${row.event}\n${row.payload}`
|
||||
})
|
||||
.join('\n\n')
|
||||
})
|
||||
|
||||
function appendLog(line: PushTestLogLine) {
|
||||
logs.value = [line, ...logs.value].slice(0, 200)
|
||||
}
|
||||
|
||||
function clearLogs() {
|
||||
logs.value = []
|
||||
}
|
||||
|
||||
async function connect() {
|
||||
if (props.useUuid && !userUuid.value.trim()) {
|
||||
return
|
||||
}
|
||||
loading.value = true
|
||||
disconnect()
|
||||
try {
|
||||
await loadPushJs()
|
||||
const params: anyObj = {}
|
||||
if (props.useUuid) {
|
||||
params.uuid = userUuid.value.trim()
|
||||
}
|
||||
const res = await createAxios({
|
||||
url: props.configApi,
|
||||
method: 'get',
|
||||
params,
|
||||
showCodeMessage: true,
|
||||
})
|
||||
if (res.code !== 1 || !res.data) {
|
||||
connected.value = false
|
||||
return
|
||||
}
|
||||
const { url, app_key, channel } = res.data
|
||||
channelName.value = typeof channel === 'string' ? channel : ''
|
||||
try {
|
||||
const handle = startPushChannelListener({
|
||||
url,
|
||||
app_key,
|
||||
channel: channelName.value,
|
||||
usePrivateAuth: !!props.useUuid,
|
||||
onLog: appendLog,
|
||||
onConnected: (ok) => {
|
||||
connected.value = ok
|
||||
},
|
||||
})
|
||||
session.value = handle
|
||||
} catch {
|
||||
connected.value = false
|
||||
}
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function disconnect() {
|
||||
if (session.value) {
|
||||
session.value.disconnect()
|
||||
session.value = null
|
||||
}
|
||||
connected.value = false
|
||||
}
|
||||
|
||||
onUnmounted(() => {
|
||||
disconnect()
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.push-log {
|
||||
margin: 0;
|
||||
font-size: 12px;
|
||||
line-height: 1.5;
|
||||
white-space: pre-wrap;
|
||||
word-break: break-all;
|
||||
}
|
||||
.text-muted {
|
||||
color: var(--el-text-color-secondary);
|
||||
font-size: 13px;
|
||||
}
|
||||
</style>
|
||||
220
web/src/views/backend/test/components/WebSocketTestPage.vue
Normal file
220
web/src/views/backend/test/components/WebSocketTestPage.vue
Normal file
@@ -0,0 +1,220 @@
|
||||
<template>
|
||||
<div class="default-main">
|
||||
<el-alert type="info" :title="tip" show-icon class="mb-12" />
|
||||
<el-alert :type="connected ? 'success' : 'warning'" :title="connected ? t('test.ws.ws_connected') : t('test.ws.ws_disconnected')" show-icon class="mb-12" />
|
||||
|
||||
<el-card shadow="never" class="mb-12">
|
||||
<el-form @submit.prevent>
|
||||
<el-form-item :label="t('test.ws.config_api')">
|
||||
<el-input :model-value="configApi" readonly />
|
||||
</el-form-item>
|
||||
<el-form-item :label="t('test.ws.ws_url')">
|
||||
<el-input v-model="wsUrl" :placeholder="t('test.ws.ws_url_placeholder')" />
|
||||
</el-form-item>
|
||||
<el-form-item :label="t('test.ws.send_message')">
|
||||
<el-input v-model="sendMessage" type="textarea" :rows="4" />
|
||||
</el-form-item>
|
||||
<el-form-item>
|
||||
<el-button type="primary" :loading="loading" @click="loadConfig">{{ t('test.ws.btn_load_config') }}</el-button>
|
||||
<el-button type="success" :disabled="!ready || connected" @click="connectWs">{{ t('test.ws.btn_connect') }}</el-button>
|
||||
<el-button :disabled="!connected" @click="disconnectWs">{{ t('test.ws.btn_disconnect') }}</el-button>
|
||||
<el-button :loading="sending" :disabled="!connected" @click="sendWs">{{ t('test.ws.btn_send') }}</el-button>
|
||||
<el-button @click="clearLogs">{{ t('test.ws.btn_clear') }}</el-button>
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
<div class="text-muted mb-8">{{ connectTip || '-' }}</div>
|
||||
</el-card>
|
||||
|
||||
<el-card shadow="never">
|
||||
<template #header>{{ t('test.ws.log_title') }}</template>
|
||||
<el-scrollbar max-height="480">
|
||||
<pre class="ws-log">{{ logText }}</pre>
|
||||
</el-scrollbar>
|
||||
</el-card>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onUnmounted, ref } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import createAxios from '/@/utils/axios'
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
/** 如 /admin/test.GameCurrentStatus/wsConfig */
|
||||
configApi: string
|
||||
tip: string
|
||||
}>(),
|
||||
{}
|
||||
)
|
||||
|
||||
const { t } = useI18n()
|
||||
const loading = ref(false)
|
||||
const sending = ref(false)
|
||||
const ready = ref(false)
|
||||
const connected = ref(false)
|
||||
const wsUrl = ref('')
|
||||
const connectTip = ref('')
|
||||
const sendMessage = ref('')
|
||||
/** 连接成功后自动订阅(由 wsConfig.subscribe_topics 下发) */
|
||||
const subscribeTopics = ref<string[]>([])
|
||||
const ws = ref<WebSocket | null>(null)
|
||||
const logs = ref<Array<{ t: number; event: string; payload: string }>>([])
|
||||
|
||||
const defaultSubscribeTopics = [
|
||||
'period.tick',
|
||||
'period.opened',
|
||||
'period.locked',
|
||||
'period.payout',
|
||||
'bet.accepted',
|
||||
'wallet.changed',
|
||||
'auto.spin.progress',
|
||||
] as const
|
||||
|
||||
const logText = computed(() => {
|
||||
if (!logs.value.length) return t('test.ws.log_empty')
|
||||
return logs.value
|
||||
.map((row) => {
|
||||
const time = new Date(row.t).toLocaleString()
|
||||
return `[${time}] ${row.event}\n${row.payload}`
|
||||
})
|
||||
.join('\n\n')
|
||||
})
|
||||
|
||||
function appendLog(line: { t: number; event: string; payload: string }) {
|
||||
logs.value = [line, ...logs.value].slice(0, 200)
|
||||
}
|
||||
|
||||
function clearLogs() {
|
||||
logs.value = []
|
||||
}
|
||||
|
||||
async function loadConfig() {
|
||||
loading.value = true
|
||||
try {
|
||||
const res = await createAxios({
|
||||
url: props.configApi,
|
||||
method: 'get',
|
||||
showCodeMessage: true,
|
||||
})
|
||||
if (res.code !== 1 || !res.data) {
|
||||
ready.value = false
|
||||
return
|
||||
}
|
||||
wsUrl.value = String(res.data.ws_url || '')
|
||||
connectTip.value = String(res.data.connect_tip || '')
|
||||
const rawTopics = res.data.subscribe_topics
|
||||
if (Array.isArray(rawTopics)) {
|
||||
subscribeTopics.value = rawTopics.filter((x: unknown): x is string => typeof x === 'string' && x.trim() !== '')
|
||||
} else {
|
||||
subscribeTopics.value = [...defaultSubscribeTopics]
|
||||
}
|
||||
const firstSample = Array.isArray(res.data.sample_messages) && res.data.sample_messages.length ? String(res.data.sample_messages[0]) : ''
|
||||
sendMessage.value = firstSample
|
||||
ready.value = wsUrl.value !== ''
|
||||
appendLog({
|
||||
t: Date.now(),
|
||||
event: 'config.loaded',
|
||||
payload: JSON.stringify(res.data, null, 2),
|
||||
})
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function connectWs() {
|
||||
if (!ready.value || !wsUrl.value) {
|
||||
return
|
||||
}
|
||||
disconnectWs()
|
||||
const socket = new WebSocket(wsUrl.value)
|
||||
ws.value = socket
|
||||
socket.onopen = () => {
|
||||
connected.value = true
|
||||
appendLog({
|
||||
t: Date.now(),
|
||||
event: 'ws.open',
|
||||
payload: wsUrl.value,
|
||||
})
|
||||
const topics = subscribeTopics.value.length > 0 ? subscribeTopics.value : [...defaultSubscribeTopics]
|
||||
const subscribePayload = JSON.stringify({ action: 'subscribe', topics })
|
||||
socket.send(subscribePayload)
|
||||
appendLog({
|
||||
t: Date.now(),
|
||||
event: 'ws.auto_subscribe',
|
||||
payload: subscribePayload,
|
||||
})
|
||||
}
|
||||
socket.onmessage = (event) => {
|
||||
appendLog({
|
||||
t: Date.now(),
|
||||
event: 'ws.message',
|
||||
payload: typeof event.data === 'string' ? event.data : JSON.stringify(event.data),
|
||||
})
|
||||
}
|
||||
socket.onerror = () => {
|
||||
appendLog({
|
||||
t: Date.now(),
|
||||
event: 'ws.error',
|
||||
payload: 'WebSocket error',
|
||||
})
|
||||
}
|
||||
socket.onclose = () => {
|
||||
connected.value = false
|
||||
ws.value = null
|
||||
appendLog({
|
||||
t: Date.now(),
|
||||
event: 'ws.close',
|
||||
payload: wsUrl.value,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function disconnectWs() {
|
||||
if (ws.value) {
|
||||
ws.value.close()
|
||||
ws.value = null
|
||||
}
|
||||
connected.value = false
|
||||
}
|
||||
|
||||
function sendWs() {
|
||||
if (!ws.value || !connected.value) {
|
||||
return
|
||||
}
|
||||
const payload = sendMessage.value.trim()
|
||||
if (!payload) {
|
||||
return
|
||||
}
|
||||
sending.value = true
|
||||
try {
|
||||
ws.value.send(payload)
|
||||
appendLog({
|
||||
t: Date.now(),
|
||||
event: 'ws.send',
|
||||
payload,
|
||||
})
|
||||
} finally {
|
||||
sending.value = false
|
||||
}
|
||||
}
|
||||
|
||||
onUnmounted(() => {
|
||||
disconnectWs()
|
||||
clearLogs()
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.ws-log {
|
||||
margin: 0;
|
||||
font-size: 12px;
|
||||
line-height: 1.5;
|
||||
white-space: pre-wrap;
|
||||
word-break: break-all;
|
||||
}
|
||||
.text-muted {
|
||||
color: var(--el-text-color-secondary);
|
||||
font-size: 13px;
|
||||
}
|
||||
</style>
|
||||
11
web/src/views/backend/test/gameCurrentStatus/index.vue
Normal file
11
web/src/views/backend/test/gameCurrentStatus/index.vue
Normal file
@@ -0,0 +1,11 @@
|
||||
<template>
|
||||
<WebSocketTestPage config-api="/admin/test.GameCurrentStatus/wsConfig" :tip="t('test.gameCurrentStatus.tip')" />
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import WebSocketTestPage from '../components/WebSocketTestPage.vue'
|
||||
|
||||
const { t } = useI18n()
|
||||
</script>
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
<template>
|
||||
<PushChannelTestPage config-api="/admin/test.PushGamePeriod/pushConfig" :tip="t('test.pushGamePeriod.tip')" />
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import PushChannelTestPage from '../components/PushChannelTestPage.vue'
|
||||
|
||||
const { t } = useI18n()
|
||||
</script>
|
||||
@@ -1,10 +0,0 @@
|
||||
<template>
|
||||
<PushChannelTestPage config-api="/admin/test.PushOperationNotice/pushConfig" :tip="t('test.pushOperationNotice.tip')" />
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import PushChannelTestPage from '../components/PushChannelTestPage.vue'
|
||||
|
||||
const { t } = useI18n()
|
||||
</script>
|
||||
@@ -1,14 +0,0 @@
|
||||
<template>
|
||||
<PushChannelTestPage
|
||||
config-api="/admin/test.PushPrivateUser/pushConfig"
|
||||
use-uuid
|
||||
:tip="t('test.pushPrivateUser.tip')"
|
||||
/>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import PushChannelTestPage from '../components/PushChannelTestPage.vue'
|
||||
|
||||
const { t } = useI18n()
|
||||
</script>
|
||||
Reference in New Issue
Block a user