eos_ship_client

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 2, 2022 License: MIT Imports: 5 Imported by: 0

README

Eos SHIP Client

Client Implementation of eosio state-history websocket.

Install package
go get -u github.com/eosswedenorg-go/eos-ship-client@latest
Example
package main

import (
    "os"
    "os/signal"
    "log"
    "time"
    "github.com/eoscanada/eos-go/ship"
    shipclient "github.com/eosswedenorg-go/eos-ship-client"
)

func processBlock(block *ship.GetBlocksResultV0) {

    if block.ThisBlock.BlockNum % 100 == 0 {
        log.Printf("Current: %d, Head: %d\n", block.ThisBlock.BlockNum, block.Head.BlockNum)
    }
}

func processTraces(traces []*ship.TransactionTraceV0) {

    for _, trace := range traces {
        log.Println("Trace ID:", trace.ID)
    }
}

func main() {

    // Create done and interrupt channels.
    done := make(chan bool)
    interrupt := make(chan os.Signal, 1)

    // Register interrupt channel to receive interrupt messages
    signal.Notify(interrupt, os.Interrupt)

    client := shipclient.NewClient(0, shipclient.NULL_BLOCK_NUMBER, false)
    client.BlockHandler = processBlock
    client.TraceHandler = processTraces

    err := client.Connect("127.0.0.1:8089")
    if err != nil {
        log.Println(err)
        return
    }

    err = client.SendBlocksRequest()
    if err != nil {
        log.Println(err)
        return
    }

    // Spawn message read loop in another thread.
    go func() {
        for {
            err := client.Read()
            if err != nil {
                log.Print(err.Error())

                // Bail out on socket read error.
                if err.Type == shipclient.ErrSockRead {
                    break
                }
            }
        }

        client.Close()

        // Client exited. signal that we are done.
        done <- true
    }()

    // Enter event loop in main thread
    for {
        select {
        case <-interrupt:
            log.Println("Interrupt, closing")

            // Cleanly close the connection by sending a close message and then
            // waiting (with timeout) for the server to close the connection.
            client.SendCloseMessage()

            select {
                case <-done: log.Println("Closed")
                case <-time.After(time.Second * 4): log.Println("Timeout");
            }
            return
        case <-done:
            log.Println("Closed")
            return
        }
    }
}
Author

Henrik Hautakoski - Sw/eden - henrik@eossweden.org

Documentation

Index

Constants

View Source
const (
	ErrNotConnected = 1
	ErrSockRead     = 2
	ErrSendClose    = 3
	ErrACK          = 4
	ErrParse        = 5
)
View Source
const NULL_BLOCK_NUMBER uint32 = 0xffffffff

Variables

This section is empty.

Functions

This section is empty.

Types

type ShipClient

type ShipClient struct {

	// Block to start receiving notifications on.
	StartBlock uint32

	// Block to end receiving notifications on.
	EndBlock uint32

	// if only irreversible blocks should be sent.
	IrreversibleOnly bool

	// Max number of non-ACKed messages that may be sent.
	MaxMessagesInFlight uint32

	// Callback functions
	BlockHandler  func(*ship.GetBlocksResultV0)
	TraceHandler  func([]*ship.TransactionTraceV0)
	StatusHandler func(*ship.GetStatusResultV0)
	CloseHandler  func()
	// contains filtered or unexported fields
}

func NewClient

func NewClient(startBlock uint32, endBlock uint32, irreversibleOnly bool) *ShipClient

func (*ShipClient) Close

func (this *ShipClient) Close() *ShipClientError

func (*ShipClient) Connect

func (this *ShipClient) Connect(host string) error

func (*ShipClient) IsOpen

func (this *ShipClient) IsOpen() bool

func (*ShipClient) Read

func (this *ShipClient) Read() *ShipClientError

func (*ShipClient) SendBlocksRequest

func (this *ShipClient) SendBlocksRequest() error

func (*ShipClient) SendCloseMessage

func (this *ShipClient) SendCloseMessage() *ShipClientError

type ShipClientError

type ShipClientError struct {
	Type int
	Text string
}

func (*ShipClientError) Error

func (e *ShipClientError) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL