react-ecosistema-unp 1.3.0-pre-release.5 → 1.3.0-pre-release.6
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,39 +1,46 @@
|
|
|
1
|
-
import { jsx as
|
|
2
|
-
import { useRef as
|
|
1
|
+
import { jsx as W } from "react/jsx-runtime";
|
|
2
|
+
import { useRef as i, useEffect as x } from "react";
|
|
3
3
|
import { L as k } from "../../index-Cs6Q8pv4.js";
|
|
4
|
-
import { useWebSocketContext as
|
|
4
|
+
import { useWebSocketContext as C } from "./WebSocketProvider.js";
|
|
5
5
|
import '../../assets/WebSocketManager.css';const z = ({
|
|
6
|
-
urlWs:
|
|
7
|
-
handlers:
|
|
8
|
-
socketKey:
|
|
9
|
-
autoReconnect:
|
|
10
|
-
maxReconnectAttempts:
|
|
6
|
+
urlWs: f,
|
|
7
|
+
handlers: m,
|
|
8
|
+
socketKey: e,
|
|
9
|
+
autoReconnect: d = !0,
|
|
10
|
+
maxReconnectAttempts: S = 5
|
|
11
11
|
}) => {
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
console.log(
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
12
|
+
const r = i(null), a = i(0), u = i(!1), { setSocket: g, setStatus: t, removeSocket: l } = C();
|
|
13
|
+
return x(() => {
|
|
14
|
+
let c = !1;
|
|
15
|
+
const b = () => {
|
|
16
|
+
r.current && (r.current.readyState === WebSocket.OPEN || r.current.readyState === WebSocket.CONNECTING) && r.current.close(), console.log(`Conectando WebSocket: ${e}`), t(e, "connecting");
|
|
17
|
+
const o = new WebSocket(f);
|
|
18
|
+
r.current = o, o.onopen = () => {
|
|
19
|
+
c || (console.log("WebSocket conectado."), a.current = 0, u.current = !0, g(e, o), t(e, "connected"));
|
|
20
|
+
}, o.onmessage = (n) => {
|
|
21
|
+
if (!c)
|
|
22
|
+
try {
|
|
23
|
+
const s = JSON.parse(n.data), p = m.find((N) => N.message === s.message);
|
|
24
|
+
p ? p.action(s) : console.warn("No se encontró handler para el mensaje:", s.message);
|
|
25
|
+
} catch (s) {
|
|
26
|
+
console.error("Error procesando mensaje:", s);
|
|
27
|
+
}
|
|
28
|
+
}, o.onerror = (n) => {
|
|
29
|
+
c || (console.error("Error en WebSocket:", n), t(e, "error"));
|
|
30
|
+
}, o.onclose = () => {
|
|
31
|
+
if (!c)
|
|
32
|
+
if (console.log("WebSocket cerrado."), t(e, "disconnected"), l(e), d && a.current < S && u.current) {
|
|
33
|
+
const n = Math.min(3e4, 1e3 * 2 ** a.current);
|
|
34
|
+
console.log(`Reintentando conexión en ${n / 1e3}s...`), setTimeout(() => {
|
|
35
|
+
a.current += 1, b();
|
|
36
|
+
}, n);
|
|
37
|
+
} else u.current ? console.error("No se realizará más reconexiones. Máximo alcanzado.") : console.error("Nunca se logró conectar. No se realizarán reconexiones.");
|
|
38
|
+
};
|
|
32
39
|
};
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
40
|
+
return b(), () => {
|
|
41
|
+
c = !0, r.current && (r.current.close(), l(e));
|
|
42
|
+
};
|
|
43
|
+
}, [f, m, e, d, S, g, t, l]), /* @__PURE__ */ W(k, {});
|
|
37
44
|
};
|
|
38
45
|
export {
|
|
39
46
|
z as WebSocketManager
|
package/package.json
CHANGED