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.
This commit is contained in:
Josh Rickmar 2014-02-18 15:51:06 -05:00
parent e837ca5b64
commit 2ff7f85b0c

View file

@ -302,17 +302,21 @@ func NotifyBtcdConnection(reply chan []byte) {
// forever (until disconnected), reading JSON-RPC requests and sending // forever (until disconnected), reading JSON-RPC requests and sending
// sending responses and notifications. // sending responses and notifications.
func WSSendRecv(ws *websocket.Conn) { func WSSendRecv(ws *websocket.Conn) {
// Add frontend notification channel to set so this handler receives // Add client context so notifications duplicated to each
// updates. // client are received by this client.
cc := clientContext{ cc := clientContext{
send: make(chan []byte), send: make(chan []byte, 1), // buffer size is number of initial notifications
disconnected: make(chan struct{}), disconnected: make(chan struct{}),
} }
NotifyBtcdConnection(cc.send) // TODO(jrick): clients should explicitly request this.
addClient <- cc addClient <- cc
defer close(cc.disconnected) defer close(cc.disconnected)
// jsonMsgs receives JSON messages from the currently connected frontend. // received passes all received messages from the currently connected
jsonMsgs := make(chan []byte) // 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 // Receive messages from websocket and send across jsonMsgs until
// connection is lost // connection is lost
@ -320,18 +324,18 @@ func WSSendRecv(ws *websocket.Conn) {
for { for {
var m []byte var m []byte
if err := websocket.Message.Receive(ws, &m); err != nil { if err := websocket.Message.Receive(ws, &m); err != nil {
close(jsonMsgs) close(received)
return return
} }
jsonMsgs <- m received <- m
} }
}() }()
for { for {
select { select {
case m, ok := <-jsonMsgs: case m, ok := <-received:
if !ok { if !ok {
// frontend disconnected. // client disconnected.
return return
} }
// Handle request here. // Handle request here.