Exemplo n.º 1
0
        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;
            }
            }
        }