export function useWebSocket() {
  const { $ws } = useNuxtApp()
  const messages = ref<any[]>([])

  // 监听消息
  const onMessage = (callback: (data: any) => void) => {
    const handler = (event: CustomEvent) => callback(event.detail)
    window.addEventListener('ws-message', handler as EventListener)
    
    // 返回清理函数
    return () => {
      window.removeEventListener('ws-message', handler as EventListener)
    }
  }

  return {
    ws: $ws,
    messages,
    onMessage
  }
} 