client_example_test.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package rpc_test
  17. import (
  18. "fmt"
  19. "math/big"
  20. "time"
  21. "github.com/ethereum/go-ethereum/rpc"
  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. type Block struct {
  32. Number *big.Int
  33. }
  34. func ExampleClientSubscription() {
  35. // Connect the client.
  36. client, _ := rpc.Dial("ws://127.0.0.1:8485")
  37. subch := make(chan Block)
  38. go subscribeBlocks(client, subch)
  39. // Print events from the subscription as they arrive.
  40. for block := range subch {
  41. fmt.Println("latest block:", block.Number)
  42. }
  43. }
  44. // subscribeBlocks runs in its own goroutine and maintains
  45. // a subscription for new blocks.
  46. func subscribeBlocks(client *rpc.Client, subch chan Block) {
  47. for i := 0; ; i++ {
  48. if i > 0 {
  49. time.Sleep(2 * time.Second)
  50. }
  51. // Subscribe to new blocks.
  52. sub, err := client.EthSubscribe(subch, "newBlocks")
  53. if err == rpc.ErrClientQuit {
  54. return // Stop reconnecting if the client was closed.
  55. } else if err != nil {
  56. fmt.Println("subscribe error:", err)
  57. continue
  58. }
  59. // The connection is established now.
  60. // Update the channel with the current block.
  61. var lastBlock Block
  62. if err := client.Call(&lastBlock, "eth_getBlockByNumber", "latest"); err != nil {
  63. fmt.Println("can't get latest block:", err)
  64. continue
  65. }
  66. subch <- lastBlock
  67. // The subscription will deliver events to the channel. Wait for the
  68. // subscription to end for any reason, then loop around to re-establish
  69. // the connection.
  70. fmt.Println("connection lost: ", <-sub.Err())
  71. }
  72. }