index.ts 470 B

12345678910111213141516
  1. import ReconnectingWebSocket from "reconnecting-websocket"
  2. import {useUserStore} from "@/pinia/user"
  3. import {storeToRefs} from "pinia"
  4. function ws(url: string): ReconnectingWebSocket {
  5. const user = useUserStore()
  6. const {token} = storeToRefs(user)
  7. const protocol = location.protocol === 'https:' ? 'wss://' : 'ws://'
  8. return new ReconnectingWebSocket(
  9. protocol + window.location.host + url + '?token=' + btoa(token.value))
  10. }
  11. export default ws