-
Notifications
You must be signed in to change notification settings - Fork 19
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
56d6305
commit 5364d5c
Showing
7 changed files
with
81 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { createContext } from "react"; | ||
|
||
export interface WebSocketContextType { | ||
sendMessage: (message: string) => void; | ||
lastMessage: MessageEvent | null; | ||
readyState: number; | ||
} | ||
|
||
export const WebSocketContext = createContext<WebSocketContextType | undefined>( | ||
undefined, | ||
); |
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,34 @@ | ||
import React from "react"; | ||
import { | ||
WebSocketContext, | ||
type WebSocketContextType, | ||
} from "./websocket-context"; | ||
import useWebSocket from "react-use-websocket"; | ||
|
||
interface WebSocketProviderProps { | ||
url: string; | ||
children: React.ReactNode; | ||
} | ||
|
||
export const WebSocketProvider: React.FC<WebSocketProviderProps> = ({ | ||
url, | ||
children, | ||
}) => { | ||
const { sendMessage, lastMessage, readyState } = useWebSocket(url, { | ||
reconnectAttempts: 10, | ||
reconnectInterval: 3000, | ||
shouldReconnect: () => true, | ||
}); | ||
|
||
const contextValue: WebSocketContextType = { | ||
sendMessage, | ||
lastMessage, | ||
readyState, | ||
}; | ||
|
||
return ( | ||
<WebSocketContext.Provider value={contextValue}> | ||
{children} | ||
</WebSocketContext.Provider> | ||
); | ||
}; |
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,12 @@ | ||
import { useContext } from "react"; | ||
import { WebSocketContext } from "../context/websocket-context"; | ||
|
||
export const useWebSocketContext = () => { | ||
const context = useContext(WebSocketContext); | ||
if (context === undefined) { | ||
throw new Error( | ||
"useWebSocketContext must be used within a WebSocketProvider", | ||
); | ||
} | ||
return context; | ||
}; |
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