-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
54 lines (47 loc) · 1.34 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
package main
import (
"io"
"log"
pb "github.com/narenaryan/serverPush/datafiles"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
const (
address = "localhost:50051"
)
// ReceiveStream listens to the stream contents and use them
func ReceiveStream(client pb.MoneyTransactionClient, request *pb.TransactionRequest) {
log.Println("Started listening to the server stream!")
stream, err := client.MakeTransaction(context.Background(), request)
if err != nil {
log.Fatalf("%v.MakeTransaction(_) = _, %v", client, err)
}
// Listen to the stream of messages
for {
response, err := stream.Recv()
if err == io.EOF {
// If there are no more messages, get out of loop
break
}
if err != nil {
log.Fatalf("%v.MakeTransaction(_) = _, %v", client, err)
}
log.Printf("Status: %v, Operation: %v", response.Status, response.Description)
}
}
func main() {
// Set up a connection to the server.
conn, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
log.Fatalf("Did not connect: %v", err)
}
defer conn.Close()
client := pb.NewMoneyTransactionClient(conn)
// Prepare data. Get this from clients like Front-end or Android App
from := "1234"
to := "5678"
amount := float32(1250.75)
// Contact the server and print out its response.
ReceiveStream(client, &pb.TransactionRequest{From: from,
To: to, Amount: amount})
}