forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cryptsywebsocket.go
113 lines (99 loc) · 2.76 KB
/
cryptsywebsocket.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"github.com/toorop/go-pusher"
"log"
"time"
)
type CryptsyPusherTrade struct {
Channel string `json:"channel"`
Trade struct {
Datetime string `json:"datetime"`
MarketID string `json:"marketid"`
MarketName string `json:"marketname"`
Price float64 `json:"price,string"`
Quantity float64 `json:"quantity,string"`
Timestamp int64 `json:"timestamp"`
Total string `json:"total"`
Type string `json:"type"`
} `json:"trade"`
}
type CryptsyPusherTicker struct {
Channel string `json:"channel"`
Trade struct {
Datetime string `json:"datetime"`
MarketID string `json:"marketid"`
Timestamp int64 `json:"timestamp"`
TopBuy struct {
Price float64 `json:"price,string"`
Quantitiy float64 `json:"quantity,string"`
} `json:"topbuy"`
TopSell struct {
Price float64 `json:"price,string"`
Quantity float64 `json:"quantity,string"`
} `json:"topsell"`
} `json:"trade"`
}
const (
CRYPTSY_PUSHER_KEY = "cb65d0a7a72cd94adf1f"
)
func (c *Cryptsy) PusherClient() {
for c.Enabled && c.Websocket {
pusherClient, err := pusher.NewClient(CRYPTSY_PUSHER_KEY)
if err != nil {
log.Printf("%s Unable to connect to Websocket. Error: %s\n", c.GetName(), err)
continue
}
if c.Verbose {
log.Printf("%s Pusher client connected.\n", c.GetName())
}
for len(c.Market) == 0 {
time.Sleep(time.Second * 1)
}
marketID := []string{}
for _, x := range c.EnabledPairs {
marketID = append(marketID, c.Market[x].ID)
}
if c.Verbose {
log.Printf("%s Websocket subscribing to %d channels.\n", c.GetName(), len(marketID))
}
for i := 0; i < len(marketID); i++ {
if marketID[i] == "" {
continue
}
err = pusherClient.Subscribe("trade." + marketID[i])
if err != nil {
log.Printf("%s Websocket Trade subscription error: %s\n", c.GetName(), err)
}
err = pusherClient.Subscribe("ticker." + marketID[i])
if err != nil {
log.Printf("%s Websocket Trade subscription error: %s\n", c.GetName(), err)
}
}
dataChannel, err := pusherClient.Bind("message")
if err != nil {
log.Printf("%s Websocket Bind error: ", c.GetName(), err)
continue
}
for c.Enabled && c.Websocket {
select {
case data := <-dataChannel:
if StringContains(data.Data, "topbuy") {
result := CryptsyPusherTicker{}
err := JSONDecode([]byte(data.Data), &result)
if err != nil {
log.Println(err)
continue
}
} else {
result := CryptsyPusherTrade{}
err := JSONDecode([]byte(data.Data), &result)
if err != nil {
log.Println(err)
continue
}
log.Printf("%s Pusher trade - market %s - Price %f Amount %f Type %s\n", c.GetName(), result.Channel, result.Trade.Price, result.Trade.Quantity, result.Trade.Type)
}
}
}
}
}