forked from fwhezfwhez/tcpx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
125 lines (111 loc) · 2.38 KB
/
main.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
// Package client executable file
package main
import (
"encoding/json"
"fmt"
"net"
"github.com/CocoKelam/tcpx"
"github.com/fwhezfwhez/errorx"
//"tcpx"
)
var packx = tcpx.NewPackx(tcpx.JsonMarshaller{})
// var packxProto = tcpx.NewPackx(tcpx.ProtobufMarshaller{})
func main() {
conn, err := net.Dial("tcp", "localhost:7171")
if err != nil {
panic(err)
}
go func() {
for {
buf, e := tcpx.FirstBlockOf(conn)
if e != nil {
//if e == io.EOF {
// break
//}
panic(errorx.Wrap(e))
}
fmt.Println(buf)
var receivedString string
//fmt.Println(buf)
message, e := packx.Unpack(buf, &receivedString)
if e != nil {
panic(errorx.Wrap(e))
}
fmt.Println("收到服务端消息块:", smartPrint(message))
fmt.Println("服务端消息:", receivedString)
//var resp pb.SayHelloReponse
//message, e := tcpx.UnpackWithMarshallerName(buf, &resp, "protobuf")
//if e != nil {
// panic(errorx.Wrap(e))
//}
//fmt.Println("收到服务端消息块:", smartPrint(message))
//fmt.Println("服务端消息:", resp)
}
}()
var buf []byte
var e error
//buf, e = tcpx.PackWithMarshallerName(tcpx.Message{
// MessageID: 11,
// Body: &pb.SayHelloRequest{
// Username: "ft",
// },
//}, "protobuf")
//if e != nil {
// panic(e)
//}
//go func() {
// heartbeat, e := tcpx.PackWithMarshallerName(tcpx.Message{
// MessageID: 20,
// Body: nil,
// }, "protobuf")
// if e != nil {
// panic(e)
// }
// for {
// time.Sleep(5 * time.Second)
// conn.Write(heartbeat)
// }
//}()
//_ = buf
//conn.Write(buf)
buf, e = packx.Pack(5, "hello,I am client xiao ming", map[string]interface{}{
"api": "/tcpx/client1/",
})
if e != nil {
panic(e)
}
fmt.Println(buf)
for i := 0; i < 1000; i++ {
conn.Write(buf)
}
//buf, e = packx.Pack(7, struct {
// Username string `json:"username"`
//}{"FT"}, map[string]interface{}{
// "api": "/tcpx/client1/",
//})
//if e != nil {
// panic(e)
//}
//
// _=buf
//conn.Write(buf)
//buf, e := tcpx.PackWithMarshallerName(tcpx.Message{
// MessageID: 9,
// Header: map[string]interface{}{
// "api": "/tcpx/client1/",
// },
// Body: struct {
// ServiceName string `json:"service_name"`
// }{"FT"},
//}, "json")
//
//if e != nil {
// panic(e)
//}
//conn.Write(buf)
select {}
}
func smartPrint(src interface{}) string {
buf, _ := json.MarshalIndent(src, " ", " ")
return string(buf)
}