예제 #1
0
        public async Task PostComment(string comment, uint userId, string command, TimeSpan elapsedTime)
        {
            var postkey = await NiconicoContext.Live.GetPostKeyAsync(ThreadIdNumber, CommentCount / 100);

            if (string.IsNullOrEmpty(postkey))
            {
                CommentPosted?.Invoke(false /* failed post comment */, null);
                return;
            }

            PostChat chat = new PostChat();

            chat.Comment  = comment;
            chat.ThreadId = ThreadId;
            chat.PostKey  = postkey;
            chat.Ticket   = _Ticket;
            chat.Mail     = command;
            chat.Premium  = "";
            chat.UserId   = userId.ToString();

            var time = (uint)((elapsedTime + TimeSpan.FromSeconds(1)).TotalSeconds * 100);

            chat.Vpos = time.ToString();
            try
            {
                var chatSerializer = new XmlSerializer(typeof(PostChat));

                using (var releaser = await _NetworkStreamLock.LockAsync())
                {
                    var writer = _NetworkStream.AsOutputStream().AsStreamForWrite();
                    chatSerializer.Serialize(writer, chat);
                    writer.WriteByte(0);
                    writer.Flush();
                }

                _LastPostChat = chat;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());

                CommentPosted?.Invoke(false /* failed post comment */, null);
            }
        }
예제 #2
0
        private async void _NicoLiveCommentReciever_CommentPosted(bool isSuccess, PostChat chat)
        {
            Debug.WriteLine("コメント投稿結果:+" + isSuccess);

            await HohoemaApp.UIDispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
            {
                PostCommentResult?.Invoke(this, isSuccess);

                if (isSuccess)
                {
                    _LiveComments.Add(new Chat()
                    {
                        User_id   = chat.UserId,
                        Vpos      = chat.Vpos,
                        No        = "",
                        Mail      = chat.Mail,
                        Text      = chat.Comment,
                        Anonymity = (chat.Mail?.Contains("184") ?? false) ? "true" : null
                    });
                }
            });
        }
예제 #3
0
        private void ParseChatXml(string xml)
        {
            var xmlDoc      = XDocument.Parse(xml);
            var xmlRoot     = xmlDoc.Root;
            var elementName = xmlRoot.Name.LocalName;

            Debug.Write(elementName);
            Debug.Write(" -> ");
            if (elementName == "thread")
            {
                Debug.Write("connect success");
                IsConnected = true;

                // <thread ticket="{チケット}" server_time="{サーバー時刻}" last_res="{送信される過去のコメント数?(NECOで使用してないので不明)}">
                var  serverTimeText = xmlRoot.Attribute(XName.Get("server_time")).Value;
                long serverTime;
                if (long.TryParse(serverTimeText, out serverTime))
                {
                    _ServerTime = serverTime;
                }

                var ticketText = xmlRoot.Attribute(XName.Get("ticket")).Value;
                _Ticket = ticketText;

                Connected?.Invoke(this, new CommentServerConnectedEventArgs()
                {
                    Ticket     = _Ticket,
                    ServerTime = (int)_ServerTime,
                    Thread     = this.ThreadId
                });
            }
            else if (elementName == "chat_result")
            {
                // <chat_result status="{コメント投稿要求の返答}" />
                var result = xmlRoot.Attribute(XName.Get("status")).Value;

                /*
                 *              0 = 投稿に成功した
                 *              1 = 投稿に失敗した(短時間に同じ内容のコメントを投稿しようとした、パラメータが間違っている、他)
                 *              4 = 投稿に失敗した(ごく短時間にコメントを連投しようとした、パラメータが間違っている、他)
                 */

                Debug.Write(result);
                Debug.Write(result == "0" ? " (success)" : " (failed)");

                CommentPosted?.Invoke(this, new CommentPostedEventArgs()
                {
                    ChatResult = (ChatResult)int.Parse(result),
                });
                _LastPostChat = null;
            }
            else if (elementName == "chat")
            {
                // _LiveComments.Add(chat);
                // <chat anonymity="{184か}" no="{コメントの番号}" date="{コメントが投稿されたリアル時間?}" mail="{コマンド}" premium="{プレミアムID}" thread="{スレッドID}" user_id="{ユーザーID}" vpos="{コメントが投稿された生放送の時間}" score="{NGスコア}">{コメント}</chat>\0
                try
                {
                    var chatSerializer = new XmlSerializer(typeof(Chat));
                    using (var readerStream = new StringReader(xml))
                    {
                        var chat = chatSerializer.Deserialize(readerStream) as Chat;
                        if (chat != null)
                        {
                            Debug.Write(chat.Text);

                            OperationCommnad officialCommand          = null;
                            string[]         officialCommandArguments = null;
                            if (ChcekOfficialOperationComment(chat, out officialCommand, out officialCommandArguments))
                            {
                                var args = new NicoLiveOperationCommandEventArgs()
                                {
                                    CommandType = officialCommand.CommandType,
                                    Arguments   = officialCommandArguments,
                                    Chat        = chat
                                };

//                                OperationCommandRecieved?.Invoke(this, args);
                            }
                            else
                            {
                                var liveChatData = new LiveChatData()
                                {
                                    No          = (int)chat.GetCommentNo(),
                                    Thread      = chat.Thread,
                                    IsAnonymity = chat.GetAnonymity(),
                                    Content     = chat.Text,
                                    Mail        = chat.Mail,
                                    UserId      = chat.User_id,
                                    Vpos        = (int)chat.GetVpos(),
                                    Date        = int.Parse(chat.Date),
                                };
                                CommentRecieved?.Invoke(this, new CommentRecievedEventArgs()
                                {
                                    Chat = liveChatData
                                });
                            }
                        }
                    }
                }
                catch { }
            }
            else
            {
                Debug.WriteLine($"not supproted");
                Debug.Write(" -> ");
                Debug.Write(xml);
            }
        }
예제 #4
0
        private void ParseLiveCommentServerResponse(string recievedString)
        {
            if (string.IsNullOrWhiteSpace(recievedString))
            {
                Debug.Write($"IGNORE");
                return;
            }

            if (!recievedString.StartsWith("<") || !recievedString.EndsWith(">"))
            {
                // Note: 寄り厳密にXMLフォーマットチェックをやるなら
                // <>の数が同数であることをチェックする
                Debug.Write($"illigal format, required XML");
                Debug.Write($" -> ");
                Debug.Write(recievedString);
                return;
            }

            var xmlDoc      = XDocument.Parse(recievedString);
            var xmlRoot     = xmlDoc.Root;
            var elementName = xmlRoot.Name.LocalName;

            Debug.Write(elementName);
            Debug.Write(" -> ");
            if (elementName == "thread")
            {
                Debug.Write("connect success");
                IsCommentServerConnected = true;

                // <thread ticket="{チケット}" server_time="{サーバー時刻}" last_res="{送信される過去のコメント数?(NECOで使用してないので不明)}">
                var  serverTimeText = xmlRoot.Attribute(XName.Get("server_time")).Value;
                long serverTime;
                if (long.TryParse(serverTimeText, out serverTime))
                {
                    _ServerTime = serverTime;
                }

                var ticketText = xmlRoot.Attribute(XName.Get("ticket")).Value;
                _Ticket = ticketText;

                CommentServerConnected?.Invoke();
            }
            else if (elementName == "chat_result")
            {
                // <chat_result status="{コメント投稿要求の返答}" />
                var result = xmlRoot.Attribute(XName.Get("status")).Value;

                /*
                 * 0 = 投稿に成功した
                 * 1 = 投稿に失敗した(短時間に同じ内容のコメントを投稿しようとした、パラメータが間違っている、他)
                 * 4 = 投稿に失敗した(ごく短時間にコメントを連投しようとした、パラメータが間違っている、他)
                 */

                Debug.Write(result);
                Debug.Write(result == "0" ? " (success)" : " (failed)");

                CommentPosted?.Invoke(result == "0", _LastPostChat);
                _LastPostChat = null;
            }
            else if (elementName == "chat")
            {
                // _LiveComments.Add(chat);
                // <chat anonymity="{184か}" no="{コメントの番号}" date="{コメントが投稿されたリアル時間?}" mail="{コマンド}" premium="{プレミアムID}" thread="{スレッドID}" user_id="{ユーザーID}" vpos="{コメントが投稿された生放送の時間}" score="{NGスコア}">{コメント}</chat>\0
                try
                {
                    var chatSerializer = new XmlSerializer(typeof(Chat));
                    using (var readerStream = new StringReader(recievedString))
                    {
                        var chat = chatSerializer.Deserialize(readerStream) as Chat;
                        if (chat != null)
                        {
                            Debug.Write(chat.Text);

                            OperationCommnad officialCommand          = null;
                            string[]         officialCommandArguments = null;
                            if (ChcekOfficialOperationComment(chat, out officialCommand, out officialCommandArguments))
                            {
                                var args = new NicoLiveOperationCommandEventArgs()
                                {
                                    CommandType = officialCommand.CommandType,
                                    Arguments   = officialCommandArguments,
                                    Chat        = chat
                                };

                                OperationCommandRecieved?.Invoke(this, args);
                            }
                            else
                            {
                                CommentRecieved?.Invoke(chat);
                            }
                        }
                    }
                }
                catch { }
            }
            else
            {
                Debug.WriteLine($"not supproted");
                Debug.Write(" -> ");
                Debug.Write(recievedString);
            }
        }