forked from nntaoli-project/goex
-
Notifications
You must be signed in to change notification settings - Fork 4
/
websocket_test.go
51 lines (42 loc) · 1.18 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
package goex
import (
"encoding/json"
. "github.com/nntaoli-project/goex/internal/logger"
"testing"
"time"
)
func Test_time(t *testing.T) {
t.Log(time.Now().Unix())
}
func ProtoHandle(data []byte) error {
println(string(data))
return nil
}
func TestNewWsConn(t *testing.T) {
Log.SetLevel(DEBUG)
clientId := "a"
args := make([]interface{}, 0)
var heartbeatFunc = func() []byte {
ts := time.Now().Unix()*1000 + 42029
args = append(args, ts)
//ping := fmt.Sprintf("{\"cmd\":\"ping\",\"args\":[%d],\"id\":\"%s\"}", ts, clientId)
ping2 := map[string]interface{}{
"cmd": "ping",
"id": clientId,
"args": args}
ping3, _ := json.Marshal(ping2)
return ping3
}
//fmt.Println(ping)
//fmt.Println(ping2)
//fmt.Println(err, string(ping3))
ws := NewWsBuilder().Dump().WsUrl("wss://api.fcoin.com/v2/ws").
ProxyUrl("socks5://127.0.0.1:1080").AutoReconnect().
Heartbeat(heartbeatFunc, 5*time.Second).ProtoHandleFunc(ProtoHandle).Build()
t.Log(ws.Subscribe(map[string]string{
//"cmd":"sub", "args":"[\"ticker.btcusdt\"]", "id": clientId}))
"cmd":"sub", "args":"ticker.btcusdt", "id": clientId}))
time.Sleep(time.Second * 20)
ws.c.Close()
time.Sleep(time.Second*120)
}