Пример #1
0
func getHelloWorld(client pb.TalkMessageClient, content string) {
	msg, err := client.GetHelloWorld(context.Background(), &pb.Msg{Content: content})
	if err != nil {
		grpclog.Fatalf("fail to call getHelloWorld: %v", err)
	}

	grpclog.Println("get Message from Server: ", msg.Content)
}
Пример #2
0
func letsStreaming(client pb.TalkMessageClient) {
	// messages
	letters := []string{"a", "b", "c", "d", "e", "f", "g", "h"}

	stream, err := client.LetsStreaming(context.Background())
	if err != nil {
		grpclog.Fatalf("%v.RecordRoute(_) = _, %v", client, err)
	}

	// receive msg
	waitc := make(chan struct{})
	go func() {
		for {
			in, err := stream.Recv()
			if err == io.EOF {
				// read done
				close(waitc)
				return
			}
			if err != nil {
				grpclog.Fatalf("Failed to receive a note : %v", err)
			}
			grpclog.Printf("Got message %s", in.Content)
		}
	}()

	// send msg
	for _, str := range letters {
		grpclog.Printf("send msg: %v", str)
		if err := stream.Send(&pb.Msg{Content: str}); err != nil {
			grpclog.Fatalf("%v.Send(%v) = %v", stream, str, err)
		}
		sleep := 5
		grpclog.Printf("sleep for %v seconds", sleep)
		time.Sleep(time.Duration(sleep) * time.Second) //"sleep for 5 seconds"
	}

	// close send
	stream.CloseSend()
	<-waitc
}