forked from tensam/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitstampwebsocket.go
71 lines (62 loc) · 1.7 KB
/
bitstampwebsocket.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"github.com/toorop/go-pusher"
"log"
)
type BitstampPusherOrderbook struct {
Asks [][]string `json:"asks"`
Bids [][]string `json:"bids"`
}
type BitstampPusherTrade struct {
Price float64 `json:"price"`
Amount float64 `json:"amount"`
ID int64 `json:"id"`
}
const (
BITSTAMP_PUSHER_KEY = "de504dc5763aeef9ff52"
)
func (b *Bitstamp) PusherClient() {
for b.Enabled && b.Websocket {
pusherClient, err := pusher.NewClient(BITSTAMP_PUSHER_KEY)
if err != nil {
log.Printf("%s Unable to connect to Websocket. Error: %s\n", b.GetName(), err)
continue
}
err = pusherClient.Subscribe("live_trades")
if err != nil {
log.Printf("%s Websocket Trade subscription error: %s\n", b.GetName(), err)
}
err = pusherClient.Subscribe("order_book")
if err != nil {
log.Printf("%s Websocket Trade subscription error: %s\n", b.GetName(), err)
}
dataChannelTrade, err := pusherClient.Bind("data")
if err != nil {
log.Printf("%s Websocket Bind error: ", b.GetName(), err)
continue
}
tradeChannelTrade, err := pusherClient.Bind("trade")
if err != nil {
log.Printf("%s Websocket Bind error: ", b.GetName(), err)
continue
}
log.Printf("%s Pusher client connected.\n", b.GetName())
for b.Websocket {
select {
case data := <-dataChannelTrade:
result := BitstampPusherOrderbook{}
err := JSONDecode([]byte(data.Data), &result)
if err != nil {
log.Println(err)
}
case trade := <-tradeChannelTrade:
result := BitstampPusherTrade{}
err := JSONDecode([]byte(trade.Data), &result)
if err != nil {
log.Println(err)
}
log.Printf("%s Pusher trade: Price: %f Amount: %f\n", b.GetName(), result.Price, result.Amount)
}
}
}
}