-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
116 lines (98 loc) · 2.89 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
package socket_v1
import "C"
import (
"fmt"
"net"
"time"
)
// 客户端结构体
type Client struct {
socketMsg
ip string // ip地址
port int // 端口
heartBeatTime int // 心跳时间
conn net.Conn // 连接句柄
ClientId string // 我的客户端id
OnMessage func(msg UDataSocket, C *Client) // 消息回调
OnConnect func(C *Client) // 上线回调
OnConnectFail func(C *Client) // 上线回调
OnDisConnect func(C *Client) // 掉线回调
}
// 对外函数1:初始化一个客户端
func NewClient(ip string, port int, OnMessage func(msg UDataSocket, C *Client), OnConnectFail, OnConnect, OnDisConnect func(C *Client)) *Client {
return &Client{
ip: ip,
port: port,
heartBeatTime: 5,
conn: nil,
ClientId: "",
OnMessage: OnMessage,
OnConnect: OnConnect,
OnConnectFail: OnConnectFail,
OnDisConnect: OnDisConnect,
}
}
// 对外函数2:连接服务器
func (Me *Client) Connect() {
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", Me.ip, Me.port))
if err != nil {
fmt.Println("net.Dial error:", err)
Me.OnConnectFail(Me) // 回调连接失败事件
} else {
Me.conn = conn
// 运行
go Me.runWaitMsg()
}
}
// 对外函数3:延时后重连
func (Me *Client) ReConnect(second int) {
select {
case <-time.After(time.Duration(second) * time.Second):
Me.Connect()
}
}
// 对外函数4:消息发送
func (Me *Client) SendMsg(msg UDataSocket) error {
return sendSocketMsg(Me.conn, msg)
}
// 内部函数1:通讯保持
func (Me *Client) runWaitMsg() {
defer func() { fmt.Println(utilDateTime(), "退出 waitMsg") }()
defer func() { _ = Me.conn.Close(); Me.conn = nil }()
// 1、启动心跳
stopHeartBeat := make(chan bool)
go Me.heartBeat(stopHeartBeat)
// 2、发送问候消息
_ = sendSocketMsg(Me.conn, UDataSocket{0, 7, []byte("hello test msg from client")})
// 3、循环接收指令
Me.OnConnect(Me) // 回调连接成功事件
if err := Me.getSocketMsg(Me.conn, func(msg *UDataSocket) bool {
// 收到反馈的问候消息
if msg.CType == 8 {
fmt.Println(msg.Zlib, msg.CType, string(msg.Content))
}
Me.OnMessage(*msg, Me) // 回调收到消息事件
return true
}); err != nil {
stopHeartBeat <- true
Me.OnDisConnect(Me) // 回调连接断开事件
return // 退出用户运行协程
}
}
// 内部函数1:心跳保持
func (Me *Client) heartBeat(stopHeartBeat chan bool) {
defer func() { fmt.Println(utilDateTime(), "退出 heartBeat") }()
T := time.NewTicker(time.Second * time.Duration(Me.heartBeatTime))
for {
select {
case <-stopHeartBeat:
return
case <-T.C:
// 发送心跳消息,CType为1
if err := Me.SendMsg(UDataSocket{CType: 1}); err != nil {
// fmt.Println("退出心跳协程,停止发送心跳")
// return // 退出心跳协程,停止发送心跳
}
}
}
}