Created
October 20, 2018 01:17
-
-
Save seanhagen/49634abdaa3ddd5da1953948e01f8a37 to your computer and use it in GitHub Desktop.
Example implementation of server for reproduction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PingMe ... | |
func (s *Service) PingMe(ctx context.Context, in *api.PingMessage) (*api.PongMessage, error) { | |
header := metadata.Pairs("header-key", "val") | |
err := grpc.SendHeader(ctx, header) | |
return &api.PongMessage{Response: fmt.Sprintf("pongback: %v", in.Greeting)}, err | |
} | |
// StreamIn ... | |
func (s *Service) StreamIn(stream api.Ping_StreamInServer) error { | |
count := 0 | |
back := "start" | |
for { | |
in, err := stream.Recv() | |
if err != nil { | |
if err == io.EOF { | |
break | |
} | |
return err | |
} | |
back = fmt.Sprintf("%v %v", back, in.Greeting) | |
count++ | |
} | |
return stream.SendAndClose(&api.PingCountMessage{ | |
Count: int64(count), | |
Message: back, | |
}) | |
} | |
// StreamBack ... | |
func (s *Service) StreamBack(in *api.PingCountMessage, stream api.Ping_StreamBackServer) error { | |
for i := int64(0); i < in.Count; i++ { | |
err := stream.Send(&api.PingMessage{ | |
Greeting: fmt.Sprintf("pong: %v", in.Message), | |
}) | |
if err != nil { | |
return err | |
} | |
} | |
return nil | |
} | |
// StreamPing ... | |
func (s *Service) StreamPing(stream api.Ping_StreamPingServer) error { | |
for { | |
in, err := stream.Recv() | |
if err == io.EOF { | |
fmt.Printf("unable to receive?: %v\n", err) | |
return nil | |
} | |
if err != nil { | |
fmt.Printf("unable to receive?: %v\n", err) | |
return err | |
} | |
spew.Dump(in) | |
out := &api.PongMessage{ | |
Response: fmt.Sprintf("pong: %v", in.Greeting), | |
} | |
if err = stream.Send(out); err != nil { | |
fmt.Printf("unable to send: %v\n", err) | |
return err | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment