/
hub.go
77 lines (62 loc) · 1.55 KB
/
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
76
77
package hub
import (
"log"
"github.com/brianstarke/dogfort/domain"
)
type Hub struct {
Connections map[domain.UserUid]*Connection
Register chan struct {
domain.UserUid
*Connection
}
Unregister chan domain.UserUid
}
var H = Hub{
Connections: make(map[domain.UserUid]*Connection),
Register: make(chan struct {
domain.UserUid
*Connection
}),
Unregister: make(chan domain.UserUid),
}
func (h *Hub) Run() {
for {
select {
case c := <-h.Register:
h.Connections[c.UserUid] = c.Connection
log.Printf("%s is active, %d currently active", c.UserUid, len(h.Connections))
h.Broadcast("user_join", string(c.UserUid))
case c := <-h.Unregister:
close(h.Connections[c].send)
delete(h.Connections, c)
h.Broadcast("user_leave", string(c))
log.Printf("connection unregistered, %d currently active", len(h.Connections))
}
}
}
/*
Returns a list of users that are currently online
*/
func (h *Hub) UsersOnline() []domain.UserUid {
users := make([]domain.UserUid, len(h.Connections))
for k, _ := range h.Connections {
users = append(users, k)
}
return users
}
func (h *Hub) Broadcast(topic string, message string) {
for _, v := range h.Connections {
v.send <- map[string]interface{}{topic: message}
}
}
func (h *Hub) MessagePublish(message *domain.Message) {
for k, v := range h.Connections {
sendIt, err := domain.ChannelDomain.UserInChannel(&k, message.ChannelId)
if err != nil {
log.Printf("Error publishing message: %s", err.Error())
}
if sendIt {
v.send <- map[string]interface{}{"message": message}
}
}
}