@@ -41,12 +41,12 @@ type client struct {
4141 type hub struct {
4242 mu sync.Mutex
4343 clients map[int64]map[*client]bool
44- voice map[int64]map[int64]string
44+ voice map[int64]map[*client]bool
4545 db *sql.DB
4646 }
4747
4848 func newHub(db *sql.DB) *hub {
49- return &hub{clients: map[int64]map[*client]bool{}, voice: map[int64]map[int64]string{}, db: db}
49+ return &hub{clients: map[int64]map[*client]bool{}, voice: map[int64]map[*client]bool{}, db: db}
5050 }
5151
5252 func (h *hub) register(c *client) {
@@ -79,7 +79,7 @@ func (h *hub) clearVoiceLocked(c *client) {
7979 return
8080 }
8181 if set := h.voice[c.voiceID]; set != nil {
82- delete(set, c.userID)
82+ delete(set, c)
8383 if len(set) == 0 {
8484 delete(h.voice, c.voiceID)
8585 }
@@ -88,15 +88,17 @@ func (h *hub) clearVoiceLocked(c *client) {
8888 }
8989
9090 // setVoice moves a client into a voice channel (0 for none) and rebroadcasts.
91+// Occupancy is keyed by connection, not user, so one tab leaving a channel
92+// does not evict another tab that is still in it.
9193 func (h *hub) setVoice(c *client, channelID int64) {
9294 h.mu.Lock()
9395 h.clearVoiceLocked(c)
9496 c.voiceID = channelID
9597 if channelID != 0 {
9698 if h.voice[channelID] == nil {
97- h.voice[channelID] = map[int64]string{}
99+ h.voice[channelID] = map[*client]bool{}
98100 }
99- h.voice[channelID][c.userID] = c.username
101+ h.voice[channelID][c] = true
100102 }
101103 h.mu.Unlock()
102104 h.broadcastPresence()
@@ -158,9 +160,13 @@ func (h *hub) voiceUsers() map[string][]string {
158160 defer h.mu.Unlock()
159161 out := map[string][]string{}
160162 for channelID, set := range h.voice {
163+ seen := map[string]bool{}
161164 names := make([]string, 0, len(set))
162- for _, name := range set {
163- names = append(names, name)
165+ for c := range set {
166+ if !seen[c.username] {
167+ seen[c.username] = true
168+ names = append(names, c.username)
169+ }
164170 }
165171 sort.Strings(names)
166172 out[strconv.FormatInt(channelID, 10)] = names
@@ -177,28 +183,32 @@ func (h *hub) broadcastPresence() {
177183 })
178184 }
179185
186+// checkOrigin allows same-host origins. It lives on the (shared) upgrader, so
187+// it must not be reassigned per request: concurrent upgrades would race.
188+func checkOrigin(r *http.Request) bool {
189+ origin := r.Header.Get("Origin")
190+ if origin == "" {
191+ return true
192+ }
193+ u, err := url.Parse(origin)
194+ if err != nil {
195+ return false
196+ }
197+ host := r.Host
198+ if h, _, err := net.SplitHostPort(host); err == nil {
199+ host = h
200+ }
201+ return u.Hostname() == host
202+}
203+
180204 var upgrader = websocket.Upgrader{
181205 ReadBufferSize: 1024,
182206 WriteBufferSize: 1024,
207+ CheckOrigin: checkOrigin,
183208 }
184209
185210 func (s *Server) handleWS(c *gin.Context) {
186211 user := c.MustGet("user").(User)
187- upgrader.CheckOrigin = func(r *http.Request) bool {
188- origin := r.Header.Get("Origin")
189- if origin == "" {
190- return true
191- }
192- u, err := url.Parse(origin)
193- if err != nil {
194- return false
195- }
196- host := r.Host
197- if h, _, err := net.SplitHostPort(host); err == nil {
198- host = h
199- }
200- return u.Hostname() == host
201- }
202212 conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
203213 if err != nil {
204214 return
@@ -211,7 +221,7 @@ func (s *Server) handleWS(c *gin.Context) {
211221
212222 const (
213223 writeWait = 10 * time.Second
214- pongWait = 60 * time.Second
224+ pongWait = 30 * time.Second
215225 pingPeriod = (pongWait * 9) / 10
216226 )
217227
@@ -235,8 +245,17 @@ func (c *client) readPump() {
235245 if json.Unmarshal(msg, &in) != nil {
236246 continue
237247 }
238- if in.Type == "voice" {
248+ switch in.Type {
249+ case "voice":
239250 c.hub.setVoice(c, in.ChannelID)
251+ case "ping":
252+ // Application-level heartbeat: the browser answers protocol pings
253+ // itself, so a client cannot see them. This lets it detect a
254+ // half-open socket (sleep, dead network) instead of trusting it.
255+ select {
256+ case c.send <- []byte(`{"type":"pong"}`):
257+ default:
258+ }
240259 }
241260 }
242261 }