From 2b25a5f98fe8bd65675b78b0769f04f7f6c3bc6a Mon Sep 17 00:00:00 2001 From: Adrian Gallagher Date: Tue, 24 Mar 2015 18:29:56 +1100 Subject: [PATCH] Added Websocket (Pusher) support for Cryptsy. --- config_example.json | 2 +- cryptsyhttp.go | 5 +++ cryptsywebsocket.go | 92 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 cryptsywebsocket.go diff --git a/config_example.json b/config_example.json index 9c3d20c8..171908df 100644 --- a/config_example.json +++ b/config_example.json @@ -87,7 +87,7 @@ "BaseCurrencies": "USD", "Enabled": true, "Verbose": false, - "Websocket": false, + "Websocket": true, "PollingDelay": 10 }, { diff --git a/cryptsyhttp.go b/cryptsyhttp.go index e8d0a02a..e7c3f67b 100644 --- a/cryptsyhttp.go +++ b/cryptsyhttp.go @@ -157,9 +157,14 @@ func (c *Cryptsy) GetFee(maker bool) (float64) { func (c *Cryptsy) Run() { if c.Verbose { + log.Printf("%s Websocket: %s.", c.GetName(), IsEnabled(c.Websocket)) log.Printf("%s polling delay: %ds.\n", c.GetName(), c.PollingDelay) } + if c.Websocket { + go c.PusherClient([]string{"213", "2", "1", "155", "466", "132", "3"}) + } + for c.Enabled { go func() { CryptsyBTC := c.GetMarkets("BTCUSD") diff --git a/cryptsywebsocket.go b/cryptsywebsocket.go new file mode 100644 index 00000000..eb7bdcac --- /dev/null +++ b/cryptsywebsocket.go @@ -0,0 +1,92 @@ +package main + +import ( + "github.com/toorop/go-pusher" + "strings" + "log" +) + +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(marketID[] string) { + pusherClient, err := pusher.NewClient(CRYPTSY_PUSHER_KEY) + if err != nil { + log.Fatalln(err) + return + } + + for i := 0; i < len(marketID); i++ { + err = pusherClient.Subscribe("trade." + marketID[i]) + + if err != nil { + log.Println("Trade subscription error : ", err) + } + + err = pusherClient.Subscribe("ticker." + marketID[i]) + if err != nil { + log.Println("Ticker subscription error : ", err) + } + } + + dataChannel, err := pusherClient.Bind("message") + if err != nil { + log.Println("Bind error: ", err) + return + } + log.Printf("%s Pusher client ready.\n", c.GetName()) + + for c.Websocket { + select { + case data := <-dataChannel: + if strings.Contains(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) + } + } + } +} \ No newline at end of file