-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- hostview에서 socket으로 guest들의 실시간 위치 정보만 받아오기 - guestview에서는 socket으로 본인의 위치를 보내는 역할
- Loading branch information
Showing
8 changed files
with
324 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { useState, useEffect } from 'react'; | ||
|
||
export const useSocket = (url: string) => { | ||
const [ws, setWs] = useState<WebSocket | null>(null); | ||
|
||
useEffect(() => { | ||
const socket = new WebSocket(url); | ||
setWs(socket); | ||
|
||
socket.onopen = () => { | ||
console.log('Socket connected'); | ||
}; | ||
|
||
socket.onmessage = event => { | ||
const data = JSON.parse(event.data); | ||
console.log(data); | ||
}; | ||
|
||
socket.onclose = () => { | ||
console.log('Socket closed'); | ||
}; | ||
|
||
socket.onerror = err => { | ||
console.error('Socket error:', err); | ||
}; | ||
|
||
return () => { | ||
socket.close(); | ||
}; | ||
}, [url]); | ||
|
||
return ws; | ||
}; |
Oops, something went wrong.