Skip to content

Instantly share code, notes, and snippets.

port := 50051 // we'll implement command line arguments leter
lis, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", *port))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
func (s *theSocialRobotServer) EventStream(stream pb.TheSocialRobot_EventStreamServer) error {
for {
// TODO handle events from the client
_, err := stream.Recv()
if err == io.EOF {
return nil
}
if err != nil {
return err
}
stream.Send(command)
command := &pb.BodyCommand{
Id: 1,
Actions: {% raw %} []*pb.Action{{Delay: 0, Action: &pb.Action_Say{Say: &pb.Say{Text: "Hello World"}}}}, {% endraw %}
}
_, err := stream.Recv()
if err == io.EOF {
return nil
}
if err != nil {
return err
}
type theSocialRobotServer struct {
pb.UnimplementedTheSocialRobotServer
}
func (s *theSocialRobotServer) EventStream(stream pb.TheSocialRobot_EventStreamServer) error {
// TBD
}
// TheSocialRobotServer is the server API for TheSocialRobot service.
// All implementations must embed UnimplementedTheSocialRobotServer
// for forward compatibility
type TheSocialRobotServer interface {
EventStream(TheSocialRobot_EventStreamServer) error
mustEmbedUnimplementedTheSocialRobotServer()
}
import pb "github.com/TheSocialRobot/BrainCore/thesocialrobot"
protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative thesocialrobot/thesocialrobot.proto
# get the protobuf compiler, protoc, for Linux
wget https://github.com/protocolbuffers/protobuf/releases/download/v21.5/protoc-21.5-linux-x86_64.zip
# unpack the zip file and move the protoc command to a convenient directory in your path
mkdir tmp-protoc ; cd tmp-protoc
unzip protoc-21.5-linux-x86_64.zip
mv bin/proto ~/.local/bin
cd .. ; rm -rf tmp-protoc
# install gRPC plugins for go