forked from xxf098/LiteSpeedTest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
41 lines (37 loc) · 980 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
package liteclient
import (
"context"
"io"
"log"
pb "github.com/laof/lite-speed-test/api/rpc/lite"
"github.com/laof/lite-speed-test/download"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func StartClient(addr string, req *pb.TestRequest) ([]*pb.TestReply, error) {
conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return nil, err
}
defer conn.Close()
c := pb.NewTestProxyClient(conn)
ctx := context.Background()
stream, err := c.StartTest(ctx, req)
if err != nil {
return nil, err
}
result := []*pb.TestReply{}
for {
reply, err := stream.Recv()
if err == io.EOF {
stream.CloseSend()
break
}
if err != nil {
return nil, err
}
log.Println("id: ", reply.Id, reply.Remarks, "ping:", reply.Ping, "avg:", download.ByteCountIECTrim(reply.AvgSpeed), "max:", download.ByteCountIECTrim(reply.MaxSpeed))
result = append(result, reply)
}
return result, nil
}