[lbry] rpc: add a blocknotify example using lbcd websocket

This commit is contained in:
Roy Lee 2022-01-04 09:52:39 -08:00
parent 0f48f40cd7
commit 1e6d53b950
2 changed files with 101 additions and 0 deletions

View file

@ -0,0 +1,34 @@
lbcd Websockets Example
=======================
This example shows how to use the rpcclient package to connect to a btcd RPC
server using TLS-secured websockets, register for block connected and block
disconnected notifications, and get the current block count.
## Running the Example
The first step is to use `go get` to download and install the rpcclient package:
```bash
$ go get github.com/lbryio/lbcd/rpcclient
```
Next, modify the `main.go` source to specify the correct RPC username and
password for the RPC server:
```Go
User: "yourrpcuser",
Pass: "yourrpcpass",
```
Finally, navigate to the example's directory and run it with:
```bash
$ git clone github.com/lbryio/lbcd
$ cd rpcclient/examples/lbcdblocknotify
$ go run .
```
## License
This example is licensed under the [copyfree](http://copyfree.org) ISC License.

View file

@ -0,0 +1,67 @@
// Copyright (c) 2014-2017 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"io/ioutil"
"log"
"path/filepath"
"github.com/lbryio/lbcd/rpcclient"
"github.com/lbryio/lbcd/wire"
"github.com/lbryio/lbcutil"
)
func main() {
// Only override the handlers for notifications you care about.
// Also note most of these handlers will only be called if you register
// for notifications. See the documentation of the rpcclient
// NotificationHandlers type for more details about each handler.
ntfnHandlers := rpcclient.NotificationHandlers{
OnFilteredBlockConnected: func(height int32, header *wire.BlockHeader, txns []*lbcutil.Tx) {
log.Printf("Block connected: %v (%d) %v",
header.BlockHash(), height, header.Timestamp)
},
OnFilteredBlockDisconnected: func(height int32, header *wire.BlockHeader) {
log.Printf("Block disconnected: %v (%d) %v",
header.BlockHash(), height, header.Timestamp)
},
}
// Connect to local lbcd RPC server using websockets.
lbcdHomeDir := lbcutil.AppDataDir("lbcd", false)
certs, err := ioutil.ReadFile(filepath.Join(lbcdHomeDir, "rpc.cert"))
if err != nil {
log.Fatal(err)
}
connCfg := &rpcclient.ConnConfig{
Host: "localhost:9245",
Endpoint: "ws",
User: "rpcuser",
Pass: "rpcpass",
Certificates: certs,
}
client, err := rpcclient.New(connCfg, &ntfnHandlers)
if err != nil {
log.Fatalln(err)
}
// Register for block connect and disconnect notifications.
if err := client.NotifyBlocks(); err != nil {
log.Fatalln(err)
}
log.Println("NotifyBlocks: Registration Complete")
// Get the current block count.
blockCount, err := client.GetBlockCount()
if err != nil {
log.Fatal(err)
}
log.Printf("Block count: %d", blockCount)
// Wait until the client either shuts down gracefully (or the user
// terminates the process with Ctrl+C).
client.WaitForShutdown()
}