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:
parent
e837ca5b64
commit
2ff7f85b0c
1 changed files with 13 additions and 9 deletions
22
sockets.go
22
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.
|
||||
|
|
Loading…
Reference in a new issue