Guest User

Untitled

a guest
Dec 28th, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.72 KB | None | 0 0
  1. func Test() (*rpcclient.Client, error) {
  2.     // Only override the handlers for notifications you care about.
  3.     // Also note most of these handlers will only be called if you register
  4.     // for notifications.  See the documentation of the rpcclient.
  5.     // NotificationHandlers type for more details about each handler.
  6.     ntfnHandlers := rpcclient.NotificationHandlers{
  7.         OnBlockConnected: func(blockHeader []byte, transactions [][]byte) {
  8.             log.Printf("Block connected: %v %v", blockHeader, transactions)
  9.         },
  10.         OnBlockDisconnected: func(blockHeader []byte) {
  11.             log.Printf("Block disconnected: %v", blockHeader)
  12.         },
  13.     }
  14.  
  15.     // Connect to local dcrd RPC server using websockets.
  16.     certs, err := ioutil.ReadFile(Env.DcrdCertFilePath)
  17.     if err != nil {
  18.         return nil, err
  19.     }
  20.     connCfg := &rpcclient.ConnConfig{
  21.         Host:         fmt.Sprintf("%s:%d", Env.DcrdHost, Env.DcrdPort),
  22.         Endpoint:     "ws",
  23.         User:         Env.DcrdUser,
  24.         Pass:         Env.DcrdPass,
  25.         Certificates: certs,
  26.     }
  27.     client, err := rpcclient.New(connCfg, &ntfnHandlers)
  28.     if err != nil {
  29.         return nil, err
  30.     }
  31.  
  32.     // Register for block connect and disconnect notifications.
  33.     if err := client.NotifyBlocks(); err != nil {
  34.         return nil, err
  35.     }
  36.  
  37.     c := make(chan os.Signal)
  38.     signal.Notify(c, os.Interrupt, syscall.SIGTERM)
  39.     go func() {
  40.         <-c
  41.         // For this example gracefully shutdown the client after 10 seconds.
  42.         // Ordinarily when to shutdown the client is highly application
  43.         // specific.
  44.         client.Shutdown()
  45.  
  46.         // Wait until the client either shuts down gracefully (or the user
  47.         // terminates the process with Ctrl+C).
  48.         client.WaitForShutdown()
  49.     }()
  50.  
  51.     addr, err := client.GetNewAddress("default")
  52.     log.Println(addr, err) // error here
  53.     return client, nil
  54. }
Add Comment
Please, Sign In to add comment