-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhandle.go
59 lines (51 loc) · 1.01 KB
/
handle.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
// Copyright (c) , donnie <donnie4w@gmail.com>
// All rights reserved.
//
// github.com/donnie4w/timgo
package timgo
import (
"github.com/donnie4w/go-logger/logger"
wss "github.com/donnie4w/gofer/websocket"
"time"
)
type handle struct {
tc *TimClient
pingCount int
handler *wss.Handler
}
func newHandle(tc *TimClient) (r *handle, err error) {
r = &handle{tc: tc}
if r.handler, err = wss.NewHandler(tc.cfg); err == nil {
go r.ping()
}
return
}
func (h *handle) close() error {
return h.handler.Close()
}
func (h *handle) pong() {
h.pingCount = 0
}
func (h *handle) ping() {
defer recoverable()
ticker := time.NewTicker(15 * time.Second)
for !h.tc.isClose {
select {
case <-ticker.C:
h.pingCount++
if h.tc.isClose {
h.close()
goto END
}
if err := h.send(h.tc.ts.ping()); err != nil || h.pingCount > 3 {
logger.Error("ping over count>>", h.pingCount, err)
h.close()
goto END
}
}
}
END:
}
func (h *handle) send(bs []byte) error {
return h.handler.Send(bs)
}