@@ -6,6 +6,8 @@ import (
66 "net"
77 "net/http"
88 "net/url"
9+ "sort"
10+ "strconv"
911 "sync"
1012 "time"
1113
@@ -14,16 +16,24 @@ import (
1416 )
1517
1618 type event struct {
17- Type string `json:"type"`
18- Message *Message `json:"message,omitempty"`
19- Users []string `json:"users,omitempty"`
20- Roster []userSummary `json:"roster,omitempty"`
19+ Type string `json:"type"`
20+ Message *Message `json:"message,omitempty"`
21+ Users []string `json:"users,omitempty"`
22+ Roster []userSummary `json:"roster,omitempty"`
23+ Voice map[string][]string `json:"voice,omitempty"`
24+}
25+
26+// wsIn is a message sent from a client over the socket.
27+type wsIn struct {
28+ Type string `json:"type"`
29+ ChannelID int64 `json:"channelId"`
2130 }
2231
2332 type client struct {
2433 hub *hub
2534 userID int64
2635 username string
36+ voiceID int64
2737 conn *websocket.Conn
2838 send chan []byte
2939 }
@@ -31,11 +41,12 @@ type client struct {
3141 type hub struct {
3242 mu sync.Mutex
3343 clients map[int64]map[*client]bool
44+ voice map[int64]map[int64]string
3445 db *sql.DB
3546 }
3647
3748 func newHub(db *sql.DB) *hub {
38- return &hub{clients: map[int64]map[*client]bool{}, db: db}
49+ return &hub{clients: map[int64]map[*client]bool{}, voice: map[int64]map[int64]string{}, db: db}
3950 }
4051
4152 func (h *hub) register(c *client) {
@@ -56,11 +67,41 @@ func (h *hub) unregister(c *client) {
5667 delete(h.clients, c.userID)
5768 }
5869 }
70+ h.clearVoiceLocked(c)
5971 h.mu.Unlock()
6072 close(c.send)
6173 h.broadcastPresence()
6274 }
6375
76+// clearVoiceLocked drops a client's voice occupancy. Caller holds h.mu.
77+func (h *hub) clearVoiceLocked(c *client) {
78+ if c.voiceID == 0 {
79+ return
80+ }
81+ if set := h.voice[c.voiceID]; set != nil {
82+ delete(set, c.userID)
83+ if len(set) == 0 {
84+ delete(h.voice, c.voiceID)
85+ }
86+ }
87+ c.voiceID = 0
88+}
89+
90+// setVoice moves a client into a voice channel (0 for none) and rebroadcasts.
91+func (h *hub) setVoice(c *client, channelID int64) {
92+ h.mu.Lock()
93+ h.clearVoiceLocked(c)
94+ c.voiceID = channelID
95+ if channelID != 0 {
96+ if h.voice[channelID] == nil {
97+ h.voice[channelID] = map[int64]string{}
98+ }
99+ h.voice[channelID][c.userID] = c.username
100+ }
101+ h.mu.Unlock()
102+ h.broadcastPresence()
103+}
104+
64105 func (h *hub) users() []string {
65106 h.mu.Lock()
66107 defer h.mu.Unlock()
@@ -112,8 +153,28 @@ func (h *hub) roster() []userSummary {
112153 return users
113154 }
114155
156+func (h *hub) voiceUsers() map[string][]string {
157+ h.mu.Lock()
158+ defer h.mu.Unlock()
159+ out := map[string][]string{}
160+ for channelID, set := range h.voice {
161+ names := make([]string, 0, len(set))
162+ for _, name := range set {
163+ names = append(names, name)
164+ }
165+ sort.Strings(names)
166+ out[strconv.FormatInt(channelID, 10)] = names
167+ }
168+ return out
169+}
170+
115171 func (h *hub) broadcastPresence() {
116- h.broadcast(event{Type: "presence", Users: h.users(), Roster: h.roster()})
172+ h.broadcast(event{
173+ Type: "presence",
174+ Users: h.users(),
175+ Roster: h.roster(),
176+ Voice: h.voiceUsers(),
177+ })
117178 }
118179
119180 var upgrader = websocket.Upgrader{
@@ -166,9 +227,17 @@ func (c *client) readPump() {
166227 return nil
167228 })
168229 for {
169- if _, _, err := c.conn.ReadMessage(); err != nil {
230+ _, msg, err := c.conn.ReadMessage()
231+ if err != nil {
170232 return
171233 }
234+ var in wsIn
235+ if json.Unmarshal(msg, &in) != nil {
236+ continue
237+ }
238+ if in.Type == "voice" {
239+ c.hub.setVoice(c, in.ChannelID)
240+ }
172241 }
173242 }