-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
199 lines (171 loc) · 4.24 KB
/
client.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package steemit
import (
"encoding/json"
"fmt"
"log"
"sync"
"time"
"github.com/gorilla/websocket"
)
const (
// https://steemit.com/witness-category/@bitcoiner/list-of-public-steem-full-api-nodes-and-example-usage
ApiAddr = "wss://steemd.steemit.com"
TimeoutSeconds = 5
PingSeconds = 30
)
type Request struct {
Id int64 `json:"id"`
Method string `json:"method"`
Params []interface{} `json:"params"`
}
type Response struct {
Id int64 `json:"id"`
Result interface{} `json:"result,omitempty"`
Error struct {
Code int `json:"code"`
Message string `json:"message"`
Data struct {
Code int `json:"code"`
Name string `json:"name"`
Message string `json:"message"`
Stack []struct {
Context struct {
Level string `json:"level"`
File string `json:"file"`
Line int `json:"line"`
Method string `json:"method"`
Hostname string `json:"hostname"`
ThreadName string `json:"thread_name"`
Timestamp string `json:"timestamp"` // format: "2017-06-07T02:28:07"
} `json:"context"`
Format string `json:"format"`
Data interface{} `json:"data"`
} `json:"stack"`
} `json:"data"`
} `json:"error,omitempty"`
}
// Client
type Client struct {
conn *websocket.Conn
callbacks map[int64]func(response Response, err error)
lastId int64
isVerbose bool
sync.Mutex
}
// Create a new client
func NewClient(verbose bool) (*Client, error) {
// connect,
conn, _, err := websocket.DefaultDialer.Dial(ApiAddr, nil)
if err != nil {
log.Fatal("Failed to dial:", err)
return nil, err
}
newClient := &Client{
conn: conn,
callbacks: make(map[int64]func(Response, error)),
lastId: 0,
isVerbose: verbose,
}
// wait for incoming messages,
go func(client *Client) {
for {
if typ, bytes, err := client.conn.ReadMessage(); err != nil {
log.Println("Failed to read:", err)
} else {
if typ == websocket.TextMessage {
if client.isVerbose {
log.Printf("Received bytes: %s", string(bytes))
}
var res Response
if err := json.Unmarshal(bytes, &res); err == nil {
if fn, exists := client.callbacks[res.Id]; exists {
go fn(res, nil) // call callback function
delete(client.callbacks, res.Id) // delete used callback function
} else {
log.Printf("No such callback with id: %d", res.Id)
}
} else {
log.Printf("Failed to decode received message: %s", err)
}
}
}
}
}(newClient)
// send ping (for keeping connection)
go func(client *Client) {
for {
time.Sleep(PingSeconds * time.Second)
client.Lock()
err := client.conn.WriteMessage(websocket.PingMessage, []byte{})
client.Unlock()
if err != nil {
log.Printf("Failed to ping: %s", err)
}
}
}(newClient)
return newClient, nil
}
// Get a new request
func (c *Client) NewRequest(api, function string, params []interface{}) Request {
req := Request{
Method: "call",
Params: []interface{}{
api,
function,
params,
},
Id: c.lastId,
}
c.lastId += 1 // increase last id
return req
}
// Send request and return its result through callback function
func (c *Client) SendRequestAsync(
request Request,
callback func(r Response, e error),
) (err error) {
var bytes []byte
if bytes, err = json.Marshal(request); err == nil {
c.callbacks[request.Id] = callback
c.Lock()
err = c.conn.WriteMessage(websocket.TextMessage, bytes)
c.Unlock()
}
return err
}
// Send request and return its result synchronously
func (c *Client) SendRequest(request Request) (r Response, err error) {
rCh := make(chan Response)
eCh := make(chan error)
err = c.SendRequestAsync(
request,
func(r Response, e error) {
if e != nil {
eCh <- e
} else {
rCh <- r
}
},
)
if err == nil {
// wait for the callback
select {
case r := <-rCh:
return r, nil
case e := <-eCh:
err = e
case <-time.After(time.Second * TimeoutSeconds): // timeout
err = fmt.Errorf("Request timed out in %d sec", TimeoutSeconds)
}
}
return Response{}, err
}
// Close connection
func (c *Client) Close() error {
c.Lock()
defer c.Unlock()
return c.conn.WriteMessage(
websocket.CloseMessage,
websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""),
)
}