Last active
January 13, 2019 10:03
-
-
Save amsokol/61435aa20b6ea699cc6be269d4be2c5d to your computer and use it in GitHub Desktop.
flutter-grpc-tutorial.go-server.pkg.service.v1.chat.go
This file contains hidden or 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
package v1 | |
import ( | |
"context" | |
"fmt" | |
"log" | |
"github.com/golang/protobuf/ptypes/empty" | |
"github.com/golang/protobuf/ptypes/wrappers" | |
"github.com/amsokol/flutter-grpc-tutorial/go-server/pkg/api/v1" | |
) | |
// chatServiceServer is implementation of v1.ChatServiceServer proto interface | |
type chatServiceServer struct { | |
msg chan string | |
} | |
// NewChatServiceServer creates Chat service object | |
func NewChatServiceServer() v1.ChatServiceServer { | |
return &chatServiceServer{msg: make(chan string, 1000)} | |
} | |
// Send sends message to the server | |
func (s *chatServiceServer) Send(ctx context.Context, message *wrappers.StringValue) (*empty.Empty, error) { | |
if message != nil { | |
log.Printf("Send requested: message=%v", *message) | |
s.msg <- message.Value | |
} else { | |
log.Print("Send requested: message=<empty>") | |
} | |
return &empty.Empty{}, nil | |
} | |
// Subscribe is streaming method to get echo messages from the server | |
func (s *chatServiceServer) Subscribe(e *empty.Empty, stream v1.ChatService_SubscribeServer) error { | |
log.Print("Subscribe requested") | |
for { | |
m := <-s.msg | |
n := v1.Message{Text: fmt.Sprintf("I have received from you: %s. Thanks!", m)} | |
if err := stream.Send(&n); err != nil { | |
// put message back to the channel | |
s.msg <- m | |
log.Printf("Stream connection failed: %v", err) | |
return nil | |
} | |
log.Printf("Message sent: %+v", n) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment