commit ca9f3dd01b58aa59982bed9a8ec4e8b46adb4425
parent 268d4fa9d96bbc795b96c402f0d45dab4c452fad
Author: William Casarin <jb55@jb55.com>
Date: Sat, 12 Mar 2022 13:11:02 -0800
lnsocket_lib fixes
Diffstat:
1 file changed, 20 insertions(+), 6 deletions(-)
diff --git a/lnsocket_lib.js b/lnsocket_lib.js
@@ -5,7 +5,7 @@ async function lnsocket_init() {
const ACT_ONE_SIZE = 50
const ACT_TWO_SIZE = 50
const ACT_THREE_SIZE = 66
- const DEFAULT_TIMEOUT = 3000
+ const DEFAULT_TIMEOUT = 15000
const COMMANDO_REPLY_CONTINUES = 0x594b
const COMMANDO_REPLY_TERM = 0x594d
@@ -234,24 +234,38 @@ async function lnsocket_init() {
return await this.read()
}
+ LNSocket.prototype.disconnect = function lnsocket_disconnect() {
+ if (this.connected === true && this.ws) {
+ this.ws.close()
+ return true
+ }
+ return false
+ }
+
function handle_connect(ln, node_id, host) {
- const ws = new WebSocket(`ws://${host}`)
+ const isws = host.startsWith("ws://") || host.startsWith("wss://")
+ if (!isws)
+ throw new Error("host must start with ws:// or wss://")
+ const ws = new WebSocket(host)
return new Promise((resolve, reject) => {
+ const timeout = ln.opts.timeout || DEFAULT_TIMEOUT
+ const timer = setTimeout(reject, timeout);
+
ws.onmessage = (v) => {
ln.queue.push(v.data.arrayBuffer())
}
ws.addEventListener('open', function(ev) {
ln.ws = ws
+ ln.connected = true
+ clearTimeout(timer)
resolve(ws)
});
ws.addEventListener('close', function(ev) {
- reject()
+ ln.connected = false
+ console.log("closed websocket connection")
});
-
- const timeout = ln.opts.timeout || DEFAULT_TIMEOUT
- setTimeout(reject, timeout);
})
}