feat: add GetClient helper and ICE candidate callback

This commit is contained in:
jevb
2026-03-18 03:47:30 +01:00
parent 8889faddef
commit 6034bb1929
3 changed files with 46 additions and 0 deletions
+8
View File
@@ -242,6 +242,14 @@ func (h *Hub) IsUserConnected(userID int64) bool {
return ok
}
// GetClient returns the client for userID, or nil if not connected.
// Safe to call from any goroutine.
func (h *Hub) GetClient(userID int64) *Client {
h.mu.RLock()
defer h.mu.RUnlock()
return h.clients[userID]
}
// Register queues a client for registration with the hub.
func (h *Hub) Register(c *Client) {
h.register <- c
+22
View File
@@ -401,6 +401,28 @@ func TestHub_ConcurrentRegisterUnregister(t *testing.T) {
}
}
// ─── GetClient ───────────────────────────────────────────────────────────────
func TestHub_GetClient(t *testing.T) {
hub, _ := newTestHub(t)
send := make(chan []byte, 256)
client := ws.NewTestClient(hub, 42, send)
hub.Register(client)
go hub.Run()
defer hub.Stop()
time.Sleep(10 * time.Millisecond)
got := hub.GetClient(42)
if got == nil {
t.Fatal("GetClient(42) returned nil")
}
got2 := hub.GetClient(999)
if got2 != nil {
t.Fatal("GetClient(999) should return nil")
}
}
// ─── assertion helpers ────────────────────────────────────────────────────────
func assertReceived(t *testing.T, ch <-chan []byte, want []byte, label string) {
+16
View File
@@ -53,6 +53,21 @@ func (h *Hub) SetupICEMonitorForTest(c *Client, channelID int64) {
h.setupICEMonitor(c, channelID)
}
// setupICECallback registers an OnICECandidate handler on the client's
// PeerConnection to send server-generated ICE candidates to the client.
func (h *Hub) setupICECallback(c *Client, channelID int64) {
pc := c.getPC()
if pc == nil {
return
}
pc.OnICECandidate(func(candidate *webrtc.ICECandidate) {
if candidate == nil {
return // gathering complete
}
c.sendMsg(buildVoiceICE(channelID, candidate.ToJSON()))
})
}
// handleVoiceJoin processes a voice_join message.
// 1. Parses channel_id.
// 2. Checks CONNECT_VOICE permission.
@@ -129,6 +144,7 @@ func (h *Hub) handleVoiceJoin(c *Client, payload json.RawMessage) {
if pc != nil {
h.setupOnTrack(c, channelID)
h.setupICEMonitor(c, channelID)
h.setupICECallback(c, channelID)
}
state, err := h.db.GetVoiceState(c.userID)