-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_broadcast_hub.go
75 lines (63 loc) · 1.8 KB
/
server_broadcast_hub.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
74
75
package websocket
import (
"github.com/rs/zerolog"
)
type BroadcastHub struct {
// Inbound messages from the c.
broadcast chan []byte
broadcastStatus chan map[*Client]SendStatus
s *Server
}
func NewBroadcastHub(s *Server) *BroadcastHub {
return &BroadcastHub{
// Channels
broadcast: make(chan []byte),
broadcastStatus: make(chan map[*Client]SendStatus),
s: s, // Server
}
}
func (h *BroadcastHub) run() {
// Start an infinite loop!`
info := func() *zerolog.Event {
return h.s.LInfo().Str("sub_module", "broadcast_hub")
}
info().Msg("running...")
defer info().Msg("leaving...")
terminate := false
for {
select {
case message := <-h.broadcast:
// On Broadcast (Messages to all c)
//log.Println("Sending broadcast Message")
info().Msg("sending message")
// TODO: make we should slice... because the data can change when reading, and there is no guarantee
// that will be the same when looping!
// TODO: send using muiltiple goroutines, but don't generate multople,
// allow only couple of goroutines to be concurently in work
sendStatuses := make(map[*Client]SendStatus)
for client := range h.s.c.GetClients() {
// TODO: we should check if the channel is still active!
select {
case client.send <- message:
// await for response
sendStatuses[client] = <-client.sendStatus
// TODO: save this response into a map
// and give back to the sender
default:
/*// Closing the channel!
close(client.send)
// Deletes the element from the map!
delete(h.c, client)*/
}
}
h.broadcastStatus <- sendStatuses
case <-h.s.ctx.Done():
// This is the general Hub... we should simply terminate it!
info().Msg("terminating...")
terminate = true
}
if terminate {
break
}
}
}