1
0
Fork 0
mirror of https://github.com/eosswedenorg/thalos synced 2026-06-16 04:24:56 +02:00

api/README.md: update.

This commit is contained in:
Henrik Hautakoski 2024-03-05 15:00:04 +01:00
parent 83d57a09c4
commit 4c843f16bf

View file

@ -2,7 +2,7 @@
## Usage ## Usage
The api is designed with callback functions that are called when messages arrive The api is designed with go channels.
## Example ## Example
@ -31,59 +31,56 @@ func main() {
ChainID: "1064487b3cd1a897ce03ae5b6a865651747e2e152090f99c1d19d44e01aea5a4", // Wax mainnet. ChainID: "1064487b3cd1a897ce03ae5b6a865651747e2e152090f99c1d19d44e01aea5a4", // Wax mainnet.
}) })
// Create client // Create thalos 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")
return return
} }
client := api.NewClient(sub, codec.Decoder) client := api.NewClient(sub, codec.Decoder)
// Subscribe to some channels. // Subscribe to some channels.
err = client.Subscribe( err = client.Subscribe(
api.TransactionChannel, api.TransactionChannel,
api.ActionChannel{Contract: "eosio"}.Channel(), api.ActionChannel{Contract: "eosio"}.Channel(),
api.ActionChannel{Name: "mine"}.Channel(), api.ActionChannel{Name: "mine"}.Channel(),
api.HeartbeatChannel, api.HeartbeatChannel,
api.TableDeltaChannel{}.Channel(), api.TableDeltaChannel{}.Channel(),
) )
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return
} }
// Wait for interrupt in a go routine and close the client. // Wait for interrupt in a go routine and close the client.
go func() { go func() {
sig := make(chan os.Signal) 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 messages // Read messages
for t := range client.Channel() { for t := range client.Channel() {
switch msg := t.(type) { switch msg := t.(type) {
case error: case error:
fmt.Println("Error:", msg) fmt.Println("Error:", msg)
case message.TransactionTrace: case message.TransactionTrace:
fmt.Println("Transaction", msg.BlockNum, msg.ID) fmt.Println("Transaction", msg.BlockNum, msg.ID)
fmt.Println(msg) fmt.Println(msg)
fmt.Println("---") fmt.Println("---")
case message.HeartBeat : case message.HeartBeat :
fmt.Println("Heartbeat") fmt.Println("Heartbeat")
fmt.Println(msg) fmt.Println(msg)
fmt.Println("---") fmt.Println("---")
} }
} }
} }
``` ```
## Message channels and types ## Message channels and types