-
Notifications
You must be signed in to change notification settings - Fork 220
/
main.go
53 lines (48 loc) · 1.39 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"context"
_ "net/http/pprof"
"os"
"github.com/bitfinexcom/bitfinex-api-go/pkg/models/trade"
"github.com/bitfinexcom/bitfinex-api-go/v2/websocket"
"github.com/op/go-logging"
)
func main() {
// create a new go-logger instance
var log = logging.MustGetLogger("bfx-websocket")
// create string formatter
var format = logging.MustStringFormatter(
`%{color}%{time:15:04:05.000} %{shortfunc} ▶ %{level:.4s} %{id:03x}%{color:reset} %{message}`,
)
// apply to logging instance
backend := logging.NewLogBackend(os.Stderr, "", 0)
backendFormatter := logging.NewBackendFormatter(backend, format)
logging.SetBackend(backendFormatter)
// create websocket client and pass logger
p := websocket.NewDefaultParameters()
p.Logger = log
client := websocket.NewWithParams(p)
err := client.Connect()
if err != nil {
log.Errorf("could not connect: %s", err.Error())
return
}
for obj := range client.Listen() {
switch obj.(type) {
case error:
log.Errorf("channel closed: %s", obj)
return
case *trade.Trade:
log.Infof("New trade: %+v\n", obj)
case *websocket.InfoEvent:
// Info event confirms connection to the bfx websocket
log.Info("Subscribing to tBTCUSD")
_, err := client.SubscribeTrades(context.Background(), "tBTCUSD")
if err != nil {
log.Infof("could not subscribe to trades: %s", err.Error())
}
default:
log.Infof("MSG RECV: %#v", obj)
}
}
}