fix(ws): assign websocket after onConnected succeeds (#7639)

* feat(layout): add logging of MihomoWebSocket instances in dev mode

* refactor(hooks): defer ws assignment in use-mihomo-ws-subscription

* chore: update tauri-plugin-mihomo-api dependency source to use github protocol

* docs: update changelog with websocket connection fix
This commit is contained in:
oomeow
2026-07-30 15:33:45 +00:00
committed by GitHub
parent 10dbe60671
commit 4b3bc54b2d
6 changed files with 42 additions and 24 deletions
+8 -10
View File
@@ -97,17 +97,11 @@ const createSharedSubscriptionEntry = (
return
}
entry.ws = ws
syncSharedWsRefs(entry)
clearReconnectTimer()
const owner = pickActiveOwner(entry)
if (owner?.onConnected) {
await owner.onConnected(ws)
if (entry.closed) {
await ws.close()
return
}
await owner?.onConnected?.(ws)
if (entry.closed) {
await ws.close()
return
}
ws.addListener((msg: Message) => {
@@ -117,6 +111,10 @@ const createSharedSubscriptionEntry = (
activeOwner.handleMessage(msg.data)
})
entry.ws = ws
syncSharedWsRefs(entry)
clearReconnectTimer()
} catch (ignoreError) {
if (!entry.closed && !entry.ws) {
clearReconnectTimer()
+19
View File
@@ -27,6 +27,7 @@ import type { CSSProperties } from 'react'
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { Outlet, useNavigate } from 'react-router'
import { MihomoWebSocket } from 'tauri-plugin-mihomo-api'
import iconDark from '@/assets/image/icon_dark.svg?react'
import iconLight from '@/assets/image/icon_light.svg?react'
@@ -122,6 +123,24 @@ const Layout = () => {
const navigate = useNavigate()
const themeReady = useMemo(() => Boolean(theme), [theme])
// 开发环境下检测 MihomoWebSocket 的所有实例
useEffect(() => {
let id: number
if (import.meta.env.DEV) {
id = setInterval(() => {
MihomoWebSocket.get_all_instances().then((list) => {
console.log('Mihomo ws instances', list)
})
}, 1000)
}
return () => {
if (id) {
clearInterval(id)
}
}
}, [])
const [menuUnlocked, setMenuUnlocked] = useState(false)
const [menuContextPosition, setMenuContextPosition] =
useState<MenuContextPosition | null>(null)