Skip to content

Commit

Permalink
fix: enable ws rfc ping for chat and pubsub (#539)
Browse files Browse the repository at this point in the history
* fix: enable ws rfc ping for chat and pubsub

* fix: apply ws ping period to pubsub in client pool builder

Co-authored-by: Sidd <iProdigy@users.noreply.github.com>
  • Loading branch information
PhilippHeuer and iProdigy committed Feb 14, 2022
1 parent 8c18d85 commit af5744a
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 7 deletions.
11 changes: 9 additions & 2 deletions chat/src/main/java/com/github/twitch4j/chat/TwitchChat.java
Expand Up @@ -232,6 +232,11 @@ public class TwitchChat implements ITwitchChat {
*/
protected final long chatJoinTimeout;

/**
* WebSocket RFC Ping Period in ms (0 = disabled)
*/
private final int wsPingPeriod;

/**
* Cache of recent number of join attempts for each channel
*/
Expand Down Expand Up @@ -278,8 +283,9 @@ public class TwitchChat implements ITwitchChat {
* @param removeChannelOnJoinFailure Whether channels should be removed after a join failure
* @param maxJoinRetries Maximum join retries per channel
* @param chatJoinTimeout Minimum milliseconds to wait after a join attempt
* @param wsPingPeriod WebSocket Ping Period
*/
public TwitchChat(EventManager eventManager, CredentialManager credentialManager, OAuth2Credential chatCredential, String baseUrl, boolean sendCredentialToThirdPartyHost, Collection<String> commandPrefixes, Integer chatQueueSize, Bucket ircMessageBucket, Bucket ircWhisperBucket, Bucket ircJoinBucket, Bucket ircAuthBucket, ScheduledThreadPoolExecutor taskExecutor, long chatQueueTimeout, ProxyConfig proxyConfig, boolean autoJoinOwnChannel, boolean enableMembershipEvents, Collection<String> botOwnerIds, boolean removeChannelOnJoinFailure, int maxJoinRetries, long chatJoinTimeout) {
public TwitchChat(EventManager eventManager, CredentialManager credentialManager, OAuth2Credential chatCredential, String baseUrl, boolean sendCredentialToThirdPartyHost, Collection<String> commandPrefixes, Integer chatQueueSize, Bucket ircMessageBucket, Bucket ircWhisperBucket, Bucket ircJoinBucket, Bucket ircAuthBucket, ScheduledThreadPoolExecutor taskExecutor, long chatQueueTimeout, ProxyConfig proxyConfig, boolean autoJoinOwnChannel, boolean enableMembershipEvents, Collection<String> botOwnerIds, boolean removeChannelOnJoinFailure, int maxJoinRetries, long chatJoinTimeout, int wsPingPeriod) {
this.eventManager = eventManager;
this.credentialManager = credentialManager;
this.chatCredential = chatCredential;
Expand All @@ -299,6 +305,7 @@ public TwitchChat(EventManager eventManager, CredentialManager credentialManager
this.removeChannelOnJoinFailure = removeChannelOnJoinFailure;
this.maxJoinRetries = maxJoinRetries;
this.chatJoinTimeout = chatJoinTimeout;
this.wsPingPeriod = wsPingPeriod;

// Create WebSocketFactory and apply proxy settings
this.webSocketFactory = new WebSocketFactory();
Expand Down Expand Up @@ -511,6 +518,7 @@ private void createWebSocket() {
try {
// WebSocket
this.webSocket = webSocketFactory.createSocket(this.baseUrl);
this.webSocket.setPingInterval(wsPingPeriod);

// WebSocket Listeners
this.webSocket.clearListeners();
Expand Down Expand Up @@ -617,7 +625,6 @@ public void onDisconnected(WebSocket websocket,
log.info("Disconnected from Twitch IRC (WebSocket)!");
}
}

});

} catch (Exception ex) {
Expand Down
Expand Up @@ -221,6 +221,12 @@ public class TwitchChatBuilder {
@With
private long chatJoinTimeout = 2000L;

/**
* WebSocket RFC Ping Period in ms (0 = disabled)
*/
@With
private int wsPingPeriod = 15_000;

/**
* Initialize the builder
*
Expand Down Expand Up @@ -275,7 +281,7 @@ public TwitchChat build() {
ircAuthBucket = userId == null ? TwitchChatLimitHelper.createBucket(this.authRateLimit) : TwitchLimitRegistry.getInstance().getOrInitializeBucket(userId, TwitchLimitType.CHAT_AUTH_LIMIT, Collections.singletonList(authRateLimit));

log.debug("TwitchChat: Initializing Module ...");
return new TwitchChat(this.eventManager, this.credentialManager, this.chatAccount, this.baseUrl, this.sendCredentialToThirdPartyHost, this.commandPrefixes, this.chatQueueSize, this.ircMessageBucket, this.ircWhisperBucket, this.ircJoinBucket, this.ircAuthBucket, this.scheduledThreadPoolExecutor, this.chatQueueTimeout, this.proxyConfig, this.autoJoinOwnChannel, this.enableMembershipEvents, this.botOwnerIds, this.removeChannelOnJoinFailure, this.maxJoinRetries, this.chatJoinTimeout);
return new TwitchChat(this.eventManager, this.credentialManager, this.chatAccount, this.baseUrl, this.sendCredentialToThirdPartyHost, this.commandPrefixes, this.chatQueueSize, this.ircMessageBucket, this.ircWhisperBucket, this.ircJoinBucket, this.ircAuthBucket, this.scheduledThreadPoolExecutor, this.chatQueueTimeout, this.proxyConfig, this.autoJoinOwnChannel, this.enableMembershipEvents, this.botOwnerIds, this.removeChannelOnJoinFailure, this.maxJoinRetries, this.chatJoinTimeout, this.wsPingPeriod);
}

/**
Expand Down
13 changes: 11 additions & 2 deletions pubsub/src/main/java/com/github/twitch4j/pubsub/TwitchPubSub.java
Expand Up @@ -139,6 +139,11 @@ public class TwitchPubSub implements ITwitchPubSub {
*/
private final Collection<String> botOwnerIds;

/**
* WebSocket RFC Ping Period in ms (0 = disabled)
*/
private final int wsPingPeriod;

/**
* WebSocket Factory
*/
Expand Down Expand Up @@ -177,11 +182,14 @@ public class TwitchPubSub implements ITwitchPubSub {
* @param taskExecutor ScheduledThreadPoolExecutor
* @param proxyConfig ProxyConfig
* @param botOwnerIds Bot Owner IDs
* @param wsPingPeriod WebSocket Ping Period
*/
public TwitchPubSub(EventManager eventManager, ScheduledThreadPoolExecutor taskExecutor, ProxyConfig proxyConfig, Collection<String> botOwnerIds) {
public TwitchPubSub(EventManager eventManager, ScheduledThreadPoolExecutor taskExecutor, ProxyConfig proxyConfig, Collection<String> botOwnerIds, int wsPingPeriod) {
this.eventManager = eventManager;
this.taskExecutor = taskExecutor;
this.botOwnerIds = botOwnerIds;
this.eventManager = eventManager;
this.wsPingPeriod = wsPingPeriod;

// register with serviceMediator
this.eventManager.getServiceMediator().addService("twitch4j-pubsub", this);

Expand Down Expand Up @@ -339,6 +347,7 @@ private void createWebSocket() {
try {
// WebSocket
this.webSocket = webSocketFactory.createSocket(WEB_SOCKET_SERVER);
this.webSocket.setPingInterval(wsPingPeriod);

// WebSocket Listeners
this.webSocket.clearListeners();
Expand Down
Expand Up @@ -55,6 +55,12 @@ public class TwitchPubSubBuilder {
@Accessors(chain = true)
private Collection<String> botOwnerIds = new HashSet<>();

/**
* WebSocket RFC Ping Period in ms (0 = disabled)
*/
@With
private int wsPingPeriod = 15_000;

/**
* Initialize the builder
*
Expand All @@ -77,7 +83,7 @@ public TwitchPubSub build() {
// Initialize/Check EventManager
eventManager = EventManagerUtils.validateOrInitializeEventManager(eventManager, defaultEventHandler);

return new TwitchPubSub(this.eventManager, scheduledThreadPoolExecutor, this.proxyConfig, this.botOwnerIds);
return new TwitchPubSub(this.eventManager, scheduledThreadPoolExecutor, this.proxyConfig, this.botOwnerIds, this.wsPingPeriod);
}

/**
Expand Down
Expand Up @@ -263,6 +263,12 @@ public class TwitchClientBuilder {
@With
private Logger.Level feignLogLevel = Logger.Level.NONE;

/**
* WebSocket RFC Ping Period in ms (0 = disabled)
*/
@With
private int wsPingPeriod = 15_000;

/**
* With a Bot Owner's User ID
*
Expand Down Expand Up @@ -416,6 +422,7 @@ public TwitchClient build() {
.withMaxJoinRetries(chatMaxJoinRetries)
.setBotOwnerIds(botOwnerIds)
.setCommandPrefixes(commandPrefixes)
.withWsPingPeriod(wsPingPeriod)
.build();
}

Expand All @@ -427,6 +434,7 @@ public TwitchClient build() {
.withScheduledThreadPoolExecutor(scheduledThreadPoolExecutor)
.withProxyConfig(proxyConfig)
.setBotOwnerIds(botOwnerIds)
.withWsPingPeriod(wsPingPeriod)
.build();
}

Expand Down
Expand Up @@ -284,6 +284,12 @@ public class TwitchClientPoolBuilder {
@With
private Logger.Level feignLogLevel = Logger.Level.NONE;

/**
* WebSocket RFC Ping Period in ms (0 = disabled)
*/
@With
private int wsPingPeriod = 15_000;

/**
* With a Bot Owner's User ID
*
Expand Down Expand Up @@ -438,6 +444,7 @@ public TwitchClientPool build() {
.withBaseUrl(chatServer)
.withChatQueueTimeout(chatQueueTimeout)
.withMaxJoinRetries(chatMaxJoinRetries)
.withWsPingPeriod(wsPingPeriod)
.setCommandPrefixes(commandPrefixes)
.setBotOwnerIds(botOwnerIds)
)
Expand All @@ -459,6 +466,7 @@ public TwitchClientPool build() {
.withMaxJoinRetries(chatMaxJoinRetries)
.setBotOwnerIds(botOwnerIds)
.setCommandPrefixes(commandPrefixes)
.withWsPingPeriod(wsPingPeriod)
.build();
}

Expand All @@ -469,13 +477,14 @@ public TwitchClientPool build() {
.eventManager(eventManager)
.executor(() -> scheduledThreadPoolExecutor)
.proxyConfig(() -> proxyConfig)
.advancedConfiguration(builder -> builder.setBotOwnerIds(botOwnerIds))
.advancedConfiguration(builder -> builder.withWsPingPeriod(wsPingPeriod).setBotOwnerIds(botOwnerIds))
.build();
} else if (this.enablePubSub) {
pubSub = TwitchPubSubBuilder.builder()
.withEventManager(eventManager)
.withScheduledThreadPoolExecutor(scheduledThreadPoolExecutor)
.withProxyConfig(proxyConfig)
.withWsPingPeriod(wsPingPeriod)
.setBotOwnerIds(botOwnerIds)
.build();
}
Expand Down

0 comments on commit af5744a

Please sign in to comment.