MCPcopy Create free account
hub / github.com/CPChain/chain / ExampleClientSubscription

Function ExampleClientSubscription

api/rpc/client_example_test.go:41–60  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

39}
40
41func ExampleClientSubscription() {
42 // Connect the client.
43 client, _ := rpc.Dial("ws://127.0.0.1:8485")
44 subch := make(chan Block)
45
46 // Ensure that subch receives the latest block.
47 go func() {
48 for i := 0; ; i++ {
49 if i > 0 {
50 time.Sleep(2 * time.Second)
51 }
52 subscribeBlocks(client, subch)
53 }
54 }()
55
56 // Print events from the subscription as they arrive.
57 for block := range subch {
58 fmt.Println("latest block:", block.Number)
59 }
60}
61
62// subscribeBlocks runs in its own goroutine and maintains
63// a subscription for new blocks.

Callers

nothing calls this directly

Calls 2

subscribeBlocksFunction · 0.85
SleepMethod · 0.80

Tested by

no test coverage detected