forked from aceld/zinx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
59 lines (45 loc) · 1012 Bytes
/
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
package main
import (
"fmt"
"github.com/aceld/zinx/ziface"
"github.com/aceld/zinx/zlog"
"github.com/aceld/zinx/znet"
"os"
"os/signal"
"time"
)
// PongRouter pong test 自定义路由
type PongRouter struct {
znet.BaseRouter
}
// Handle Pong Handle
func (this *PongRouter) Handle(request ziface.IRequest) {
zlog.Debug("Call PongRouter Handle")
//先读取服务器返回的数据
zlog.Debug("recv from client : msgId=", request.GetMsgID(), ", data=", string(request.GetData()))
}
func wait() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)
sig := <-c
fmt.Println("===exit===", sig)
}
func main() {
// Create a TLS client.
c := znet.NewTLSClient("127.0.0.1", 8899)
c.SetOnConnStart(func(connection ziface.IConnection) {
go func() {
for {
err := connection.SendMsg(1, []byte("Ping with TLS"))
if err != nil {
fmt.Println(err)
break
}
time.Sleep(1 * time.Second)
}
}()
})
c.AddRouter(2, &PongRouter{})
c.Start()
wait()
}