-
Notifications
You must be signed in to change notification settings - Fork 205
/
Copy pathmain.go
64 lines (60 loc) · 2.09 KB
/
main.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
package main
import (
"cryptochassis.com/ccapi"
"fmt"
"time"
)
type MyEventHandler struct {
ccapi.EventHandler
}
func (*MyEventHandler) ProcessEvent(event ccapi.Event, session ccapi.Session) bool {
if event.GetType() == ccapi.EventType_SUBSCRIPTION_STATUS {
fmt.Printf("Received an event of type SUBSCRIPTION_STATUS:\n%s\n", event.ToStringPretty(2, 2))
} else if event.GetType() == ccapi.EventType_SUBSCRIPTION_DATA {
messageList := event.GetMessageList()
for i := 0; i < int(messageList.Size()); i++ {
message := messageList.Get(i)
fmt.Printf("Best bid and ask at %s are:\n", message.GetTimeISO())
elementList := message.GetElementList()
for j := 0; j < int(elementList.Size()); j++ {
element := elementList.Get(j)
elementNameValueMap := element.GetNameValueMap()
if elementNameValueMap.Has_key("BID_PRICE") {
fmt.Printf(" BID_PRICE = %s\n", elementNameValueMap.Get("BID_PRICE"))
}
if elementNameValueMap.Has_key("BID_SIZE") {
fmt.Printf(" BID_SIZE = %s\n", elementNameValueMap.Get("BID_SIZE"))
}
if elementNameValueMap.Has_key("ASK_PRICE") {
fmt.Printf(" ASK_PRICE = %s\n", elementNameValueMap.Get("ASK_PRICE"))
}
if elementNameValueMap.Has_key("ASK_SIZE") {
fmt.Printf(" ASK_SIZE = %s\n", elementNameValueMap.Get("ASK_SIZE"))
}
}
}
}
return true
}
func main() {
option := ccapi.NewSessionOptions()
defer ccapi.DeleteSessionOptions(option)
config := ccapi.NewSessionConfigs()
defer ccapi.DeleteSessionConfigs(config)
meh := &MyEventHandler{}
meh.EventHandler = ccapi.NewDirectorEventHandler(meh)
defer func() {
ccapi.DeleteDirectorEventHandler(meh.EventHandler)
}()
session := ccapi.NewSession(option, config, meh.EventHandler)
defer ccapi.DeleteSession(session)
subscriptionList := ccapi.NewSubscriptionList()
defer ccapi.DeleteSubscriptionList(subscriptionList)
subscription := ccapi.NewSubscription("okx", "BTC-USDT", "MARKET_DEPTH")
defer ccapi.DeleteSubscription(subscription)
subscriptionList.Add(subscription)
session.Subscribe(subscriptionList)
time.Sleep(10 * time.Second)
session.Stop()
fmt.Println("Bye")
}