-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
client.go
68 lines (52 loc) · 1.36 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
package main
import (
"fmt"
"github.com/aceld/zinx/examples/zinx_client/c_router"
"github.com/aceld/zinx/ziface"
"github.com/aceld/zinx/zlog"
"github.com/aceld/zinx/znet"
"os"
"os/signal"
"time"
)
func business(conn ziface.IConnection) {
for {
err := conn.SendMsg(100, []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", "刘丹冰Aceld")
conn.SetProperty("Home", "https://yuque.com/aceld")
go business(conn)
}
func DoClientConnectedLost(conn ziface.IConnection) {
if name, err := conn.GetProperty("Name"); err == nil {
zlog.Debug("Conn Property Name = ", name)
}
if home, err := conn.GetProperty("Home"); err == nil {
zlog.Debug("Conn Property Home = ", home)
}
zlog.Debug("DoClientConnectedLost is Called ... ")
}
func main() {
client := znet.NewClient("127.0.0.1", 8999)
client.SetOnConnStart(DoClientConnectedBegin)
client.SetOnConnStop(DoClientConnectedLost)
client.AddRouter(2, &c_router.PingRouter{})
client.AddRouter(3, &c_router.HelloRouter{})
client.Start()
// close
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)
sig := <-c
fmt.Println("===exit===", sig)
client.Stop()
time.Sleep(time.Second * 2)
}