Skip to content

Commit

Permalink
Merge pull request #103 from lightninglabs/2024-02-fix-lnc-disconnect…
Browse files Browse the repository at this point in the history
…ion-error

Fix WASM-client disconnection error
  • Loading branch information
ellemouton authored Feb 5, 2024
2 parents d8c9f92 + 58a5902 commit f135fdc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
2 changes: 2 additions & 0 deletions cmd/wasm-client/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package main

import (
"github.com/btcsuite/btclog"
"github.com/lightninglabs/lightning-node-connect/gbn"
"github.com/lightninglabs/lightning-node-connect/mailbox"
"github.com/lightningnetwork/lnd"
"github.com/lightningnetwork/lnd/build"
Expand All @@ -25,6 +26,7 @@ func SetupLoggers(root *build.RotatingLogWriter, intercept signal.Interceptor) {

lnd.SetSubLogger(root, Subsystem, log)
lnd.AddSubLogger(root, mailbox.Subsystem, intercept, mailbox.UseLogger)
lnd.AddSubLogger(root, gbn.Subsystem, intercept, gbn.UseLogger)

grpclog.SetLoggerV2(NewGrpcLogLogger(root, intercept, "GRPC"))
}
Expand Down
27 changes: 22 additions & 5 deletions cmd/wasm-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,29 @@ func (w *wasmClient) IsConnected(_ js.Value, _ []js.Value) interface{} {
return js.ValueOf(w.lndConn != nil)
}

func (w *wasmClient) Disconnect(_ js.Value, _ []js.Value) interface{} {
// Disconnect disconnects the client, and closes the connection.
// The first argument passed should be a onDisconnect callback, which will be
// invoked once the client has disconnected.
func (w *wasmClient) Disconnect(_ js.Value, args []js.Value) interface{} {
if w.lndConn != nil {
if err := w.lndConn.Close(); err != nil {
log.Errorf("Error closing RPC connection: %v", err)
}
w.lndConn = nil
// We launch the closure of the connection in a goroutine to
// avoid that the JS websocket freezes and blocks while closing.
go func() {
if err := w.lndConn.Close(); err != nil {
log.Errorf("Error closing RPC connection: %v",
err)
}
w.lndConn = nil

// We expect the first arg to be the onDisconnect
// callback
if len(args) > 0 && args[0].Type() == js.TypeFunction {
callback := args[0]

// Call the onDisconnect callback.
callback.Invoke()
}
}()
}

return nil
Expand Down
4 changes: 3 additions & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@
}

async function disconnect() {
window[namespace].wasmClientDisconnect();
window[namespace].wasmClientDisconnect(onDisconnect);
}

function onDisconnect() {
document.getElementById('disconnectBtn').disabled = true;
document.getElementById('reconnectBtn').disabled = false;
document.getElementById('ready').style.display= 'none' ;
Expand Down

0 comments on commit f135fdc

Please sign in to comment.