@@ -16,32 +16,67 @@ export interface UseWebSocketOptions {
1616 immediate ?: boolean
1717}
1818
19- export function useWebSocketService ( channel : MaybeRefOrGetter < string | null > , options : UseWebSocketOptions = { } ) {
19+ const resolveChannelValue = ( channel : MaybeRefOrGetter < string | null > ) : string | null => {
20+ if ( typeof channel === 'function' ) return channel ( )
21+ if ( isRef ( channel ) ) return channel . value
22+ return channel
23+ }
24+
25+ const resolveWsBaseUrl = ( ) : string | undefined => {
2026 const config = useRuntimeConfig ( )
21- const baseUrl = config . public . wsURL as string | undefined
27+ const explicit = config . public . wsURL as string | undefined
28+ if ( explicit ?. trim ( ) ) {
29+ return explicit . replace ( / \/ + $ / , '' )
30+ }
31+
32+ const baseUrl = config . public . baseUrl as string | undefined
33+ if ( ! baseUrl ?. trim ( ) ) return undefined
34+
35+ try {
36+ const url = new URL ( baseUrl )
37+ url . protocol = url . protocol === 'https:' ? 'wss:' : 'ws:'
38+ url . pathname = '/ws'
39+ url . search = ''
40+ url . hash = ''
41+ return `${ url . origin } /ws`
42+ } catch {
43+ return undefined
44+ }
45+ }
46+
47+ export const useWebSocketService = (
48+ channel : MaybeRefOrGetter < string | null > ,
49+ options : UseWebSocketOptions = { } ,
50+ ) => {
51+ const wsBaseUrl = resolveWsBaseUrl ( )
2252
2353 const wsUrl = computed ( ( ) => {
24- let channelValue : string | null
25- if ( typeof channel === 'function' ) {
26- channelValue = channel ( )
27- } else if ( isRef ( channel ) ) {
28- channelValue = channel . value
29- } else {
30- channelValue = channel
31- }
32- if ( ! baseUrl || ! channelValue ) return ''
33- return `${ baseUrl } /${ channelValue } `
54+ const channelValue = resolveChannelValue ( channel )
55+ if ( ! wsBaseUrl || ! channelValue ) return undefined
56+ return `${ wsBaseUrl } /${ channelValue . replace ( / ^ \/ + / , '' ) } `
3457 } )
3558
59+ const closedStatus = ref < 'OPEN' | 'CONNECTING' | 'CLOSED' > ( 'CLOSED' )
60+
61+ if ( import . meta. server ) {
62+ return {
63+ open : ( ) => { } ,
64+ close : ( ) => { } ,
65+ status : closedStatus ,
66+ }
67+ }
68+
3669 const { open, close, status } = useWebSocket ( wsUrl , {
37- immediate : options . immediate ?? true ,
70+ immediate : false ,
71+ autoConnect : false ,
72+ autoClose : true ,
3873 autoReconnect : options . autoReconnect ?? {
3974 delay : 1000 ,
40- onFailed ( ) {
75+ onFailed : ( ) => {
4176 console . error ( 'WebSocket connection failed' )
4277 } ,
4378 } ,
44- onMessage ( _ws : WebSocket , event : MessageEvent ) {
79+ onMessage : ( _ws : WebSocket , event : MessageEvent ) => {
4580 try {
4681 const parsed = JSON . parse ( event . data ) as WebSocketMessage
4782 if ( parsed . type && options . onMessage ) {
@@ -54,17 +89,21 @@ export function useWebSocketService(channel: MaybeRefOrGetter<string | null>, op
5489 } )
5590
5691 watch (
57- ( ) => wsUrl . value ,
92+ wsUrl ,
5893 ( url ) => {
5994 if ( url ) {
6095 open ( )
6196 } else {
6297 close ( )
6398 }
6499 } ,
65- { immediate : true }
100+ { immediate : options . immediate ?? true } ,
66101 )
67102
103+ onBeforeUnmount ( ( ) => {
104+ close ( )
105+ } )
106+
68107 return {
69108 open,
70109 close,
0 commit comments