-
Notifications
You must be signed in to change notification settings - Fork 3
/
ws_trade_service.go
90 lines (76 loc) · 1.6 KB
/
ws_trade_service.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
package phemex
import (
"encoding/json"
"fmt"
"github.com/gorilla/websocket"
"strings"
)
// StartWsTradeService :
type StartWsTradeService struct {
c *Client
id *int64
symbols []string
}
// ID :
func (s *StartWsTradeService) ID(id int64) *StartWsTradeService {
s.id = &id
return s
}
// Symbols :
func (s *StartWsTradeService) Symbols(symbols []string) *StartWsTradeService {
s.symbols = symbols
return s
}
// Do :
func (s *StartWsTradeService) Do(c *websocket.Conn, handler WsHandler, errHandler ErrHandler, opts ...RequestOption) (err error) {
stop := make(chan struct{})
if c == nil {
return fmt.Errorf("the connection is not initialized (%v)", *s.id)
}
go func() {
defer close(stop)
if WebsocketKeepalive {
keepAlive(c, *s.id, stop, errHandler)
}
err = c.WriteJSON(map[string]interface{}{
"id": *s.id,
"method": "trade.subscribe",
"params": s.symbols,
})
if err != nil {
errHandler(err)
return
}
for {
_, msg, err := c.ReadMessage()
if err != nil {
errHandler(err)
return
}
var resp interface{}
switch {
case strings.HasPrefix(string(msg), "{\"error\""):
resp = new(WsError)
default:
resp = new(WsTradeEvent)
}
err = json.Unmarshal(msg, &resp)
if err != nil {
errHandler(err)
return
}
handler(resp)
}
}()
return
}
// WsTradeEvent ws WsTradeEvent
type WsTradeEvent struct {
Trade []WsTrade `json:"trades"`
Sequence int `json:"sequence"`
Symbol string `json:"symbol"`
Timestamp int `json:"timestamp"`
Type string `json:"type"`
}
type WsTrade struct {
}