房间号相关修复,视频’音频时长修复

This commit is contained in:
2025-07-25 16:02:35 +08:00
parent 5c4c8909a9
commit d77f06ad9a
2 changed files with 38 additions and 22 deletions

View File

@@ -686,6 +686,7 @@ func (c *WebSocketController) SendMessageHandler(ctx *gin.Context) {
clientMsg := models.ClientReceivedMessage{
SenderID: "system",
RoomID: payload.RoomId,
ReceiverID: payload.TargetClientID,
SenderUserID: payload.SenderUserID,
ReceiverUserID: payload.ReceiverUserID,
@@ -784,25 +785,33 @@ func (c *WebSocketController) SendToUserHandler(ctx *gin.Context) {
RoomId: payload.RoomId,
SenderUserID: payload.SenderUserID,
ReceiverUserID: payload.ReceiverUserID,
Duration: payload.Duration,
MessageType: payload.MessageType,
MessageContent: payload.MessageContent,
CallID: payload.CallID,
CallStatus: payload.CallStatus,
}
var savedID = 0
// 保存到数据库
if err := c.DB.Create(&chatMsg).Error; err != nil {
log.Printf("❌ 保存消息到数据库失败: %v", err)
} else {
// 获取消息id
log.Printf("💾 消息保存到数据库成功: ID=%d", chatMsg.ID)
savedID = int(chatMsg.ID)
}
// 创建基础消息结构
clientMsg := models.ClientReceivedMessage{
SenderID: "system",
ReceiverID: "",
ID: savedID,
RoomID: payload.RoomId,
SenderUserID: payload.SenderUserID,
ReceiverUserID: payload.ReceiverUserID,
Duration: payload.Duration,
MessageType: payload.MessageType,
Content: payload.MessageContent,
SendTime: time.Now().Format(time.RFC3339),
@@ -1026,29 +1035,29 @@ func (c *WebSocketController) sendMessageToUser(userID string, message models.Cl
if len(clientIDs) == 0 {
log.Printf("⚠️ 用户无在线客户端: UserID=%s", userID)
return fmt.Errorf("用户 %s 无在线客户端", userID)
}
//return fmt.Errorf("用户 %s 无在线客户端", userID)
} else {
log.Printf("📤📤📤📤📤📤📤📤 发送消息给用户: \n UserID=%s \n ClientCount=%d \n MsgType=%d",
userID, len(clientIDs), message.MessageType)
log.Printf("📤📤📤📤📤📤📤📤 发送消息给用户: \n UserID=%s \n ClientCount=%d \n MsgType=%d",
userID, len(clientIDs), message.MessageType)
var lastErr error
successCount := 0
var lastErr error
successCount := 0
for _, clientID := range clientIDs {
if err := c.sendMessageToClient(clientID, message); err != nil {
log.Printf("⚠️ 发送到客户端失败: %v | ClientID=%s", err, clientID)
lastErr = err
} else {
successCount++
for _, clientID := range clientIDs {
if err := c.sendMessageToClient(clientID, message); err != nil {
log.Printf("⚠️ 发送到客户端失败: %v | ClientID=%s", err, clientID)
lastErr = err
} else {
successCount++
}
}
}
if successCount == 0 {
return fmt.Errorf("所有客户端发送失败,最后错误: %v", lastErr)
}
if successCount == 0 {
return fmt.Errorf("所有客户端发送失败,最后错误: %v", lastErr)
}
log.Printf("✅ 用户消息发送完成: \n UserID=%s \n 成功=%d/%d", userID, successCount, len(clientIDs))
log.Printf("✅ 用户消息发送完成: \n UserID=%s \n 成功=%d/%d", userID, successCount, len(clientIDs))
}
return nil
}

View File

@@ -8,8 +8,10 @@ type SendMessagePayload struct {
TargetClientID string `json:"target_client_id"` // 目标客户端ID
SenderUserID string `json:"sender_user_id"` // 发送者用户ID
ReceiverUserID string `json:"receiver_user_id"` // 接收者用户ID
MessageType int `json:"message_type"` // 消息类型(0-8)
MessageType int `json:"message_type"` // 消息类型(0-9)
MessageContent string `json:"message_content"` // 消息内容
RoomId string `json:"room_id"` // 发送者用户ID
Duration int `json:"duration"` // 时长(音频、视频、音视频通话)
// 通话专用字段
CallID string `json:"call_id,omitempty"` // 通话唯一ID
CallStatus string `json:"call_status,omitempty"` // 通话状态:invite/accepted/rejected/ended/candidate
@@ -18,9 +20,10 @@ type SendMessagePayload struct {
// 通过用户ID发送消息的结构
type SendToUserPayload struct {
RoomId string `json:"room_id"` // 发送者用户ID
Duration int `json:"duration"` // 时长(音频、视频、音视频通话)
SenderUserID string `json:"sender_user_id"` // 发送者用户ID
ReceiverUserID string `json:"receiver_user_id"` // 接收者用户ID
MessageType int `json:"message_type"` // 消息类型(0-8)
MessageType int `json:"message_type"` // 消息类型(0-9)
MessageContent string `json:"message_content"` // 消息内容
// 通话专用字段
CallID string `json:"call_id,omitempty"` // 通话唯一ID
@@ -33,8 +36,9 @@ type XkChatMessage struct {
RoomId string `gorm:"type:varchar(100);index" json:"room_id"` // 房间ID
SenderUserID string `gorm:"type:varchar(100);index" json:"sender_user_id"` // 发送者用户ID
ReceiverUserID string `gorm:"type:varchar(100);index" json:"receiver_user_id"` // 接收者用户ID
MessageType int `gorm:"type:int" json:"message_type"` // 消息类型(0-8)
MessageType int `gorm:"type:int" json:"message_type"` // 消息类型(0-9)
MessageContent string `gorm:"type:text" json:"message_content"` // 消息内容
Duration int `json:"duration"` // 时长(音频、视频、音视频通话)
CallID string `gorm:"type:varchar(100);index" json:"call_id"` // 通话唯一ID
CallStatus string `gorm:"type:varchar(50)" json:"call_status"` // 通话状态
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"` // 创建时间
@@ -42,13 +46,16 @@ type XkChatMessage struct {
// 客户端接收消息结构(最终格式)
type ClientReceivedMessage struct {
ID int `json:"id"` // 房间号
RoomID string `json:"room_id"` // 房间号
SenderID string `json:"sender_id"` // 发送者连接ID
ReceiverID string `json:"receiver_id"` // 接收者连接ID
SenderUserID string `json:"sender_user_id"` // 发送者用户ID
ReceiverUserID string `json:"receiver_user_id"` // 接收者用户ID
MessageType int `json:"message_type"` // 消息类型(0-8)
MessageType int `json:"message_type"` // 消息类型(0-9)
Content string `json:"content"` // 消息内容
SendTime string `json:"send_time"` // 发送时间
Duration int `json:"duration"` // 时长(音频、视频、音视频通话)
// 通话专用字段
CallID string `json:"call_id,omitempty"` // 通话唯一ID