github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/rpc/client_example_test.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package rpc_test
    13  
    14  import (
    15  	"context"
    16  	"fmt"
    17  	"math/big"
    18  	"time"
    19  
    20  	"github.com/Sberex/go-sberex/rpc"
    21  )
    22  
    23  // In this example, our client whishes to track the latest 'block number'
    24  // known to the server. The server supports two methods:
    25  //
    26  // eth_getBlockByNumber("latest", {})
    27  //    returns the latest block object.
    28  //
    29  // eth_subscribe("newBlocks")
    30  //    creates a subscription which fires block objects when new blocks arrive.
    31  
    32  type Block struct {
    33  	Number *big.Int
    34  }
    35  
    36  func ExampleClientSubscription() {
    37  	// Connect the client.
    38  	client, _ := rpc.Dial("ws://127.0.0.1:8485")
    39  	subch := make(chan Block)
    40  
    41  	// Ensure that subch receives the latest block.
    42  	go func() {
    43  		for i := 0; ; i++ {
    44  			if i > 0 {
    45  				time.Sleep(2 * time.Second)
    46  			}
    47  			subscribeBlocks(client, subch)
    48  		}
    49  	}()
    50  
    51  	// Print events from the subscription as they arrive.
    52  	for block := range subch {
    53  		fmt.Println("latest block:", block.Number)
    54  	}
    55  }
    56  
    57  // subscribeBlocks runs in its own goroutine and maintains
    58  // a subscription for new blocks.
    59  func subscribeBlocks(client *rpc.Client, subch chan Block) {
    60  	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    61  	defer cancel()
    62  
    63  	// Subscribe to new blocks.
    64  	sub, err := client.EthSubscribe(ctx, subch, "newBlocks")
    65  	if err != nil {
    66  		fmt.Println("subscribe error:", err)
    67  		return
    68  	}
    69  
    70  	// The connection is established now.
    71  	// Update the channel with the current block.
    72  	var lastBlock Block
    73  	if err := client.CallContext(ctx, &lastBlock, "eth_getBlockByNumber", "latest"); err != nil {
    74  		fmt.Println("can't get latest block:", err)
    75  		return
    76  	}
    77  	subch <- lastBlock
    78  
    79  	// The subscription will deliver events to the channel. Wait for the
    80  	// subscription to end for any reason, then loop around to re-establish
    81  	// the connection.
    82  	fmt.Println("connection lost: ", <-sub.Err())
    83  }