Skip to content

Instantly share code, notes, and snippets.

@dipeshdulal
Created July 12, 2020 03:04
Show Gist options
  • Save dipeshdulal/e10c2b8d39e0871149bc5cba0c35007a to your computer and use it in GitHub Desktop.
Save dipeshdulal/e10c2b8d39e0871149bc5cba0c35007a to your computer and use it in GitHub Desktop.
JoinChannel Implementation for Chat Server
func (s *chatServiceServer) JoinChannel(ch *chatpb.Channel, msgStream chatpb.ChatService_JoinChannelServer) error {
msgChannel := make(chan *chatpb.Message)
s.channel[ch.Name] = append(s.channel[ch.Name], msgChannel)
// doing this never closes the stream
for {
select {
case <-msgStream.Context().Done():
return nil
case msg := <-msgChannel:
fmt.Printf("GO ROUTINE (got message): %v \n", msg)
msgStream.Send(msg)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment