-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
73 lines (61 loc) · 1.6 KB
/
server.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"context"
"grpc-sse/protos"
"google.golang.org/grpc/grpclog"
emptypb "google.golang.org/protobuf/types/known/emptypb"
)
type EventRecord struct {
Code string
}
type EventServer struct {
protos.UnimplementedEventServer
Log grpclog.LoggerV2
EventCh chan EventRecord
Connected []protos.Event_SubscribeToEventsServer
}
func (c *EventServer) EventLoop() {
c.Log.Infof("Event loop started")
for e := range c.EventCh {
for _, sub := range c.Connected {
msg := &protos.EventMsg{
Text: "Event occured",
Event: e.Code,
Action: "REFRESH",
}
err := sub.Send(msg)
if err != nil {
c.Log.Errorf("delivery failed to: %v error: %s", sub, err)
}
}
}
c.Log.Infof("Event loop closed")
}
func (c *EventServer) Status(context.Context, *emptypb.Empty) (msg *protos.StringMsg, err error) {
c.Log.Infoln("Handing Hello...")
msg = &protos.StringMsg{
Text: "Status OK",
}
return
}
func (c *EventServer) Echo(_ context.Context, param *protos.StringMsg) (msg *protos.StringMsg, err error) {
c.Log.Infoln("Handing Echo...")
msg = &protos.StringMsg{
Text: "Echo: " + param.Text,
}
return
}
func (c *EventServer) SubscribeToEvents(_ *emptypb.Empty, sub protos.Event_SubscribeToEventsServer) (err error) {
c.Log.Infoln("Subscribe request...")
c.Connected = append(c.Connected, sub)
<-sub.Context().Done()
return sub.Context().Err()
}
func (c *EventServer) BroadcastEvent(_ context.Context, e *protos.EventCode) (emp *emptypb.Empty, err error) {
c.Log.Infoln("Broadcast request...")
c.EventCh <- EventRecord{
Code: e.Code,
}
emp = &emptypb.Empty{}
return
}