-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert go_kraken/websocket package (#21)
* remove socket address * rework kraken_websocket.go * remove go_kraken * delete .vscode * clean and update feed_service * update README * rename intertal/ports/socket.go
- Loading branch information
1 parent
09069ff
commit 71ec31c
Showing
10 changed files
with
191 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"daemon_endpoint":"127.0.0.1:9000","kraken_ws_endpoint":"ws.kraken.com","markets":[{"base_asset":"5ac9f65c0efcc4775e0baec4ec03abdde22473cd3cf33c0419ca290e0751b225","quote_asset":"91b988204bfb8568d81d7c2962c5f37d6e413e5efaa36c417d245ba2237e8320","kraken_ticker":"LTC/USDT","interval":500}]} | ||
{"daemon_endpoint":"127.0.0.1:9000","markets":[{"base_asset":"5ac9f65c0efcc4775e0baec4ec03abdde22473cd3cf33c0419ca290e0751b225","quote_asset":"25e4fac326a58791c64e31b29d446ac6a6ea5f52ab3a62b12596c7abc34f35a6","kraken_ticker":"LTC/USDT","interval":500}]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
{ | ||
"daemon_endpoint":"127.0.0.1:9000", | ||
"kraken_ws_endpoint":"ws.kraken.com", | ||
"markets":[{ | ||
"base_asset":"5ac9f65c0efcc4775e0baec4ec03abdde22473cd3cf33c0419ca290e0751b225", | ||
"quote_asset":"e3ec88b0ed9228c3337f746f5d9ea20c1e68bbe1f73d94621c1e8452bebb9e22", | ||
"kraken_ticker":"LTC/USDT", | ||
"interval":500 | ||
}] | ||
} | ||
"daemon_endpoint": "127.0.0.1:9000", | ||
"markets": [ | ||
{ | ||
"base_asset": "5ac9f65c0efcc4775e0baec4ec03abdde22473cd3cf33c0419ca290e0751b225", | ||
"quote_asset": "e3ec88b0ed9228c3337f746f5d9ea20c1e68bbe1f73d94621c1e8452bebb9e22", | ||
"kraken_ticker": "LTC/USDT", | ||
"interval": 500 | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,95 @@ | ||
package ports | ||
|
||
import ( | ||
"errors" | ||
"net/url" | ||
|
||
ws "github.com/aopoltorzhicky/go_kraken/websocket" | ||
"github.com/gorilla/websocket" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
// KrakenWebSocket is the interface to manage kraken web socket streams | ||
type KrakenWebSocket interface { | ||
Connect(address string, tickersToSubscribe []string) error | ||
StartListen() (chan TickerWithPrice, error) | ||
Close() error | ||
Connect() error | ||
Start() (chan TickerWithPrice, error) | ||
Stop() error | ||
} | ||
|
||
type krakenWebSocket struct { | ||
krakenWS *ws.Client | ||
krakenWebSocketConn *websocket.Conn | ||
tickerWithPriceChan chan TickerWithPrice | ||
tickersToSubscribe []string | ||
isListening bool | ||
} | ||
|
||
// NewKrakenWebSocket is a factory function for KrakenWebSocket interface | ||
func NewKrakenWebSocket() KrakenWebSocket { | ||
func NewKrakenWebSocket(tickersToSubscribe []string) KrakenWebSocket { | ||
return &krakenWebSocket{ | ||
krakenWS: ws.New(), | ||
krakenWebSocketConn: nil, | ||
tickerWithPriceChan: make(chan TickerWithPrice), | ||
tickersToSubscribe: tickersToSubscribe, | ||
isListening: false, | ||
} | ||
} | ||
|
||
// Connect method will connect to the websocket kraken server, ping it and subscribe to tickers threads. | ||
func (socket *krakenWebSocket) Connect(address string, tickersToSubscribe []string) error { | ||
func (socket *krakenWebSocket) Connect() error { | ||
// connect to server | ||
err := socket.krakenWS.Connect() | ||
if err != nil { | ||
return err | ||
} | ||
// test if the server is alive | ||
err = socket.krakenWS.Ping() | ||
url := url.URL{Scheme: "wss", Host: krakenWebSocketURL, Path: "/"} | ||
websocketConn, _, err := websocket.DefaultDialer.Dial(url.String(), nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// subscribe to tickers | ||
err = socket.krakenWS.SubscribeTicker(tickersToSubscribe) | ||
socket.krakenWebSocketConn = websocketConn | ||
subscribeMsg := createSubscribeToMarketMessage(socket.tickersToSubscribe) | ||
err = sendRequestMessage(socket.krakenWebSocketConn, subscribeMsg) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (socket *krakenWebSocket) StartListen() (chan TickerWithPrice, error) { | ||
if socket.krakenWS == nil { | ||
return nil, errors.New("Socket not connected") | ||
func (socket *krakenWebSocket) Start() (chan TickerWithPrice, error) { | ||
go socket.listen() | ||
return socket.tickerWithPriceChan, nil | ||
} | ||
|
||
func (socket *krakenWebSocket) listen() { | ||
if socket.krakenWebSocketConn == nil { | ||
return | ||
} | ||
|
||
if socket.isListening { | ||
return | ||
} | ||
|
||
go func() { | ||
for obj := range socket.krakenWS.Listen() { | ||
switch obj := obj.(type) { | ||
case error: | ||
log.Debug("Channel closed: ", obj) | ||
case ws.DataUpdate: | ||
tickerUpdate, ok := obj.Data.(ws.TickerUpdate) | ||
if ok { | ||
result := TickerWithPrice{ | ||
Ticker: tickerUpdate.Pair, | ||
Price: tickerUpdate.Close.Today.(float64), | ||
} | ||
socket.tickerWithPriceChan <- result | ||
} | ||
socket.isListening = true | ||
|
||
for { | ||
_, message, err := socket.krakenWebSocketConn.ReadMessage() | ||
if err != nil { | ||
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) { | ||
log.Warn("error: ", err) | ||
} | ||
socket.isListening = false | ||
break | ||
} | ||
}() | ||
|
||
return socket.tickerWithPriceChan, nil | ||
tickerWithPrice, err := toTickerWithPrice(message) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
socket.tickerWithPriceChan <- *tickerWithPrice | ||
} | ||
} | ||
|
||
func (socket *krakenWebSocket) Close() error { | ||
socket.krakenWS.Close() | ||
socket.krakenWS = nil | ||
func (socket *krakenWebSocket) Stop() error { | ||
err := socket.krakenWebSocketConn.Close() | ||
if err != nil { | ||
return err | ||
} | ||
socket.krakenWebSocketConn = nil | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.