-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #857 from stakwork/feature/update-useSocket
feat: update websocket
- Loading branch information
Showing
8 changed files
with
84 additions
and
59 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,21 @@ | ||
// SocketContext.ts | ||
import { createContext, useContext } from 'react' | ||
import { Socket } from 'socket.io-client' | ||
|
||
interface SocketContextType { | ||
socket: Socket | ||
} | ||
|
||
const SocketContext = createContext<SocketContextType | undefined>(undefined) | ||
|
||
export const useSocket = () => { | ||
const context = useContext(SocketContext) | ||
|
||
if (!context) { | ||
throw new Error('useSocket must be used within a SocketProvider') | ||
} | ||
|
||
return context.socket | ||
} | ||
|
||
export default SocketContext |
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,17 @@ | ||
// SocketProvider.tsx | ||
import { FC, ReactNode } from 'react' | ||
import { io } from 'socket.io-client' | ||
import { API_URL } from '~/constants' | ||
import SocketContext from './SocketContext' | ||
|
||
interface SocketProviderProps { | ||
children: ReactNode | ||
} | ||
|
||
const contextValue = { | ||
socket: io(API_URL), | ||
} | ||
|
||
export const SocketProvider: FC<SocketProviderProps> = ({ children }) => ( | ||
<SocketContext.Provider value={contextValue}>{children}</SocketContext.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
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,23 +1,8 @@ | ||
// useSocket.ts | ||
import { useContext } from 'react' | ||
import SocketContext from '~/components/App/Providers/Socket/SocketContext' | ||
|
||
import { useEffect, useState } from 'react' | ||
import { Socket, io } from 'socket.io-client' | ||
import { API_URL } from '~/constants' | ||
export const useSocket = () => { | ||
const context = useContext(SocketContext) | ||
|
||
const useSocket = (): Socket | null => { | ||
const [socket, setSocket] = useState<Socket | null>(null) | ||
|
||
useEffect(() => { | ||
const socketInstance = io(API_URL) | ||
|
||
setSocket(socketInstance) | ||
|
||
return () => { | ||
socketInstance.disconnect() | ||
} | ||
}, []) | ||
|
||
return socket | ||
return context?.socket | ||
} | ||
|
||
export default useSocket |