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