diff --git a/BarrageGrab/Modles/JsonEntity/BarrageMessages.cs b/BarrageGrab/Modles/JsonEntity/BarrageMessages.cs index a7d23fc..c449dc7 100644 --- a/BarrageGrab/Modles/JsonEntity/BarrageMessages.cs +++ b/BarrageGrab/Modles/JsonEntity/BarrageMessages.cs @@ -1,4 +1,8 @@ -using System.ComponentModel; +using System; +using System.ComponentModel; +using Newtonsoft.Json.Linq; +using Newtonsoft.Json; +using Org.BouncyCastle.Asn1.Crmf; namespace BarrageGrab.Modles.JsonEntity { @@ -94,6 +98,120 @@ namespace BarrageGrab.Modles.JsonEntity Type = type; ProcessName = processName; } + + /// + /// 当收到弹幕消息时执行回调 + /// + public void IfChatMsg(Action action) => IfTypedMsg(PackMsgType.弹幕消息, action); + + /// + /// 当收到点赞消息时执行回调 + /// + public void IfLikeMsg(Action action) => IfTypedMsg(PackMsgType.点赞消息, action); + + /// + /// 当收到进直播间消息时执行回调 + /// + public void IfMemberMsg(Action action) => IfTypedMsg(PackMsgType.进直播间, action); + + /// + /// 当收到关注消息时执行回调 + /// + public void IfFollowMsg(Action action) => IfTypedMsg(PackMsgType.关注消息, action); + + /// + /// 当收到礼物消息时执行回调 + /// + public void IfGiftMsg(Action action) => IfTypedMsg(PackMsgType.礼物消息, action); + + /// + /// 当收到直播间统计消息时执行回调 + /// + public void IfUserSeqMsg(Action action) => IfTypedMsg(PackMsgType.直播间统计, action); + + /// + /// 当收到粉丝团消息时执行回调 + /// + public void IfFansclubMsg(Action action) => IfTypedMsg(PackMsgType.粉丝团消息, action); + + /// + /// 当收到直播间分享消息时执行回调 + /// + public void IfShareMsg(Action action) => IfTypedMsg(PackMsgType.直播间分享, action); + + /// + /// 当收到下播消息 + /// + public void IfLiveEndMsg(Action action) => IfTypedMsg(PackMsgType.下播, action); + + /// + /// 解析所有未知类型的消息 + /// + public void IfAnyMsg(Action action) + { + // 将 Data 转换为 JObject + JObject jObject = null; + try + { + jObject = JObject.Parse(this.Data); + } + catch (Exception) + { + // 解析失败,忽略错误 + return; + } + + // 如果成功解析为 JObject,转换为 Msg 对象 + if (jObject != null) + { + var msg = jObject.ToObject(); + if (msg != null) + { + action(msg); + } + } + } + + /// + /// 泛型方法,根据 PackMsgType 解析消息对象并执行回调 + /// + private void IfTypedMsg(PackMsgType expectedType, Action action) where T : Msg + { + if (this.Type != expectedType) return; + if (this.Data == null) return; + + // 尝试将 Data 直接作为 T 类型使用 + T msg = null; + + try + { + // 如果 Data 是字符串类型 + if (this.Data is string) + { + // 先尝试解析为 JObject + try + { + JObject jObject = JObject.Parse(this.Data); + msg = jObject.ToObject(); + } + catch + { + // 如果解析 JObject 失败,尝试直接反序列化 + msg = JsonConvert.DeserializeObject(this.Data); + } + } + } + catch (Exception) + { + // 解析失败,忽略错误 + } + + // 如果成功解析消息,执行回调 + if (msg != null) + { + action(msg); + } + } } /// @@ -114,7 +232,8 @@ namespace BarrageGrab.Modles.JsonEntity /// /// 主播简要信息 /// - public RoomAnchorInfo Onwer { get; set; } + public RoomAnchorInfo Owner { get; set; } + public string Onwer { get; set; } = "该字段存在拼写错误,请修正为 ‘Owner’ 后使用"; /// /// 消息内容 @@ -131,6 +250,16 @@ namespace BarrageGrab.Modles.JsonEntity /// public string WebRoomId { get; set; } + /// + /// 房间标题 + /// + public string RoomTitle { get; set; } + + /// + /// 是否是匿名直播间 + /// + public bool IsAnonymous { get; set; } + /// /// 用户使用的 Appid ,已知 1128,8663,2329 等 ///