public async Task SendDanmu(string text) { try { string sendText = $"cid={RoomId}&mid={ApiClient.UserId}&msg={text}&rnd={ApiClient.GetTimeSpan}&mode=1&pool=0&type=json&color=16777215&fontsize=25&playTime=0.0"; var url = $"https://api.live.bilibili.com/api/sendmsg?access_key={ApiClient.AccessKey}&actionKey=appkey&appkey={ApiClient.AndroidKey.Appkey}&build={ApiClient.build}&device=android&mobi_app=android&platform=android&ts={ApiClient.GetTimeSpan}"; url += "&sign=" + ApiClient.GetSign(url); string result = await HttpClient.PostResults(url, sendText); JObject jb = JObject.Parse(result); var model = new EventModel { PrintMsg = "发送弹幕结果:" + ((int)jb["code"] == 0), Send = false }; PrintEvt?.Invoke(this, model); } catch (Exception ex) { LogManager.Instance.LogError("SendDanmu", ex); } }
private async void ProcessDanmaku(int action, byte[] buffer) { switch (action) { case 5: { var json = Encoding.UTF8.GetString(buffer, 0, buffer.Length); try { var dama = new DanmakuModel(json, 2); switch (dama.MsgType) { case MsgTypeEnum.Comment: { var model = new EventModel(); if (dama.CommentText.StartsWith("#点歌")) { var values = dama.CommentText.Replace("#点歌", "").Split(' '); var sv = values.Where(x => !string.IsNullOrEmpty(x)).ToArray(); var musicInfo = await MusicClient.GetSongList(sv); musicInfo.SongChoser = dama.UserName; if (musicInfo.ErrorMsg.IsNotEmpty()) { model.PrintMsg = string.Format("用户:{0} 点歌:{1} 失败,可能原因:{2}", dama.UserName, dama.CommentText, musicInfo.ErrorMsg); PrintEvt?.Invoke(this, model); } else { model.PrintMsg = string.Format("用户:{0} 点歌:{1} 成功,加入队列中", dama.UserName, dama.CommentText); model.MusicInfo = musicInfo; PlaySongEvt?.Invoke(this, model); } } else { model.PrintMsg = string.Format("用户:{0} 发送了一条弹幕:{1}", dama.UserName, dama.CommentText); PrintEvt?.Invoke(this, model); } break; } case MsgTypeEnum.GiftSend: { var model = new EventModel { PrintMsg = string.Format("谢谢{0}赠送的{1}个{2},{3}表示灰常喜欢,么么哒~", dama.UserName, dama.GiftCount, dama.GiftName, "name".GetConfig()), DateTime = DateTime.Now }; PlaySongEvt?.Invoke(this, model); break; } } } catch (Exception ex) { LogManager.Instance.LogError("ProcessDanmaku", ex); } break; } } }