-
Notifications
You must be signed in to change notification settings - Fork 3
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
9936440
commit 0dc6984
Showing
5 changed files
with
167 additions
and
83 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,48 @@ | ||
"use client"; | ||
|
||
import { socketManager } from "@/utils/websockets"; | ||
import { useEffect, useRef, useState } from "react"; | ||
|
||
type Props = { | ||
namespace: string; | ||
auth_token: string; | ||
}; | ||
export default function useSocket({ namespace, auth_token }: Props) { | ||
const [connected, setConnected] = useState(false); | ||
|
||
const socketRef = useRef( | ||
socketManager.socket(`/${namespace}`, { | ||
auth: { | ||
token: auth_token, | ||
}, | ||
}), | ||
); | ||
|
||
useEffect(() => { | ||
const socket = socketRef.current; | ||
|
||
if (socket.connected) { | ||
onConnect(); | ||
} | ||
|
||
function onConnect() { | ||
setConnected(true); | ||
} | ||
|
||
function onDisconnect() { | ||
setConnected(false); | ||
} | ||
|
||
socket.on("connect", onConnect); | ||
socket.on("disconnect", onDisconnect); | ||
socket.connect(); | ||
|
||
return () => { | ||
socket.off("connect", onConnect); | ||
socket.off("disconnect", onDisconnect); | ||
socket.disconnect(); | ||
}; | ||
}, []); | ||
|
||
return { handle: socketRef.current, connected }; | ||
} |
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,2 +1,2 @@ | ||
export const API_BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL; | ||
export const API_BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL!; | ||
export const API_REST_BASE_URL = `${API_BASE_URL}/api/v1`; |
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,8 +1,10 @@ | ||
"use client"; | ||
|
||
import { API_BASE_URL } from "./constants/api.constant"; | ||
import { Manager } from "socket.io-client"; | ||
|
||
export const getSocketManager = () => | ||
new Manager(API_BASE_URL, { | ||
closeOnBeforeunload: true, | ||
withCredentials: true, | ||
}); | ||
export const socketManager = new Manager(API_BASE_URL, { | ||
withCredentials: true, | ||
closeOnBeforeunload: true, | ||
autoConnect: false, | ||
}); |