github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/examples/v2/book-feed/main.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"log"
     6  	"net/http"
     7  	_ "net/http/pprof"
     8  	"os"
     9  	"os/signal"
    10  
    11  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/common"
    12  	"github.com/bitfinexcom/bitfinex-api-go/v2/websocket"
    13  )
    14  
    15  func main() {
    16  	client := websocket.New()
    17  	err := client.Connect()
    18  	if err != nil {
    19  		log.Printf("could not connect: %s", err.Error())
    20  		return
    21  	}
    22  	go func() {
    23  		for msg := range client.Listen() {
    24  			log.Printf("recv: %#v", msg)
    25  			if _, ok := msg.(*websocket.InfoEvent); ok {
    26  				_, err := client.SubscribeBook(context.Background(), "BTCUSD", common.Precision0, common.FrequencyRealtime, 1)
    27  				if err != nil {
    28  					log.Printf("could not subscribe to book: %s", err.Error())
    29  				}
    30  			}
    31  		}
    32  	}()
    33  	done := make(chan bool, 1)
    34  	interrupt := make(chan os.Signal)
    35  	signal.Notify(interrupt, os.Interrupt, os.Kill)
    36  	go func() {
    37  		log.Println(http.ListenAndServe("localhost:6060", nil))
    38  	}()
    39  	go func() {
    40  		<-interrupt
    41  		client.Close()
    42  		done <- true
    43  		os.Exit(0)
    44  	}()
    45  	<-done
    46  }