|
|
@@ -1,4 +1,4 @@
|
|
|
-import { useRef } from "react"
|
|
|
+import { useRef } from "react";
|
|
|
|
|
|
interface NewWebsocketOptions {
|
|
|
onopen?: () => void;
|
|
|
@@ -12,17 +12,17 @@ interface WebsocketConfig extends NewWebsocketOptions {
|
|
|
}
|
|
|
|
|
|
type WebsocketConfigMap = {
|
|
|
- [id: string]: WebsocketConfig
|
|
|
-}
|
|
|
+ [id: string]: WebsocketConfig;
|
|
|
+};
|
|
|
|
|
|
type WebsocketMap = {
|
|
|
- [id: string]: WebSocket
|
|
|
-}
|
|
|
+ [id: string]: WebSocket;
|
|
|
+};
|
|
|
|
|
|
export const useWebsockets = () => {
|
|
|
const websocketMap = useRef<WebsocketMap>({});
|
|
|
- const websocketConfigMap = useRef<WebsocketConfigMap>({})
|
|
|
-
|
|
|
+ const websocketConfigMap = useRef<WebsocketConfigMap>({});
|
|
|
+
|
|
|
/**
|
|
|
* Setup for a new websocket, after calling new websocket you can open the connection with openWebsocket
|
|
|
* @param id Id to access later the websocket config/connection
|
|
|
@@ -30,50 +30,52 @@ export const useWebsockets = () => {
|
|
|
* @param options Websocket listeners
|
|
|
* @returns An object with the config setted for that websocket. This config will be used to open the ws on openWebsocket
|
|
|
*/
|
|
|
- const newWebsocket = (id: string, apiEndpoint: string, options: NewWebsocketOptions): WebsocketConfig => {
|
|
|
-
|
|
|
+ const newWebsocket = (
|
|
|
+ id: string,
|
|
|
+ apiEndpoint: string,
|
|
|
+ options: NewWebsocketOptions
|
|
|
+ ): WebsocketConfig => {
|
|
|
if (!id) {
|
|
|
console.log("Id cannot be empty");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
if (!apiEndpoint) {
|
|
|
- console.log("Api endpoint string cannot be empty")
|
|
|
+ console.log("Api endpoint string cannot be empty");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
let protocol = window.location.protocol == "https:" ? "wss" : "ws";
|
|
|
|
|
|
- const url = `${protocol}://${window.location.host}${apiEndpoint}`
|
|
|
+ const url = `${protocol}://${window.location.host}${apiEndpoint}`;
|
|
|
+
|
|
|
+ const mockFunction = () => {};
|
|
|
|
|
|
- const mockFunction = () => {}
|
|
|
-
|
|
|
const wsConfig: WebsocketConfig = {
|
|
|
url,
|
|
|
onopen: options?.onopen || mockFunction,
|
|
|
onmessage: options?.onmessage || mockFunction,
|
|
|
onerror: options?.onerror || mockFunction,
|
|
|
onclose: options?.onclose || mockFunction,
|
|
|
- }
|
|
|
-
|
|
|
+ };
|
|
|
+
|
|
|
websocketConfigMap.current = {
|
|
|
...websocketConfigMap.current,
|
|
|
[id]: wsConfig,
|
|
|
- }
|
|
|
+ };
|
|
|
return wsConfig;
|
|
|
- }
|
|
|
+ };
|
|
|
|
|
|
/**
|
|
|
* Opens the websocket connection based on a config previously setted by
|
|
|
- * newWebsocket
|
|
|
+ * newWebsocket
|
|
|
*/
|
|
|
const openWebsocket = (id: string) => {
|
|
|
const wsConfig = websocketConfigMap.current[id];
|
|
|
|
|
|
// Prevent calling openWebsocket before newWebsocket
|
|
|
if (!wsConfig) {
|
|
|
- console.log("Couldn't find ws config")
|
|
|
+ console.log("Couldn't find ws config");
|
|
|
return;
|
|
|
}
|
|
|
// In case of having a previous websocket opened with the same ID, close the previous one
|
|
|
@@ -85,14 +87,14 @@ export const useWebsockets = () => {
|
|
|
const { url, ...listeners } = wsConfig;
|
|
|
|
|
|
const ws = new WebSocket(wsConfig.url);
|
|
|
-
|
|
|
+
|
|
|
Object.assign(ws, listeners);
|
|
|
|
|
|
websocketMap.current = {
|
|
|
...websocketMap.current,
|
|
|
[id]: ws,
|
|
|
- }
|
|
|
- }
|
|
|
+ };
|
|
|
+ };
|
|
|
|
|
|
/**
|
|
|
* Close specific websocket
|
|
|
@@ -106,29 +108,29 @@ export const useWebsockets = () => {
|
|
|
}
|
|
|
|
|
|
ws.close(code, reason);
|
|
|
- }
|
|
|
+ };
|
|
|
|
|
|
- /**
|
|
|
+ /**
|
|
|
* Closes all websockets opened by the useWebsocket hook
|
|
|
- */
|
|
|
+ */
|
|
|
const closeAllWebsockets = () => {
|
|
|
- Object.keys(websocketMap.current).forEach(key => {
|
|
|
+ Object.keys(websocketMap.current).forEach((key) => {
|
|
|
closeWebsocket(key);
|
|
|
- })
|
|
|
- }
|
|
|
+ });
|
|
|
+ };
|
|
|
|
|
|
/**
|
|
|
* Get websocket by id
|
|
|
*/
|
|
|
const getWebsocket = (id: string) => {
|
|
|
return websocketMap.current[id];
|
|
|
- }
|
|
|
+ };
|
|
|
|
|
|
return {
|
|
|
newWebsocket,
|
|
|
openWebsocket,
|
|
|
getWebsocket,
|
|
|
closeWebsocket,
|
|
|
- closeAllWebsockets
|
|
|
- }
|
|
|
-}
|
|
|
+ closeAllWebsockets,
|
|
|
+ };
|
|
|
+};
|