Skip to content

Commit

Permalink
feat: grpc application server
Browse files Browse the repository at this point in the history
  • Loading branch information
mastersans committed Jul 23, 2024
1 parent 5d5eb0f commit ec7a77f
Show file tree
Hide file tree
Showing 8 changed files with 733 additions and 0 deletions.
45 changes: 45 additions & 0 deletions grpc/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package client

import (
"context"
"io"
"log"
"time"

pb "github.com/keploy/samples-go/grpc/generated/proto"
"google.golang.org/grpc"
)

const (
address = "localhost:50051"
)

func StartClient() {
conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()

client := pb.NewStudentServiceClient(conn)

ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

stream, err := client.GetStudentStream(ctx, &pb.StudentRequest{Id: 1})
if err != nil {
log.Fatalf("could not get student stream: %v", err)
}

for {
msg, err := stream.Recv()
if err == io.EOF {
log.Println("Stream closed")
break
}
if err != nil {
log.Fatalf("stream receive error: %v", err)
}
log.Printf("Streamed Student: Name: %s, Age: %d", msg.GetName(), msg.GetAge())
}
}
239 changes: 239 additions & 0 deletions grpc/generated/proto/message.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ec7a77f

Please sign in to comment.