Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

Commit

Permalink
docs: update json examples
Browse files Browse the repository at this point in the history
  • Loading branch information
dajohi committed May 18, 2017
1 parent 0d48ca0 commit f54151c
Showing 1 changed file with 82 additions and 66 deletions.
148 changes: 82 additions & 66 deletions docs/json_rpc_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2398,7 +2398,7 @@ Num transactions: 1
Notifications (Websocket-specific)**<br />

The following is an example Go application which uses the
[btcrpcclient](https://github.com/btcsuite/btcrpcclient) package to connect with
[dcrrpcclient](https://github.com/decred/dcrrpcclient) package to connect with
a btcd instance via Websockets and registers for
[blockconnected](#blockconnected) and [blockdisconnected](#blockdisconnected)
notifications with [notifyblocks](#notifyblocks). It also sets up handlers for
Expand All @@ -2408,69 +2408,85 @@ the notifications.
package main

import (
"github.com/btcsuite/btcrpcclient"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"io/ioutil"
"log"
"path/filepath"
"time"
"io/ioutil"
"log"
"path/filepath"
"time"

"github.com/decred/dcrd/wire"
"github.com/decred/dcrrpcclient"
"github.com/decred/dcrutil"
)

func main() {
// Setup handlers for blockconnected and blockdisconnected
// notifications.
ntfnHandlers := btcrpcclient.NotificationHandlers{
OnBlockConnected: func(hash *chainhash.Hash, height int32) {
log.Printf("Block connected: %v (%d)", hash, height)
},
OnBlockDisconnected: func(hash *chainhash.Hash, height int32) {
log.Printf("Block disconnected: %v", hash, height)
},
}

// Load the certificate for the TLS connection which is automatically
// generated by btcd when it starts the RPC server and doesn't already
// have one.
btcdHomeDir := btcutil.AppDataDir("btcd", false)
certs, err := ioutil.ReadFile(filepath.Join(btcdHomeDir, "rpc.cert"))
if err != nil {
log.Fatal(err)
}

// Create a new RPC client using websockets.
connCfg := &btcrpcclient.ConnConfig{
Host: "localhost:8334",
Endpoint: "ws",
User: "yourrpcuser",
Pass: "yourrpcpass",
Certificates: certs,
}
client, err := btcrpcclient.New(connCfg, &ntfnHandlers)
if err != nil {
log.Fatal(err)
}

// Register for blockconnected and blockdisconneted notifications.
if err := client.NotifyBlocks(); err != nil {
client.Shutdown()
log.Fatal(err)
}

// For this example, gracefully shutdown the client after 10 seconds.
// Ordinarily when to shutdown the client is highly application
// specific.
log.Println("Client shutdown in 10 seconds...")
time.AfterFunc(time.Second*10, func() {
log.Println("Client shutting down...")
client.Shutdown()
log.Println("Client shutdown complete.")
})

// Wait until the client either shuts down gracefully (or the user
// terminates the process with Ctrl+C).
client.WaitForShutdown()
// Setup handlers for blockconnected and blockdisconnected
// notifications.
ntfnHandlers := dcrrpcclient.NotificationHandlers{
OnBlockConnected: func(blockHeaderBytes []byte, transactions [][]byte) {
var bh wire.BlockHeader
err := bh.FromBytes(blockHeaderBytes)
if err != nil {
log.Printf("Unable to deserialize "+
"block header: %v", err)
return
}
log.Printf("Block connected: %v (%d)",
bh.BlockHash(), bh.Height)
},
OnBlockDisconnected: func(blockHeaderBytes []byte) {
var bh wire.BlockHeader
err := bh.FromBytes(blockHeaderBytes)
if err != nil {
log.Printf("Unable to deserialize "+
"block header: %v", err)
return
}
log.Printf("Block disconnected: %v (%d)",
bh.BlockHash(), bh.Height)
},
}

// Load the certificate for the TLS connection which is automatically
// generated by dcrd when it starts the RPC server and doesn't already
// have one.
dcrdHomeDir := dcrutil.AppDataDir("dcrd", false)
certs, err := ioutil.ReadFile(filepath.Join(dcrdHomeDir, "rpc.cert"))
if err != nil {
log.Fatal(err)
}

// Create a new RPC client using websockets.
connCfg := &dcrrpcclient.ConnConfig{
Host: "localhost:8334",
Endpoint: "ws",
User: "yourrpcuser",
Pass: "yourrpcpass",
Certificates: certs,
}
client, err := dcrrpcclient.New(connCfg, &ntfnHandlers)
if err != nil {
log.Fatal(err)
}

// Register for blockconnected and blockdisconneted notifications.
if err := client.NotifyBlocks(); err != nil {
client.Shutdown()
log.Fatal(err)
}

// For this example, gracefully shutdown the client after 10 seconds.
// Ordinarily when to shutdown the client is highly application
// specific.
log.Println("Client shutdown in 10 seconds...")
time.AfterFunc(time.Second*10, func() {
log.Println("Client shutting down...")
client.Shutdown()
log.Println("Client shutdown complete.")
})

// Wait until the client either shuts down gracefully (or the user
// terminates the process with Ctrl+C).
client.WaitForShutdown()
}
```

Expand All @@ -2490,7 +2506,7 @@ Example output:
**9.2.1 Using notifyblocks to be Notified of Block Connects and Disconnects**<br />

The following is example node.js code which uses [ws](https://github.com/einaros/ws)
(can be installed with `npm install ws`) to connect with a btcd instance,
(can be installed with `npm install ws`) to connect with a dcrd instance,
issues [notifyblocks](#notifyblocks) to register for
[blockconnected](#blockconnected) and [blockdisconnected](#blockdisconnected)
notifications, and displays all incoming messages.
Expand All @@ -2500,14 +2516,14 @@ var fs = require('fs');
var WebSocket = require('ws');

// Load the certificate for the TLS connection which is automatically
// generated by btcd when it starts the RPC server and doesn't already
// generated by dcrd when it starts the RPC server and doesn't already
// have one.
var cert = fs.readFileSync('/path/to/btcd/appdata/rpc.cert');
var user = "yourusername";
var cert = fs.readFileSync('/path/to/dcrd/appdata/rpc.cert');
Var user = "yourusername";
var password = "yourpassword";


// Initiate the websocket connection. The btcd generated certificate acts as
// Initiate the websocket connection. The dcrd generated certificate acts as
// its own certificate authority, so it needs to be specified in the 'ca' array
// for the certificate to properly validate.
var ws = new WebSocket('wss://127.0.0.1:8334/ws', {
Expand Down

0 comments on commit f54151c

Please sign in to comment.