From b80cfb8e73b0204b18f1446e800e5d15f6345495 Mon Sep 17 00:00:00 2001 From: SimbaFs Date: Sat, 3 Aug 2024 00:38:59 +0800 Subject: [PATCH] fix ws URL --- frontend/src/hooks/useAttendance.ts | 5 ++++- frontend/src/hooks/useOrigin.ts | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 frontend/src/hooks/useOrigin.ts diff --git a/frontend/src/hooks/useAttendance.ts b/frontend/src/hooks/useAttendance.ts index 6b9727d..f36e0a7 100644 --- a/frontend/src/hooks/useAttendance.ts +++ b/frontend/src/hooks/useAttendance.ts @@ -4,11 +4,14 @@ import { useToken } from './useToken' import useWS from './useWS' import { shouldParse } from '@/libs/util' import { Diff } from './useDiff' +import { useOrigin } from './useOrigin' // useAttendance get init value of attendance from server and listen to websocket for updates export function useAttendance() { const token = useToken() - const { socket, lastMessage } = useWS(`ws://localhost:3000/ws?token=${token}`) + const wsURL = useOrigin().replace('http', 'ws') + '/ws?token=' + token + console.log({ wsURL }) + const { socket, lastMessage } = useWS(wsURL) const [attendance, updateAttendance] = useReducer( ( state: Attendance, diff --git a/frontend/src/hooks/useOrigin.ts b/frontend/src/hooks/useOrigin.ts new file mode 100644 index 0000000..b62ae66 --- /dev/null +++ b/frontend/src/hooks/useOrigin.ts @@ -0,0 +1,11 @@ +import { useEffect, useState } from 'react' + +export function useOrigin() { + const [origin, setOrigin] = useState('') + + useEffect(() => { + setOrigin(window.location.origin) + }, []) + + return origin +}