-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecho_client.go
119 lines (108 loc) · 3.09 KB
/
echo_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
117
118
119
// Copyright 2021 fangyousong(方友松). All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// echo client
package main
import (
"bufio"
"flag"
"fmt"
"github.com/truexf/goutil"
"os"
"time"
"github.com/truexf/iip"
)
type EchoClientHandler struct {
}
func (m *EchoClientHandler) Handle(path string, request iip.Request, responseData []byte, dataCompleted bool) error {
fmt.Printf("response in handler: %s\n for request: %s\n", goutil.UnsafeBytesToString(responseData), string(request.Data()))
return nil
}
var (
certFileClient = flag.String("cert", "", "echo_client -cert certFile -key keyFile")
keyFileClient = flag.String("key", "", "echo_client -cert certFile -key keyFile")
tls = flag.String("tls", "0", "-tls=1")
)
func main() {
fmt.Println("connect server: 9090")
var client *iip.Client
var err error
flag.Parse()
//1. 创建Client
if *certFileClient != "" && *keyFileClient != "" {
fmt.Printf("certfile: %s, keyfile: %s\n", *certFileClient, *keyFileClient)
client, err = iip.NewClientTLS(
iip.ClientConfig{
MaxConnections: 1000,
MaxChannelsPerConn: 10,
ChannelPacketQueueLen: 1000,
TcpWriteQueueLen: 1000,
TcpReadBufferSize: 16 * 1024,
TcpWriteBufferSize: 16 * 1024,
TcpConnectTimeout: time.Second * 3,
},
":9090",
nil,
*certFileClient,
*keyFileClient,
)
} else if *tls != "0" {
fmt.Println("new tls client")
client, err = iip.NewClientTLS(
iip.ClientConfig{
MaxConnections: 1000,
MaxChannelsPerConn: 10,
ChannelPacketQueueLen: 1000,
TcpWriteQueueLen: 1000,
TcpReadBufferSize: 16 * 1024,
TcpWriteBufferSize: 16 * 1024,
TcpConnectTimeout: time.Second * 3,
},
":9090",
nil,
"",
"",
)
} else {
client, err = iip.NewClient(
iip.ClientConfig{
MaxConnections: 1000,
MaxChannelsPerConn: 10,
ChannelPacketQueueLen: 1000,
TcpWriteQueueLen: 1000,
TcpReadBufferSize: 16 * 1024,
TcpWriteBufferSize: 16 * 1024,
TcpConnectTimeout: time.Second * 3,
},
":9090",
nil,
)
}
if err != nil {
fmt.Printf("new client fail, %s\n", err.Error())
return
}
// 这里并非必要,只是展示Handler作为一个响应回调处理的功能,client handler一般一般用于流式(分段)响应的应用场景,见Client.DoStreamRequest
client.RegisterHandler("/echo", &EchoClientHandler{})
// 2.创建channel
channel, err := client.NewChannel()
if err != nil {
fmt.Printf("new channel fail, %s\n", err.Error())
return
}
fmt.Println("input some words:")
lineReader := bufio.NewScanner(os.Stdin)
// 循环接收控制台的输入,并将其发送给server,并接收server的响应并输出到控制台
for {
if !lineReader.Scan() {
break
}
response, err := channel.DoRequest("/echo", iip.NewDefaultRequest(lineReader.Bytes()), time.Second)
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Printf("response ret: %s\n", goutil.UnsafeBytesToString(response))
}
fmt.Println("input some words:")
}
}