/
define.go
139 lines (112 loc) · 2.61 KB
/
define.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
package relaysdk
import (
"net"
"time"
)
//客户端
type Client struct {
sess *net.TCPConn
id string
connected bool
count int
reconnecting bool //正在重新连接的标志
addr string
port int //端口 默认17083
reconnectTime time.Duration //重连时间
reconnectTimes int
reconnectTicker *time.Ticker //自动连接
reconnectTickerOver chan interface{} //关闭自动连接
heartBeatTime time.Duration //重连时间
heartBeatTicker *time.Ticker //心跳包的发送
heartBeatTickerOver chan interface{} //关闭心跳
onDisconnect func(c *Client)
onConnected func(c *Client)
onConnecting func(c *Client)
onTimeout func(c *Client)
onClose func(c *Client)
onRelayOpen func(data []byte)
onRelayClosed func(data []byte)
onRelayReset func(data []byte)
}
func (c *Client) IsReconnecting() bool {
return c.reconnecting
}
func (c *Client) IsConnected() bool {
return c.connected
}
func (c *Client) Id() string {
return c.id
}
func (c *Client) SetId(id string) *Client {
c.id = id
return c
}
func (c *Client) Port() int {
return c.port
}
func (c *Client) SetPort(port int) *Client {
c.port = port
return c
}
func (c *Client) Address() string {
return c.addr
}
func (c *Client) SetAddress(address string) *Client {
c.addr = address
return c
}
func (c *Client) ReconnectTimes() int {
return c.reconnectTimes
}
func (c *Client) SetReconnectTimes(reconnectTimes int) *Client {
c.reconnectTimes = reconnectTimes
return c
}
func (c *Client) ReconnectTime() time.Duration {
if c.reconnectTime == 0 {
c.reconnectTime = time.Second * 10
}
return c.reconnectTime
}
func (c *Client) SetReconnectTime(reconnectTime time.Duration) *Client {
c.reconnectTime = reconnectTime
return c
}
func (c *Client) HeartBeatTime() time.Duration {
if c.heartBeatTime == 0 {
c.heartBeatTime = 5
}
return c.heartBeatTime
}
func (c *Client) OnConnecting(f func(c *Client)) *Client {
c.onConnecting = f
return c
}
func (c *Client) OnConnected(f func(c *Client)) *Client {
c.onConnected = f
return c
}
func (c *Client) OnDisconnect(f func(c *Client)) *Client {
c.onDisconnect = f
return c
}
func (c *Client) OnTimeout(f func(c *Client)) *Client {
c.onTimeout = f
return c
}
func (c *Client) OnClose(f func(c *Client)) *Client {
c.onClose = f
return c
}
func (c *Client) OnRelayOpen(f func(data []byte)) *Client {
c.onRelayOpen = f
return c
}
func (c *Client) OnRelayClosed(f func(data []byte)) *Client {
c.onRelayClosed = f
return c
}
func (c *Client) OnRelayReset(f func(data []byte)) *Client {
c.onRelayReset = f
return c
}