forked from aceld/zinx
/
client.go
73 lines (58 loc) · 1.62 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
package main
import (
"fmt"
"github.com/Yeuoly/zinx/examples/zinx_client/c_router"
"github.com/Yeuoly/zinx/ziface"
"github.com/Yeuoly/zinx/zlog"
"github.com/Yeuoly/zinx/znet"
"os"
"os/signal"
"time"
)
// 客户端自定义业务
func business(conn ziface.IConnection) {
for {
err := conn.SendMsg(1, []byte("Ping...[FromClient]"))
if err != nil {
fmt.Println(err)
zlog.Error(err)
break
}
time.Sleep(1 * time.Second)
}
}
// 创建连接的时候执行
func DoClientConnectedBegin(conn ziface.IConnection) {
zlog.Debug("DoConnecionBegin is Called ... ")
//设置两个链接属性,在连接创建之后
conn.SetProperty("Name", "刘丹冰")
conn.SetProperty("Home", "https://yuque.com/aceld")
go business(conn)
}
// 连接断开的时候执行
func DoClientConnectedLost(conn ziface.IConnection) {
//在连接销毁之前,查询conn的Name,Home属性
if name, err := conn.GetProperty("Name"); err == nil {
zlog.Error("Conn Property Name = ", name)
}
if home, err := conn.GetProperty("Home"); err == nil {
zlog.Error("Conn Property Home = ", home)
}
zlog.Debug("DoClientConnectedLost is Called ... ")
}
func main() {
//创建一个Client句柄,使用Zinx的API
client := znet.NewClient("127.0.0.1", 8999)
//添加首次建立链接时的业务
client.SetOnConnStart(DoClientConnectedBegin)
client.SetOnConnStop(DoClientConnectedLost)
//注册收到服务器消息业务路由
client.AddRouter(0, &c_router.PingRouter{})
//启动客户端client
client.Start()
// close
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)
sig := <-c
fmt.Println("===exit===", sig)
}