Youtube Livestream Api LiveChatMessages列表

时间:2017-10-29 14:13:49

标签: youtube-api youtube-data-api

我试图从Youtube Livestream获取消息,但是我没有收到新的消息。包含NextPageToken。 有时我会收到新消息,但需要5-10分钟。 Youtube聊天发送工作也很好。 有什么想法吗?

这是来自文档:https://developers.google.com/youtube/v3/live/docs/liveChatMessages/list

private async Task GetMessagesAsync(string liveChatId, string nextPageToken, long? pollingIntervalMillis)
{
    liveChatId = "EiEKGFVDVUQ3WGNXTk92SlpvaHFMM3dZTi1uZxIFL2xpdmU";

    if (!updatingChat)
    {
        if (!string.IsNullOrEmpty(liveChatId))
        {
            newMessages = true;
            var chatMessages = youTubeService.LiveChatMessages.List(liveChatId, "id,snippet,authorDetails");
            var chatResponse = await chatMessages.ExecuteAsync();

            PageInfo pageInfo = chatResponse.PageInfo;

            newMessages = false;

            if (pageInfo.TotalResults.HasValue)
            {
                if (!prevCount.Equals(pageInfo.TotalResults.Value))
                {
                    prevCount = pageInfo.TotalResults.Value;
                    newMessages = true;
                }
            }

            if (newMessages)
            {
                Messages = new List<YouTubeMessage>();
                foreach (var chatMessage in chatResponse.Items)
                {
                    string messageId = chatMessage.Id;
                    string displayName = chatMessage.AuthorDetails.DisplayName;
                    string displayMessage = chatMessage.Snippet.DisplayMessage;
                    string NextPagetoken = chatResponse.NextPageToken;

                    YouTubeMessage message = new YouTubeMessage(messageId, displayName, displayMessage);

                    if (!Messages.Contains(message))
                    {
                        Messages.Add(message);
                        string output = "[" + displayName + "]: " + displayMessage;
                        Console.WriteLine(time + output);
                    }
                }
            }
            await GetMessagesAsync(liveChatId, chatResponse.NextPageToken, chatResponse.PollingIntervalMillis);
        }
    }
    updatingChat = false;
    await Task.Delay(100);
}

public async Task YouTubeChatSend(string message)
{
    try
    {
        LiveChatMessage liveMessage = new LiveChatMessage();

        liveMessage.Snippet = new LiveChatMessageSnippet()
        {
            LiveChatId = "EiEKGFVDVUQ3WGNXTk92SlpvaHFMM3dZTi1uZxIFL2xpdmU",
            Type = "textMessageEvent",
            TextMessageDetails = new LiveChatTextMessageDetails() { MessageText = message }
        };

        var insert = this.youTubeService.LiveChatMessages.Insert(liveMessage, "snippet");
        var response = await insert.ExecuteAsync();

        if (response != null)
        {

        }
    }
    catch
    {
        Console.WriteLine("Failed to chat send");
    }
}

0 个答案:

没有答案