-
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.
[Feat] link the front-end with the backend for online and offline status
- Loading branch information
Showing
7 changed files
with
102 additions
and
57 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,17 @@ | ||
'use client'; | ||
import MainContainer from "../../components/mainContainer"; | ||
import AuthChecker from "../../components/authChecker"; | ||
import { WebSocketProvider } from '@/components/webSocket' | ||
import React from 'react'; | ||
|
||
export default function DashboardLayout({ children }: { children: React.ReactNode }) { | ||
return ( | ||
<AuthChecker> | ||
<MainContainer> | ||
{children} | ||
</MainContainer> | ||
<WebSocketProvider> | ||
<MainContainer> | ||
{children} | ||
</MainContainer> | ||
</WebSocketProvider> | ||
</AuthChecker> | ||
); | ||
} |
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,37 @@ | ||
import React, { createContext, useEffect, useState, ReactNode } from 'react'; | ||
import Cookies from 'js-cookie'; | ||
|
||
interface WebSocketContextValue { | ||
socket: WebSocket | null; | ||
} | ||
|
||
export const WebSocketContext = createContext<WebSocketContextValue | null>(null); | ||
|
||
interface WebSocketProviderProps { | ||
children: ReactNode; | ||
} | ||
|
||
export const WebSocketProvider: React.FC<WebSocketProviderProps> = ({ children }) => { | ||
const [socket, setSocket] = useState<WebSocket | null>(null); | ||
|
||
useEffect(() => { | ||
const access = Cookies.get('access'); | ||
const newSocket = new WebSocket(`ws://localhost:8000/ws/user-status?token=${access}`); | ||
|
||
newSocket.onopen = () => { | ||
console.log('WebSocket connection opened'); | ||
}; | ||
|
||
setSocket(newSocket); | ||
|
||
return () => { | ||
newSocket.close(); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<WebSocketContext.Provider value={{ socket }}> | ||
{children} | ||
</WebSocketContext.Provider> | ||
); | ||
}; |