I am using gRPC
with Golang
.
After successfully running their HelloWorld tutorial
which required me to add a second function, compile the protoc file, I decided to make some further changes.
My code is included, which appears not to work because it expects the pb
variable to be a HelloWorld object, or something, as shown in the error message in command line below. Could someone please tell me where I have gone wrong, for what should be some very small changes.
I have fixed some typos in my original post's code below.
Command line:
go run server/server.go
# command-line-arguments
server/server.go:24:71: undefined: helloworld.TheGRPCNotificationMessage
Command line protoc file:
protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative GRPCNotification/GRPCNotification.proto
GRPCNotification.proto
syntax = "proto3";
import "google/protobuf/empty.proto";
option go_package = "google.golang.org/grpc/examples/helloworld/helloworld";
option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
package grpcnotificationpackage;
// The greeting service definition.
service TheGRPCNotificationService {
// Sends a notification and nothing sent back
rpc SendNotificationWithGRPC (TheGRPCNotificationMessage) returns (google.protobuf.Empty);
// Receives a notification and does not reply
rpc ReceiveNotificationWithGRPC (stream TheGRPCNotificationMessage) returns (google.protobuf.Empty);
}
// The request notification message
message TheGRPCNotificationMessage {
string message = 1;
// ... add more here
}
// Since nothing gets returned from server we need this empty
message Empty {
// Nothing goes here
}
Client.go
// Package main implements a client for Greeter service.
package main
import (
"context"
"log"
"time"
"google.golang.org/grpc"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
)
const (
address = "localhost:50051"
defaultName = "world"
)
func main() {
// Set up a connection to the server.
conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewGreeterClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.SendNotificationWithGRPC(ctx, &pb.TheGRPCNotificationMessage{Message: "Horse"})
if err != nil {
log.Fatalf("could not greet: %v", err)
}
log.Printf("Sent a message, please check it reached server...")
}
Server.go
package main
import (
"context"
"log"
"net"
"google.golang.org/grpc"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
)
const (
port = ":50051"
)
// server is used to implement helloworld.GreeterServer
type server struct {
pb.UnimplementedGreeterServer
}
// SendNotificationWithGRPC implements helloworld.GreeterServer <--- Problem?
func (s *server) ReceiveNotificationWithGRPC(ctx context.Context, in *pb.TheGRPCNotificationMessage) {
log.Printf("Received: %v", in.Name)
}
func main() {
lis, err := net.Listen("tcp", port)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterGreeterServer(s, &server{})
log.Printf("server listening at %v", lis.Addr())
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
I wish the example tutorial did not glue me to it so roughly!