client_example_test.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. "context"
  19. "fmt"
  20. "math/big"
  21. "time"
  22. "github.com/ethereum/go-ethereum/rpc"
  23. )
  24. // In this example, our client whishes to track the latest 'block number'
  25. // known to the server. The server supports two methods:
  26. //
  27. // eth_getBlockByNumber("latest", {})
  28. // returns the latest block object.
  29. //
  30. // eth_subscribe("newBlocks")
  31. // creates a subscription which fires block objects when new blocks arrive.
  32. type Block struct {
  33. Number *big.Int
  34. }
  35. func ExampleClientSubscription() {
  36. // Connect the client.
  37. client, _ := rpc.Dial("ws://127.0.0.1:8485")
  38. subch := make(chan Block)
  39. // Ensure that subch receives the latest block.
  40. go func() {
  41. for i := 0; ; i++ {
  42. if i > 0 {
  43. time.Sleep(2 * time.Second)
  44. }
  45. subscribeBlocks(client, subch)
  46. }
  47. }()
  48. // Print events from the subscription as they arrive.
  49. for block := range subch {
  50. fmt.Println("latest block:", block.Number)
  51. }
  52. }
  53. // subscribeBlocks runs in its own goroutine and maintains
  54. // a subscription for new blocks.
  55. func subscribeBlocks(client *rpc.Client, subch chan Block) {
  56. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  57. defer cancel()
  58. // Subscribe to new blocks.
  59. sub, err := client.EthSubscribe(ctx, subch, "newBlocks")
  60. if err != nil {
  61. fmt.Println("subscribe error:", err)
  62. return
  63. }
  64. // The connection is established now.
  65. // Update the channel with the current block.
  66. var lastBlock Block
  67. if err := client.CallContext(ctx, &lastBlock, "eth_getBlockByNumber", "latest"); err != nil {
  68. fmt.Println("can't get latest block:", err)
  69. return
  70. }
  71. subch <- lastBlock
  72. // The subscription will deliver events to the channel. Wait for the
  73. // subscription to end for any reason, then loop around to re-establish
  74. // the connection.
  75. fmt.Println("connection lost: ", <-sub.Err())
  76. }