Skip to content

Commit e741fc2

Browse files
author
Jim Rogers
committed
Remove fixed period poll sample
1 parent cb3a381 commit e741fc2

1 file changed

Lines changed: 4 additions & 69 deletions

File tree

java/src/main/java/com/google/api/services/samples/youtube/cmdline/live/ListLiveChatMessages.java

Lines changed: 4 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,6 @@ public class ListLiveChatMessages {
5757
*/
5858
private static YouTube youtube;
5959

60-
/**
61-
* A timer used to schedule message retrieval.
62-
*/
63-
private static Timer pollTimer;
64-
6560
/**
6661
* Lists live chat messages and SuperChat details from a live broadcast.
6762
*
@@ -94,11 +89,7 @@ public static void main(String[] args) {
9489
System.exit(1);
9590
}
9691

97-
/**
98-
* List live chat messages with poll interval from server. Alternatively, messages
99-
* may be requested at a fixed interval with listChatMessagesFixedPeriod, e.g.
100-
* listChatMessagesFixedPeriod(liveChatId, 1000, 0)
101-
*/
92+
// Get live chat messages
10293
listChatMessages(liveChatId, null, 0);
10394
} catch (GoogleJsonResponseException e) {
10495
System.err
@@ -116,7 +107,8 @@ public static void main(String[] args) {
116107
}
117108

118109
/**
119-
* Lists live chat messages, polling at the server supplied interval.
110+
* Lists live chat messages, polling at the server supplied interval. Owners and moderators of a
111+
* live chat will poll at a faster rate.
120112
*
121113
* @param liveChatId The live chat id to list messages from.
122114
* @param nextPageToken The page token from the previous request, if any.
@@ -128,7 +120,7 @@ private static void listChatMessages(
128120
long delayMs) {
129121
System.out.println(
130122
String.format("Getting chat messages in %1$.3f seconds...", delayMs * 0.001));
131-
pollTimer = new Timer();
123+
Timer pollTimer = new Timer();
132124
pollTimer.schedule(
133125
new TimerTask() {
134126
@Override
@@ -166,63 +158,6 @@ public void run() {
166158
}, delayMs);
167159
}
168160

169-
/**
170-
* Lists live chat messages, polling at the client supplied interval. This method is not
171-
* recommended because it will consume more API usage, but it may be necessary in some
172-
* applications that require lower latency. Page tokens do not work when polling faster than the
173-
* server supplied interval, so we need to keep track of the publish time to avoid duplicate
174-
* message output. Message ids will not work for tracking the last received message because
175-
* messages may be removed from chat.
176-
*
177-
* @param liveChatId The live chat id to list messages from.
178-
* @param periodMs The fixed interval to poll messages.
179-
* @param minPublishTime The minimum message time to output.
180-
*/
181-
private static void listChatMessagesFixedPeriod(
182-
final String liveChatId,
183-
final long periodMs,
184-
final long minPublishTime) {
185-
System.out.println(
186-
String.format("Getting chat messages in %1$.3f seconds...", periodMs * 0.001));
187-
pollTimer = new Timer();
188-
pollTimer.schedule(
189-
new TimerTask() {
190-
@Override
191-
public void run() {
192-
try {
193-
// Get chat messages from YouTube
194-
LiveChatMessageListResponse response = youtube
195-
.liveChatMessages()
196-
.list(liveChatId, "snippet, authorDetails")
197-
.setFields(LIVE_CHAT_FIELDS)
198-
.execute();
199-
200-
// Display messages and super chat details
201-
long maxPublishTime = minPublishTime;
202-
List<LiveChatMessage> messages = response.getItems();
203-
for (int i = 0; i < messages.size(); i++) {
204-
LiveChatMessage message = messages.get(i);
205-
LiveChatMessageSnippet snippet = message.getSnippet();
206-
long publishTime = snippet.getPublishedAt().getValue();
207-
if (publishTime >= minPublishTime) {
208-
System.out.println(buildOutput(
209-
snippet.getDisplayMessage(),
210-
message.getAuthorDetails(),
211-
snippet.getSuperChatDetails()));
212-
}
213-
maxPublishTime = Math.max(maxPublishTime, publishTime);
214-
}
215-
216-
// Request the next page of messages
217-
listChatMessagesFixedPeriod(liveChatId, periodMs, maxPublishTime + 1);
218-
} catch (Throwable t) {
219-
System.err.println("Throwable: " + t.getMessage());
220-
t.printStackTrace();
221-
}
222-
}
223-
}, periodMs);
224-
}
225-
226161
/**
227162
* Formats a chat message for console output.
228163
*

0 commit comments

Comments
 (0)