base-websocket
Advanced tools
Comparing version 1.0.10 to 1.0.11
{ | ||
"name": "base-websocket", | ||
"version": "1.0.10", | ||
"version": "1.0.11", | ||
"description": "webSocket basic module", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -8,4 +8,5 @@ interface Data<obj> { | ||
onResponse: (res: any) => void; | ||
onError: () => void; | ||
onClose: () => void; | ||
onError: (error: Event) => void; | ||
onClose: (e: CloseEvent) => void; | ||
retry?: number; | ||
} | ||
@@ -15,3 +16,3 @@ type Send = <obj>(data: Data<obj>) => void; | ||
declare let closeWS: () => void; | ||
declare function initWebSocket({ url, onOpen, onResponse, onError, onClose, }: Init): void; | ||
declare function initWebSocket({ url, onOpen, onResponse, onError, onClose, retry, }: Init): void; | ||
export { initWebSocket, send, closeWS }; |
@@ -1,7 +0,7 @@ | ||
let a = () => { | ||
}, r = () => { | ||
}, o = !1; | ||
function S({ | ||
url: t, | ||
onOpen: c = function() { | ||
let g = () => { | ||
}, u = () => { | ||
}, n = !1; | ||
function d({ | ||
url: c, | ||
onOpen: s = function() { | ||
}, | ||
@@ -12,28 +12,39 @@ onResponse: l = function() { | ||
}, | ||
onClose: f = function() { | ||
} | ||
onClose: a = function() { | ||
}, | ||
retry: f = 0 | ||
}) { | ||
if (o) | ||
if (n) | ||
return; | ||
o = !0; | ||
const e = new WebSocket(t); | ||
a = (n) => { | ||
e.send(JSON.stringify(n)); | ||
}, r = () => { | ||
console.log("WS CLOSE"), f(), e.close(), o = !1; | ||
}, e.onopen = () => { | ||
c(); | ||
}, e.onmessage = async (n) => { | ||
const s = JSON.parse(await new Response(n.data).text()); | ||
s && l(s); | ||
}, e.onerror = (n) => { | ||
console.log("ERROR: ", n), o = !1, e.close(), i(); | ||
}, e.onclose = () => { | ||
console.log("CLOSE"); | ||
n = !0; | ||
const t = new WebSocket(c); | ||
let o = 0; | ||
g = (e) => { | ||
t.send(JSON.stringify(e)); | ||
}, u = () => { | ||
console.log("WS CLOSE"), t.close(), n = !1; | ||
}, t.onopen = () => { | ||
o = 0, s(); | ||
}, t.onmessage = async (e) => { | ||
const S = JSON.parse(await new Response(e.data).text()); | ||
S && l(S); | ||
}, t.onerror = (e) => { | ||
console.log("ERROR: ", e), n = !1, i(e), t.close(); | ||
}, t.onclose = (e) => { | ||
e.code !== 1e3 && o < f ? (o++, console.log( | ||
`WS connection lost, attempting to reconnect... (attempt ${o})` | ||
), d({ | ||
url: c, | ||
onOpen: s, | ||
onResponse: l, | ||
onError: i, | ||
onClose: a, | ||
retry: f | ||
})) : (console.log("CLOSE"), a(e), n = !1); | ||
}; | ||
} | ||
export { | ||
r as closeWS, | ||
S as initWebSocket, | ||
a as send | ||
u as closeWS, | ||
d as initWebSocket, | ||
g as send | ||
}; |
2090
65