/
wsHandler.go
52 lines (46 loc) · 1.13 KB
/
wsHandler.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
package apiClient
import (
"time"
"github.com/fasthttp-contrib/websocket"
"github.com/gramework/gramework"
)
// WSHandler returns gramework handler
func (client *Instance) WSHandler() func(*gramework.Context) error {
return func(ctx *gramework.Context) error {
if websocket.IsWebSocketUpgrade(ctx.RequestCtx) {
websocket.Upgrade(ctx.RequestCtx, func(conn *websocket.Conn) {
for {
v := <-client.watch(ctx)
conn.WriteMessage(websocket.TextMessage, v)
}
}, 0, 0)
return nil
}
client.handleHTTP(ctx)
return nil
}
}
func (client *Instance) watch(ctx *gramework.Context) chan []byte {
c := make(chan []byte)
go func() {
for {
api, err := client.nextServer()
if err != nil {
ctx.Logger.Errorf("error: %s", err)
time.Sleep(client.conf.WatcherTickTime)
continue
}
bytes := buffer.Get()
defer buffer.Put(bytes)
_, body, err := api.HostClient.Get(bytes.B, api.Addr)
if err != nil {
ctx.Logger.Errorf("error while .Do() the request %s", err)
time.Sleep(client.conf.WatcherTickTime)
continue
}
c <- body
time.Sleep(client.conf.WatcherTickTime)
}
}()
return c
}