From 2ff7f85b0cd79584b18f91583229a151e767a53a Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Tue, 18 Feb 2014 15:51:06 -0500 Subject: [PATCH] Notify connected ws clients of btcd connect state. This shouldn't be nececssary (a TODO was added to remind me to make clients explicitly ask for this info) but in the meantime this fixes clients such as btcgui which otherwise wouldn't think btcwallet is properly connected to btcd and will desensitise some widgets. --- sockets.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/sockets.go b/sockets.go index 444397c..fb66539 100644 --- a/sockets.go +++ b/sockets.go @@ -302,17 +302,21 @@ func NotifyBtcdConnection(reply chan []byte) { // forever (until disconnected), reading JSON-RPC requests and sending // sending responses and notifications. func WSSendRecv(ws *websocket.Conn) { - // Add frontend notification channel to set so this handler receives - // updates. + // Add client context so notifications duplicated to each + // client are received by this client. cc := clientContext{ - send: make(chan []byte), + send: make(chan []byte, 1), // buffer size is number of initial notifications disconnected: make(chan struct{}), } + NotifyBtcdConnection(cc.send) // TODO(jrick): clients should explicitly request this. addClient <- cc defer close(cc.disconnected) - // jsonMsgs receives JSON messages from the currently connected frontend. - jsonMsgs := make(chan []byte) + // received passes all received messages from the currently connected + // frontend to the for-select loop. It is closed when reading a + // message from the websocket connection fails (presumably due to + // a disconnected client). + received := make(chan []byte) // Receive messages from websocket and send across jsonMsgs until // connection is lost @@ -320,18 +324,18 @@ func WSSendRecv(ws *websocket.Conn) { for { var m []byte if err := websocket.Message.Receive(ws, &m); err != nil { - close(jsonMsgs) + close(received) return } - jsonMsgs <- m + received <- m } }() for { select { - case m, ok := <-jsonMsgs: + case m, ok := <-received: if !ok { - // frontend disconnected. + // client disconnected. return } // Handle request here.