fix: 完善直播结束通知与 WebSocket 连接

This commit is contained in:
2026-06-25 10:50:35 +08:00
parent 2281c98b1b
commit 80d4f692e0
12 changed files with 130 additions and 14 deletions

View File

@@ -18,7 +18,7 @@ const (
)
type Message struct {
Type string `json:"type"` // "chat", "system", "danmaku"
Type string `json:"type"` // "chat", "system", "danmaku", "stream_end"
Username string `json:"username"`
Content string `json:"content"`
RoomID string `json:"room_id"`
@@ -190,7 +190,15 @@ func (r *roomHub) handleBroadcast(message Message) {
delete(r.clients, client)
}
}
shouldDeleteIfIdle := message.Type == "stream_end"
if shouldDeleteIfIdle {
r.history = nil
}
r.mutex.Unlock()
if shouldDeleteIfIdle {
r.manager.deleteRoomIfIdle(r)
}
}
func (r *roomHub) handleClearHistory() {
@@ -223,6 +231,15 @@ func (h *Hub) BroadcastToRoom(msg Message) {
h.getOrCreateRoom(msg.RoomID).broadcast <- msg
}
func (h *Hub) NotifyStreamEnded(roomID string) {
h.BroadcastToRoom(Message{
Type: "stream_end",
Username: "System",
Content: "The host has ended the live stream.",
RoomID: roomID,
})
}
func (c *Client) ReadPump() {
defer func() {
c.Hub.UnregisterClient(c)

View File

@@ -172,7 +172,7 @@ func NewRTMPServer(rtmpPort string) *RTMPServer {
monitor.Warnf("Failed to mark room inactive room_id=%s: %v", roomID, err)
}
}
chat.MainHub.ClearRoomHistory(roomID)
chat.MainHub.NotifyStreamEnded(roomID)
monitor.Infof("Publishing ended for room_id=%s", roomID)
} else {
monitor.Infof("Variant publishing ended for room_id=%s path=%s", roomID, channelPath)