jnfrati 4 лет назад
Родитель
Сommit
ef2319fb98
1 измененных файлов с 5 добавлено и 7 удалено
  1. 5 7
      dashboard/src/shared/hooks/useWebsockets.ts

+ 5 - 7
dashboard/src/shared/hooks/useWebsockets.ts

@@ -1,4 +1,4 @@
-import { useCallback, useEffect, useRef, useState } from "react"
+import { useRef } from "react"
 
 
 interface NewWebsocketOptions {
 interface NewWebsocketOptions {
   onopen?: () => void;
   onopen?: () => void;
@@ -28,7 +28,7 @@ export const useWebsockets = () => {
    * @param id Id to access later the websocket config/connection
    * @param id Id to access later the websocket config/connection
    * @param apiEndpoint Endpoint to connect the websocket e.g: /api/websocket
    * @param apiEndpoint Endpoint to connect the websocket e.g: /api/websocket
    * @param options Websocket listeners
    * @param options Websocket listeners
-   * @returns 
+   * @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 => {
     
     
@@ -82,13 +82,11 @@ export const useWebsockets = () => {
     if (prevWs) {
     if (prevWs) {
       prevWs.close();
       prevWs.close();
     }
     }
-    
+    const { url, ...listeners } = wsConfig;
+
     const ws = new WebSocket(wsConfig.url);
     const ws = new WebSocket(wsConfig.url);
-    ws.onopen = wsConfig.onopen;
-    ws.onmessage = wsConfig.onmessage;
-    ws.onerror = wsConfig.onerror;
-    ws.onclose = wsConfig.onclose;
     
     
+    Object.assign(ws, listeners);
 
 
     websocketMap.current = {
     websocketMap.current = {
       ...websocketMap.current,
       ...websocketMap.current,