-
Notifications
You must be signed in to change notification settings - Fork 3
/
websocket.go
53 lines (45 loc) · 1.14 KB
/
websocket.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
package phemex
import (
"encoding/json"
"fmt"
"math/rand"
"time"
"github.com/gorilla/websocket"
)
// WsHandler handle raw websocket message
type WsHandler func(message interface{})
// ErrHandler handles errors
type ErrHandler func(err error)
func keepAlive(c *websocket.Conn, id int64, stop chan struct{}, errHandler ErrHandler) {
rand.Seed(time.Now().UnixNano())
ticker := time.NewTicker(WebsocketTimeout)
lastResponse := time.Now()
c.SetPongHandler(func(msg string) error {
lastResponse = time.Now()
return nil
})
go func() {
defer func() {
ticker.Stop()
}()
for {
select {
case <-stop:
return
case <-ticker.C:
id++
p, _ := json.Marshal(map[string]interface{}{
"id": id,
"method": "server.ping",
"params": []string{},
})
c.WriteControl(websocket.PingMessage, p, time.Time{})
// as suggested https://github.com/phemex/phemex-api-docs/blob/master/Public-API-en.md#1-session-management
if time.Since(lastResponse) > 3*WebsocketTimeout {
errHandler(fmt.Errorf("last pong exceeded the timeout: %[1]v (%[2]v)", time.Since(lastResponse), id))
return
}
}
}
}()
}