mirror of
https://github.com/eosswedenorg/thalos-docs
synced 2026-07-03 11:53:42 +02:00
docs/api/examples.md: update go example
This commit is contained in:
parent
7743393c58
commit
278b3a788d
1 changed files with 31 additions and 18 deletions
|
|
@ -14,7 +14,6 @@ import (
|
||||||
"github.com/eosswedenorg/thalos/api/message"
|
"github.com/eosswedenorg/thalos/api/message"
|
||||||
_ "github.com/eosswedenorg/thalos/api/message/json"
|
_ "github.com/eosswedenorg/thalos/api/message/json"
|
||||||
api_redis "github.com/eosswedenorg/thalos/api/redis"
|
api_redis "github.com/eosswedenorg/thalos/api/redis"
|
||||||
|
|
||||||
"github.com/redis/go-redis/v9"
|
"github.com/redis/go-redis/v9"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -27,6 +26,7 @@ func main() {
|
||||||
ChainID: "1064487b3cd1a897ce03ae5b6a865651747e2e152090f99c1d19d44e01aea5a4", // Wax mainnet.
|
ChainID: "1064487b3cd1a897ce03ae5b6a865651747e2e152090f99c1d19d44e01aea5a4", // Wax mainnet.
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Create client
|
||||||
codec, err := message.GetCodec("json")
|
codec, err := message.GetCodec("json")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Failed to get json codec")
|
fmt.Println("Failed to get json codec")
|
||||||
|
|
@ -35,34 +35,47 @@ func main() {
|
||||||
|
|
||||||
client := api.NewClient(sub, codec.Decoder)
|
client := api.NewClient(sub, codec.Decoder)
|
||||||
|
|
||||||
client.OnAction = func(act message.ActionTrace) {
|
// Subscribe to some channels.
|
||||||
fmt.Println("ActionTrace")
|
err = client.Subscribe(
|
||||||
fmt.Println(act)
|
api.TransactionChannel,
|
||||||
fmt.Println("---")
|
api.ActionChannel{Contract: "eosio"}.Channel(),
|
||||||
|
api.ActionChannel{Name: "mine"}.Channel(),
|
||||||
|
api.HeartbeatChannel,
|
||||||
|
api.TableDeltaChannel{}.Channel(),
|
||||||
|
)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
client.OnHeartbeat = func(hb message.HeartBeat) {
|
// Wait for interrupt in a go routine and close the client.
|
||||||
fmt.Println("HeartBeat -- block:", hb.BlockNum, "head:", hb.HeadBlockNum, "lib:", hb.LastIrreversibleBlockNum)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Subscribe to some stuffs.
|
|
||||||
client.Subscribe(api.ActionChannel{Contract: "eosio"}.Channel())
|
|
||||||
client.Subscribe(api.ActionChannel{Name: "mine"}.Channel())
|
|
||||||
client.Subscribe(api.HeartbeatChannel)
|
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
sig := make(chan os.Signal, 1)
|
sig := make(chan os.Signal)
|
||||||
signal.Notify(sig, os.Interrupt)
|
signal.Notify(sig, os.Interrupt)
|
||||||
|
|
||||||
<-sig
|
<-sig
|
||||||
fmt.Println("Got interrupt")
|
fmt.Println("Got interrupt")
|
||||||
|
|
||||||
client.Close()
|
client.Close()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Read stuff.
|
// Read messages
|
||||||
client.Run()
|
for t := range client.Channel() {
|
||||||
|
switch msg := t.(type) {
|
||||||
|
case error:
|
||||||
|
fmt.Println("Error:", msg)
|
||||||
|
case message.TransactionTrace:
|
||||||
|
fmt.Println("Transaction", msg.BlockNum, msg.ID)
|
||||||
|
fmt.Println(msg)
|
||||||
|
fmt.Println("---")
|
||||||
|
case message.HeartBeat :
|
||||||
|
fmt.Println("Heartbeat")
|
||||||
|
fmt.Println(msg)
|
||||||
|
fmt.Println("---")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Nodejs
|
## Nodejs
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue