feat(call): enhance call service with reliable message delivery and automatic cleanup
- Introduced a new method, PublishToUserOnlineReliable, to ensure critical signaling messages are reliably sent to online users. - Updated CallService to include a cleanup mechanism for expired calls, improving resource management and user experience. - Enhanced call acceptance logic to utilize optimistic locking for state updates, ensuring accurate call status handling across devices. - Refactored wire generation to accommodate the new database dependency in CallService, streamlining service initialization.
This commit is contained in:
@@ -200,6 +200,44 @@ func (h *Hub) PublishToUserOnline(userID string, eventType string, payload inter
|
||||
return true
|
||||
}
|
||||
|
||||
// PublishToUserOnlineReliable 可靠地发送事件给在线用户(阻塞发送)
|
||||
// 用于重要的信令消息(如 SDP, ICE candidate),确保消息送达
|
||||
// 如果用户不在线,返回 false
|
||||
func (h *Hub) PublishToUserOnlineReliable(userID string, eventType string, payload interface{}) bool {
|
||||
h.mu.RLock()
|
||||
targets := make([]*Client, 0, len(h.clients[userID]))
|
||||
for _, c := range h.clients[userID] {
|
||||
targets = append(targets, c)
|
||||
}
|
||||
h.mu.RUnlock()
|
||||
|
||||
if len(targets) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
msg := ResponseMessage{
|
||||
EventID: h.NextID(),
|
||||
Type: eventType,
|
||||
TS: time.Now().UnixMilli(),
|
||||
Payload: payload,
|
||||
}
|
||||
data, err := json.Marshal(msg)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// 阻塞发送,确保消息送达
|
||||
for _, c := range targets {
|
||||
select {
|
||||
case <-c.Quit:
|
||||
// 客户端已断开,跳过
|
||||
case c.Send <- data:
|
||||
// 消息已发送
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// publish 内部发布方法
|
||||
func (h *Hub) publish(userID string, ev Event) {
|
||||
h.mu.Lock()
|
||||
|
||||
Reference in New Issue
Block a user