-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
06c4176
commit aa912c2
Showing
21 changed files
with
189 additions
and
234 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
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,31 @@ | ||
import { PropsWithChildren, useEffect, useState } from 'react' | ||
|
||
import { socket } from '@/socket' | ||
|
||
interface Props extends PropsWithChildren {} | ||
|
||
const WebSocketContext: React.FC<Props> = ({ children }) => { | ||
const [_, setIsConnected] = useState(socket.connected) | ||
|
||
useEffect(() => { | ||
function onConnect() { | ||
setIsConnected(true) | ||
} | ||
|
||
function onDisconnect() { | ||
setIsConnected(false) | ||
} | ||
|
||
socket.on('connect', onConnect) | ||
socket.on('disconnect', onDisconnect) | ||
|
||
return () => { | ||
socket.off('connect', onConnect) | ||
socket.off('disconnect', onDisconnect) | ||
} | ||
}, []) | ||
|
||
return children | ||
} | ||
|
||
export default WebSocketContext |
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 |
---|---|---|
@@ -1,61 +1,7 @@ | ||
import { useEffect } from 'react' | ||
import useWebSocket from 'react-use-websocket' | ||
import { io } from 'socket.io-client' | ||
|
||
import { getWebsocketUrl } from './repository' | ||
import { WebSocketMessage } from './types' | ||
|
||
/** | ||
* Hook to access the websocket | ||
* | ||
* This is a wrapper over react-use-websocket that enforces | ||
* the application protocol implemented on the backend | ||
* | ||
* @param topic | ||
* @returns | ||
*/ | ||
export const useSocket = (topic?: string) => { | ||
const filter = (msg: MessageEvent) => { | ||
const { type } = JSON.parse(msg.data) | ||
|
||
return type === topic | ||
} | ||
|
||
const hook = useWebSocket(getWebsocketUrl().toString(), { | ||
share: true, | ||
filter: topic ? filter : undefined, | ||
}) | ||
|
||
const lastMessage = hook.lastJsonMessage as unknown as WebSocketMessage | ||
|
||
const send = (type: string, data: any) => { | ||
hook.sendJsonMessage({ | ||
type, | ||
data, | ||
}) | ||
} | ||
|
||
const subscribe = (topic: string) => { | ||
send('subscribe', topic) | ||
} | ||
|
||
const unsubscribe = (topic: string) => { | ||
send('unsubscribe', topic) | ||
} | ||
|
||
useEffect(() => { | ||
if (topic) { | ||
subscribe(topic) | ||
|
||
return () => { | ||
unsubscribe(topic) | ||
} | ||
} | ||
}, []) | ||
|
||
return { | ||
lastMessage, | ||
send, | ||
subscribe, | ||
unsubscribe, | ||
} | ||
} | ||
export const socket = io(getWebsocketUrl().toString(), { | ||
transports: ['websocket'], | ||
}) |
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
Oops, something went wrong.