github.com/lbryio/lbcd@v0.22.119/rpcclient/examples/lbcdblocknotify/lbcdclient.go (about)

     1  package main
     2  
     3  import (
     4  	"io/ioutil"
     5  	"log"
     6  	"path/filepath"
     7  
     8  	"github.com/lbryio/lbcd/rpcclient"
     9  )
    10  
    11  func newLbcdClient(server, user, pass string, notls bool, adpt adapter) *rpcclient.Client {
    12  
    13  	ntfnHandlers := rpcclient.NotificationHandlers{
    14  		OnFilteredBlockConnected: adpt.onFilteredBlockConnected,
    15  	}
    16  
    17  	// Config lbcd RPC client with websockets.
    18  	connCfg := &rpcclient.ConnConfig{
    19  		Host:       server,
    20  		Endpoint:   "ws",
    21  		User:       user,
    22  		Pass:       pass,
    23  		DisableTLS: true,
    24  	}
    25  
    26  	if !notls {
    27  		cert, err := ioutil.ReadFile(filepath.Join(lbcdHomeDir, "rpc.cert"))
    28  		if err != nil {
    29  			log.Fatalf("can't read lbcd certificate: %s", err)
    30  		}
    31  		connCfg.Certificates = cert
    32  		connCfg.DisableTLS = false
    33  	}
    34  
    35  	client, err := rpcclient.New(connCfg, &ntfnHandlers)
    36  	if err != nil {
    37  		log.Fatalf("can't create rpc client: %s", err)
    38  	}
    39  
    40  	// Register for block connect and disconnect notifications.
    41  	if err = client.NotifyBlocks(); err != nil {
    42  		log.Fatalf("can't register block notification: %s", err)
    43  	}
    44  
    45  	// Get the current block count.
    46  	blockCount, err := client.GetBlockCount()
    47  	if err != nil {
    48  		log.Fatalf("can't get block count: %s", err)
    49  	}
    50  	log.Printf("Current block count: %d", blockCount)
    51  
    52  	return client
    53  }