forked from nntaoli-project/goex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket_test.go
54 lines (44 loc) · 1.22 KB
/
websocket_test.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
package goex
import (
"bytes"
"compress/gzip"
"encoding/json"
"io/ioutil"
"testing"
"time"
)
func Test_time(t *testing.T) {
t.Log(time.Now().Unix())
}
func TestNewWsConn(t *testing.T) {
//os.Setenv("https_proxy" , "socks5://127.0.0.1:1080")
ws := NewWsConn("wss://api.huobipro.com/ws")
//ws := NewWsConn("wss://real.okex.com:10441/websocket")
time.Sleep(time.Second)
ws.Heartbeat(func() interface{} {
return map[string]interface{}{"ping": time.Duration(time.Now().Nanosecond())}
}, 5*time.Second)
ws.ReConnect()
ws.Subscribe(map[string]string{
"sub": "market.btcusdt.detail",
"id": "2"})
ws.Subscribe(map[string]string{
"sub": "market.btcusdt.depth.step0",
"id": "1"})
//ws.WriteJSON(map[string]string{"event": "addChannel", "channel": "ok_sub_spot_bch_btc_ticker"})
ws.ReceiveMessage(func(msg []byte) {
println("receive message...")
gzipreader, _ := gzip.NewReader(bytes.NewReader(msg))
data, _ := ioutil.ReadAll(gzipreader)
var resp map[string]interface{}
json.Unmarshal(data, &resp)
if resp["ping"] != nil {
ws.WriteJSON(map[string]interface{}{"pong": resp["ping"]})
ws.actived = time.Now()
}
println(string(data))
})
time.Sleep(2 * time.Minute)
ws.CloseWs()
time.Sleep(time.Second)
}