react-ecosistema-unp 1.3.0-pre-release.2 → 1.3.0-pre-release.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,42 +1,40 @@
|
|
|
1
1
|
import { jsx as p } from "react/jsx-runtime";
|
|
2
|
-
import { useRef as
|
|
3
|
-
import { L as
|
|
4
|
-
import { useWebSocketContext as
|
|
5
|
-
import '../../assets/WebSocketManager.css';const
|
|
6
|
-
urlWs:
|
|
7
|
-
handlers:
|
|
8
|
-
socketKey:
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
maxReconnectAttempts: d = 5
|
|
2
|
+
import { useRef as u, useCallback as W, useEffect as x } from "react";
|
|
3
|
+
import { L as N } from "../../index-Cs6Q8pv4.js";
|
|
4
|
+
import { useWebSocketContext as h } from "./WebSocketProvider.js";
|
|
5
|
+
import '../../assets/WebSocketManager.css';const z = ({
|
|
6
|
+
urlWs: i,
|
|
7
|
+
handlers: m,
|
|
8
|
+
socketKey: e,
|
|
9
|
+
autoReconnect: d = !0,
|
|
10
|
+
maxReconnectAttempts: f = 5
|
|
12
11
|
}) => {
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
console.log("WebSocket conectado."),
|
|
16
|
-
},
|
|
17
|
-
console.log("Mensaje recibido:",
|
|
12
|
+
const r = u(null), t = u(0), { setSocket: g, setStatus: n, removeSocket: s } = h(), a = u(!1), l = W(() => {
|
|
13
|
+
r.current && r.current.close(), console.log(`Conectando WebSocket: ${e}`), n(e, "connecting"), r.current = new WebSocket(i), r.current.onopen = () => {
|
|
14
|
+
console.log("WebSocket conectado."), t.current = 0, a.current = !0, g(e, r.current), n(e, "connected");
|
|
15
|
+
}, r.current.onmessage = (o) => {
|
|
16
|
+
console.log("Mensaje recibido:", o.data);
|
|
18
17
|
try {
|
|
19
|
-
const
|
|
20
|
-
S ? S.action(
|
|
21
|
-
} catch (
|
|
22
|
-
console.error("Error procesando mensaje
|
|
18
|
+
const c = JSON.parse(o.data), S = m.find((b) => b.message === c.message);
|
|
19
|
+
S ? S.action(c) : console.warn("No se encontró handler para el mensaje:", c.message);
|
|
20
|
+
} catch (c) {
|
|
21
|
+
console.error("Error procesando mensaje:", c);
|
|
23
22
|
}
|
|
24
|
-
},
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
},
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
console.error("Error en WebSocket:", n), t(r, "error"), (o = e.current) == null || o.close();
|
|
23
|
+
}, r.current.onerror = (o) => {
|
|
24
|
+
console.error("Error en WebSocket:", o), n(e, "error");
|
|
25
|
+
}, r.current.onclose = () => {
|
|
26
|
+
if (console.log("WebSocket cerrado."), n(e, "disconnected"), s(e), d && t.current < f && a.current) {
|
|
27
|
+
const o = Math.min(3e4, 1e3 * 2 ** t.current);
|
|
28
|
+
console.log(`Reintentando conexión en ${o / 1e3}s...`), setTimeout(() => {
|
|
29
|
+
t.current += 1, l();
|
|
30
|
+
}, o);
|
|
31
|
+
} else a.current ? console.error("No se realizará más reconexiones. Máximo alcanzado.") : console.error("Nunca se logró conectar. No se realizarán reconexiones.");
|
|
34
32
|
};
|
|
35
|
-
}, [
|
|
36
|
-
return
|
|
37
|
-
|
|
38
|
-
}), [
|
|
33
|
+
}, [i, m, e, d, f, g, n, s]);
|
|
34
|
+
return x(() => (l(), () => {
|
|
35
|
+
r.current && (r.current.close(), s(e));
|
|
36
|
+
}), [l, e, s]), /* @__PURE__ */ p(N, {});
|
|
39
37
|
};
|
|
40
38
|
export {
|
|
41
|
-
|
|
39
|
+
z as WebSocketManager
|
|
42
40
|
};
|
package/package.json
CHANGED