/
nwsclient_cr.go
115 lines (107 loc) · 2.85 KB
/
nwsclient_cr.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
*
* @author nghiatc
* @since Aug 8, 2018
*/
package nwsc
import (
"fmt"
"github.com/congnghia0609/ntc-gsuperwss/nwss"
"github.com/congnghia0609/ntc-gsuperwss/util"
"log"
"time"
"github.com/congnghia0609/ntc-gconf/nconf"
"github.com/gorilla/websocket"
)
func (nwsc *NWSClient) recvCR() {
util.TCF{
Try: func() {
defer nwsc.Close()
defer close(nwsc.done)
for {
_, message, err := nwsc.conn.ReadMessage()
if err != nil {
log.Println("read:", err)
nwsc.Reconnect()
// return
}
log.Printf("recvCR: %s", message)
if len(message) > 0 {
// CRNWSServer
crnwss := nwss.GetInstanceCR(nwss.NameCRNWSS)
if crnwss != nil {
crnwss.BroadcastMsgByte(message)
}
}
}
},
Catch: func(e util.Exception) {
log.Printf("nwsc.recvCR Caught %v\n", e)
},
Finally: func() {
//log.Println("Finally...")
},
}.Do()
}
func (nwsc *NWSClient) sendCR() {
util.TCF{
Try: func() {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case t := <-ticker.C:
//err := nws.conn.WriteMessage(websocket.TextMessage, []byte(t.String()))
msec := t.UnixNano() / 1000000
///// 1. Candlesticks Data.
data := `{"tt":"1h","s":"ETH_BTC","t":` + fmt.Sprint(msec) + `,"e":"kline","k":{"c":"0.00028022","t":1533715200000,"v":"905062.00000000","h":"0.00028252","l":"0.00027787","o":"0.00027919"}}`
err := nwsc.conn.WriteMessage(websocket.TextMessage, []byte(data))
if err != nil {
log.Println("write:", err)
//return
}
case <-nwsc.interrupt:
log.Println("interrupt")
// To cleanly close a connection, a client should send a close
// frame and wait for the server to close the connection.
err := nwsc.conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
if err != nil {
log.Println("write close:", err)
return
}
select {
case <-nwsc.done:
case <-time.After(time.Second):
}
nwsc.Close()
return
}
}
},
Catch: func(e util.Exception) {
log.Printf("nwsc.sendCR Caught %v\n", e)
},
Finally: func() {
//log.Println("Finally...")
},
}.Do()
}
// NewCRNWSClient new instance of NWSClient
func NewCRNWSClient() *NWSClient {
var crnwsc *NWSClient
c := nconf.GetConfig()
scheme := c.GetString(NameCRWSC + ".nwsc.scheme")
address := c.GetString(NameCRWSC + ".nwsc.host")
path := c.GetString(NameCRWSC + ".nwsc.path")
log.Printf("################ CRNWSClient[%s] start...", NameCRWSC)
crnwsc, _ = NewInstanceWSC(NameCRWSC, scheme, address, path)
// crnwsc, _ = NewInstanceWSC(NameCRWSC, "wss", "stream.streetcodevn.com", "/ws/v1/cr/btcusdt")
return crnwsc
}
// StartCRNWSClient start
func (nwsc *NWSClient) StartCRNWSClient() {
// Thread receive message.
go nwsc.recvCR()
// Thread send message.
//go nwsc.sendCR()
}