-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[#79] 웹 소켓 정상작동 시에 연결 #154
The head ref may contain hidden characters: "79-79-\uC6F9-\uC18C\uCF13-\uC815\uC0C1\uC791\uB3D9-\uC2DC\uC5D0-\uC5F0\uACB0"
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,47 +6,42 @@ interface Props { | |
socket: Socket; | ||
endsAt: Date; | ||
socketEvent: string; | ||
pingTime: number; | ||
} | ||
|
||
export default function useSocketTimer({ socket, endsAt, socketEvent }: Props) { | ||
export default function useSocketTimer({ socket, endsAt, socketEvent, pingTime }: Props) { | ||
const timerIntervalId = useRef<NodeJS.Timeout | null>(null); | ||
const pingIntervalId = useRef<NodeJS.Timeout | null>(null); | ||
|
||
const endTime = useMemo(() => endsAt.getTime(), [endsAt]); | ||
const [isTimeout, setIsTimeout] = useState(false); | ||
const [remainMiliSeconds, setRemainMiliSeconds] = useState<number>(-1); | ||
|
||
useEffect(() => { | ||
console.log('타이머 실행'); | ||
// 웹 소켓 대신 사용. | ||
mockWebSocket(); | ||
socket.emit(socketEvent); | ||
if (socket.hasListeners(socketEvent)) { | ||
socket.on(socketEvent, handlePingMessage); | ||
} | ||
socket.on(socketEvent, handlePingMessage); | ||
|
||
pingIntervalId.current = setInterval(() => { | ||
socket.emit(socketEvent); | ||
}, pingTime); | ||
}, [socket]); | ||
|
||
const handlePingMessage = useCallback((time: Date | string) => { | ||
console.log(time); | ||
if (timerIntervalId.current) clearInterval(timerIntervalId.current); | ||
|
||
time = typeof time === 'string' ? new Date(time) : time; | ||
const remainMiliSec = endTime - time.getTime(); | ||
setRemainMiliSeconds(remainMiliSec); | ||
timerIntervalId.current = setInterval(() => { | ||
setRemainMiliSeconds((prev) => prev - 1000); | ||
}, 1000); | ||
}, []); | ||
|
||
// 웹 소켓 대신 사용. | ||
// 웹 소켓 연결 후 삭제 예정 | ||
const mockWebSocket = useCallback(() => { | ||
const delayFactor = 2000; | ||
const serverTime = new Date(); | ||
handlePingMessage(serverTime); | ||
setInterval(() => { | ||
const serverTime = new Date(); | ||
handlePingMessage(serverTime); | ||
}, 5000 + Math.random() * delayFactor); | ||
}, []); | ||
const handlePingMessage = useCallback( | ||
(time: Date) => { | ||
console.log('서버에서 온 시간 websocket 연결 확인 용', time); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. console.log 이 부분은 지우지 않고 배포하게 되나요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. �if (process.env.NODE_ENV === "production") { // production에서만 사용할 수 없도록 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오오 신기하네요, 감사합니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. � 왜 자꾸 이런게 생길까요 � <- ??? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 그러게요 마크다운에서 깨지는 문자가 있나봐요: |
||
if (timerIntervalId.current) clearInterval(timerIntervalId.current); | ||
|
||
time = typeof time === 'string' ? new Date(time) : time; | ||
|
||
const remainMiliSec = endTime - time.getTime(); | ||
setRemainMiliSeconds(remainMiliSec); | ||
|
||
timerIntervalId.current = setInterval(() => { | ||
setRemainMiliSeconds((prev) => prev - 1000); | ||
}, 1000); | ||
}, | ||
[endTime], | ||
); | ||
|
||
useEffect(() => { | ||
// 초기 값인 -1 => 서버에서 시간이 오지 않았다. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
상수화 해주시면 더 좋아용
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SocketTimer에서 pingTime을 직접 주입하면 범용성이 없는것 같아 SocketTimer를 사용하는 곳에서 넘겨 받도록 했습니다.
현재는 대회 페이지에서 pingTime과 socketEvent를 상수화하고 SocketTimer에서 Props로 전달합니다.