use of com.google.api.services.youtube.model.LiveChatMessageListResponse in project api-samples by youtube.
the class ListLiveChatMessages method listChatMessages.
/**
* Lists live chat messages, polling at the server supplied interval. Owners and moderators of a
* live chat will poll at a faster rate.
*
* @param liveChatId The live chat id to list messages from.
* @param nextPageToken The page token from the previous request, if any.
* @param delayMs The delay in milliseconds before making the request.
*/
private static void listChatMessages(final String liveChatId, final String nextPageToken, long delayMs) {
System.out.println(String.format("Getting chat messages in %1$.3f seconds...", delayMs * 0.001));
Timer pollTimer = new Timer();
pollTimer.schedule(new TimerTask() {
@Override
public void run() {
try {
// Get chat messages from YouTube
LiveChatMessageListResponse response = youtube.liveChatMessages().list(liveChatId, "snippet, authorDetails").setPageToken(nextPageToken).setFields(LIVE_CHAT_FIELDS).execute();
// Display messages and super chat details
List<LiveChatMessage> messages = response.getItems();
for (int i = 0; i < messages.size(); i++) {
LiveChatMessage message = messages.get(i);
LiveChatMessageSnippet snippet = message.getSnippet();
System.out.println(buildOutput(snippet.getDisplayMessage(), message.getAuthorDetails(), snippet.getSuperChatDetails()));
}
// Request the next page of messages
listChatMessages(liveChatId, response.getNextPageToken(), response.getPollingIntervalMillis());
} catch (Throwable t) {
System.err.println("Throwable: " + t.getMessage());
t.printStackTrace();
}
}
}, delayMs);
}
Aggregations