-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
49 lines (42 loc) · 948 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"fmt"
pb "github.com/JaimeRC/grpc-golang/bi_directional_streaming/proto"
"google.golang.org/grpc"
"io"
"log"
"net"
)
const (
port = ":50054"
)
type server struct {
pb.UnimplementedFeedsServer
}
func main() {
listener, err := net.Listen("tcp", port)
if err != nil {
log.Fatalf("could not start the server: %v", err)
}
s := grpc.NewServer()
pb.RegisterFeedsServer(s, &server{})
if err := s.Serve(listener); err != nil {
log.Fatalf("could not start the server: %v", err)
}
}
// Broadcast reads client stream and broadcasts received feeds
func (*server) Broadcast(stream pb.Feeds_BroadcastServer) error {
for {
msg, err := stream.Recv()
if err == io.EOF {
return nil
}
if err != nil {
log.Fatalf("could not recive from stream: %v", err)
return err
}
feed := "New Feed Recieved: " + msg.GetFeed()
fmt.Println("sending new feed...")
stream.Send(&pb.FeedResponse{Feed: feed})
}
}