Class TwitchChatConnectionPool
- All Implemented Interfaces:
ITwitchChat,TransactionalSubscriber<String,,String, Boolean> AutoCloseable
TwitchChat connections.
This pool is easiest to use for:
- Reading from many channels without an account
- Reading from many channels with an account
- Reading from many channels using valid proxies
- Reading and sending messages/whispers with only one account
Warning: If a custom ScheduledThreadPoolExecutor is specified,
its corePoolSize must be large enough for the threads required by connections made by this class.
Warning: If a chatAccount is to be shared across multiple connections and used to send messages,
one should use advancedConfiguration to ensure the two are using a shared Bucket.
Note: If whispers are to be sent using this pool, one must manually join the channel to send the whisper from first. If chatAccount's are dynamically supplied such that no two connections are using the same account, one can set twitchChatBuilder.withAutoJoinOwnChannel(true) via advancedConfiguration to avoid the manual join.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classTwitchChatConnectionPool.TwitchChatConnectionPoolBuilder<C extends TwitchChatConnectionPool,B extends TwitchChatConnectionPool.TwitchChatConnectionPoolBuilder<C, B>> Nested classes/interfaces inherited from class com.github.twitch4j.common.pool.TwitchModuleConnectionPool
TwitchModuleConnectionPool.TwitchModuleConnectionPoolBuilder<C,X, Y, Z, B, C2 extends TwitchModuleConnectionPool<C, X, Y, Z, B>, B2 extends TwitchModuleConnectionPool.TwitchModuleConnectionPoolBuilder<C, X, Y, Z, B, C2, B2>> Nested classes/interfaces inherited from class com.github.twitch4j.common.pool.SubscriptionConnectionPool
SubscriptionConnectionPool.SubscriptionConnectionPoolBuilder<C,S, T, U, C2 extends SubscriptionConnectionPool<C, S, T, U>, B extends SubscriptionConnectionPool.SubscriptionConnectionPoolBuilder<C, S, T, U, C2, B>> Nested classes/interfaces inherited from class com.github.twitch4j.common.pool.AbstractConnectionPool
AbstractConnectionPool.AbstractConnectionPoolBuilder<C,C2 extends AbstractConnectionPool<C>, B extends AbstractConnectionPool.AbstractConnectionPoolBuilder<C, C2, B>> -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected io.github.bucket4j.BandwidthCustom RateLimit for AUTHprotected final booleanDeprecated.use removeChannelOnJoinFailure via advancedConfiguration instead.protected final @NonNull Supplier<com.github.philippheuer.credentialmanager.domain.OAuth2Credential>Provides a chat account to be used when constructing a newTwitchChatinstance.protected io.github.bucket4j.BandwidthCustom RateLimit for ChatMessagesprotected io.github.bucket4j.BandwidthCustom RateLimit for JOIN/PARTprotected io.github.bucket4j.BandwidthCustom RateLimit for Messages per Channelprotected io.github.bucket4j.Bandwidth[]Custom RateLimit for WhispersFields inherited from class com.github.twitch4j.common.pool.TwitchModuleConnectionPool
advancedConfiguration, executor, proxyConfigFields inherited from class com.github.twitch4j.common.pool.SubscriptionConnectionPool
closed, disposeUnusedConnections, maxSubscriptionsPerConnection, saturatedConnections, subscriptions, unsaturatedConnections -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected -
Method Summary
Modifier and TypeMethodDescriptionbuilder()protected TwitchChatprotected voiddisposeConnection(TwitchChat connection) Note: this map does not dynamically update unlikeTwitchChat.getChannelIdToChannelName()Note: this map does not dynamically update unlikeTwitchChat.getChannelNameToChannelId()Returns a set of all currently joined channels (without # prefix)longprotected Stringprotected intprotected StringhandleDuplicateSubscription(TwitchChat twitchChat, TwitchChat old, String s) protected StringhandleSubscription(TwitchChat twitchChat, String s) protected BooleanhandleUnsubscription(TwitchChat twitchChat, String s) booleanisChannelJoined(String channelName) Check if Chat is currently in a channelvoidjoinChannel(String channelName) Joins a channelbooleanleaveChannel(String channelName) Parts from a channelbooleanSends a message to the channel while including the specified message tags.booleansendMessage(String channelToIdentifyChatInstance, String targetChannel, String message) Sends a message from theTwitchChatidentified either to a channel or directly on the socket.booleansendMessage(String channelToIdentifyChatInstance, String targetChannel, String message, @Nullable Map<String, Object> tags) Sends a message from the identifiedTwitchChatinstance with the specified tags.booleansendMessage(String channelToIdentifyChatInstance, String targetChannel, String message, String nonce, String replyMsgId) Sends a message from the identifiedTwitchChatinstance with an optional nonce or reply parent.booleansendPrivateMessage(String channelToIdentifyChatInstance, String toChannel, String message) Deprecated.Twitch will decommission this method on February 18, 2023; migrate to TwitchHelix#updateChatSettingsJoins a channel.Parts from a channel.Methods inherited from class com.github.twitch4j.common.pool.TwitchModuleConnectionPool
getConnectionEventManager, getDefaultConnectionEventManager, getEventManager, getExecutorMethods inherited from class com.github.twitch4j.common.pool.SubscriptionConnectionPool
close, getConnections, numConnections, numSubscriptionsMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.github.twitch4j.chat.ITwitchChat
ban, clearChat, close, delete, getEventManager, sendActionMessage, sendAnnouncement, sendMessage, sendMessage, setEmoteOnly, setFollowersOnly, setSlowMode, setSubscribersOnly, setUniqueChat, timeout, unban
-
Field Details
-
chatAccount
@NonNull protected final @NonNull Supplier<com.github.philippheuer.credentialmanager.domain.OAuth2Credential> chatAccountProvides a chat account to be used when constructing a newTwitchChatinstance. By default, this yields null, which corresponds to an anonymous connection. -
automaticallyPartOnBan
Deprecated.use removeChannelOnJoinFailure via advancedConfiguration instead.Whether chat connections should automatically part from channels they have been banned from. This is useful for reclaiming subscription headroom so a minimal number of chat instances are running. By default false so that a chat instance can (eventually) reconnect if a unban occurs. -
chatRateLimit
protected io.github.bucket4j.Bandwidth chatRateLimitCustom RateLimit for ChatMessages -
whisperRateLimit
protected io.github.bucket4j.Bandwidth[] whisperRateLimitCustom RateLimit for Whispers -
joinRateLimit
protected io.github.bucket4j.Bandwidth joinRateLimitCustom RateLimit for JOIN/PART -
authRateLimit
protected io.github.bucket4j.Bandwidth authRateLimitCustom RateLimit for AUTH -
perChannelRateLimit
protected io.github.bucket4j.Bandwidth perChannelRateLimitCustom RateLimit for Messages per ChannelFor example, this can restrict messages per channel at 100/30 (for a verified bot that has a global 7500/30 message limit).
-
-
Constructor Details
-
TwitchChatConnectionPool
-
-
Method Details
-
sendMessage
public boolean sendMessage(String channel, String message, @Nullable @Nullable Map<String, Object> tags) Description copied from interface:ITwitchChatSends a message to the channel while including the specified message tags.- Specified by:
sendMessagein interfaceITwitchChat- Parameters:
channel- the name of the channel to send the message to.message- the message to be sent.tags- the message tags.- Returns:
- whether the message was added to the queue
-
sendMessage
public boolean sendMessage(String channelToIdentifyChatInstance, String targetChannel, String message) Sends a message from theTwitchChatidentified either to a channel or directly on the socket.- Parameters:
channelToIdentifyChatInstance- the channel used to identify whichTwitchChatinstance should be used to send the message; the instance must be subscribed to this channel.targetChannel- the channel to send the message to, if not null (otherwise it is sent directly on the socket).message- the message to be sent.- Returns:
- whether a
TwitchChatinstance was found and used to send the message
-
sendMessage
public boolean sendMessage(String channelToIdentifyChatInstance, String targetChannel, String message, @Unofficial String nonce, String replyMsgId) Sends a message from the identifiedTwitchChatinstance with an optional nonce or reply parent.- Parameters:
channelToIdentifyChatInstance- the channel used to identify whichTwitchChatinstance should be used to send the message; the instance must be subscribed to this channel.targetChannel- the channel to send the message to, if not null (otherwise it is sent directly on the socket).message- the message to be sent.nonce- the cryptographic nonce (optional).replyMsgId- the msgId of the parent message being replied to (optional).- Returns:
- whether a
TwitchChatinstance was found and used to send the message
-
sendMessage
public boolean sendMessage(String channelToIdentifyChatInstance, String targetChannel, String message, @Nullable @Nullable Map<String, Object> tags) Sends a message from the identifiedTwitchChatinstance with the specified tags.- Parameters:
channelToIdentifyChatInstance- the channel used to identify whichTwitchChatinstance should be used to send the message; the instance must be subscribed to this channel.targetChannel- the channel to send the message to, if not null (otherwise it is sent directly on the socket).message- the message to be sent.tags- the message tags.- Returns:
- whether a
TwitchChatinstance was found and used to send the message
-
sendPrivateMessage
@Deprecated public boolean sendPrivateMessage(String channelToIdentifyChatInstance, String toChannel, String message) Deprecated.Twitch will decommission this method on February 18, 2023; migrate to TwitchHelix#updateChatSettingsSends a whisper.- Parameters:
channelToIdentifyChatInstance- the channel used to identify whichTwitchChatinstance should be used to send the message; the instance must be subscribed to this channel.toChannel- the channel to send the whisper to.message- the message to send in the whisper.- Returns:
- whether a
TwitchChatinstance was identified to send the message from. - Throws:
NullPointerException- if the identifiedTwitchChatdoes not have a valid chatCredential
-
subscribe
Joins a channel.- Specified by:
subscribein interfaceTransactionalSubscriber<String,String, Boolean> - Overrides:
subscribein classSubscriptionConnectionPool<TwitchChat,String, String, Boolean> - Parameters:
s- the channel name- Returns:
- the channel name
-
joinChannel
Description copied from interface:ITwitchChatJoins a channel- Specified by:
joinChannelin interfaceITwitchChat- Parameters:
channelName- The target channel name.
-
unsubscribe
Parts from a channel.- Specified by:
unsubscribein interfaceTransactionalSubscriber<String,String, Boolean> - Overrides:
unsubscribein classSubscriptionConnectionPool<TwitchChat,String, String, Boolean> - Parameters:
s- the channel name- Returns:
- a non-null class if able to part, null otherwise
-
leaveChannel
Description copied from interface:ITwitchChatParts from a channel- Specified by:
leaveChannelin interfaceITwitchChat- Parameters:
channelName- The target channel name.- Returns:
- whether the channel was previously joined
-
isChannelJoined
Description copied from interface:ITwitchChatCheck if Chat is currently in a channel- Specified by:
isChannelJoinedin interfaceITwitchChat- Parameters:
channelName- channel to check (without # prefix)- Returns:
- boolean
-
getChannels
Description copied from interface:ITwitchChatReturns a set of all currently joined channels (without # prefix)- Specified by:
getChannelsin interfaceITwitchChat- Returns:
- a set of channel names
-
handleSubscription
- Specified by:
handleSubscriptionin classSubscriptionConnectionPool<TwitchChat,String, String, Boolean>
-
handleDuplicateSubscription
- Specified by:
handleDuplicateSubscriptionin classSubscriptionConnectionPool<TwitchChat,String, String, Boolean>
-
handleUnsubscription
- Specified by:
handleUnsubscriptionin classSubscriptionConnectionPool<TwitchChat,String, String, Boolean>
-
getRequestFromSubscription
- Specified by:
getRequestFromSubscriptionin classSubscriptionConnectionPool<TwitchChat,String, String, Boolean>
-
getSubscriptionSize
- Specified by:
getSubscriptionSizein classSubscriptionConnectionPool<TwitchChat,String, String, Boolean>
-
createConnection
- Specified by:
createConnectionin classAbstractConnectionPool<TwitchChat>
-
disposeConnection
- Specified by:
disposeConnectionin classAbstractConnectionPool<TwitchChat>
-
getLatency
public long getLatency()- Specified by:
getLatencyin interfaceITwitchChat- Returns:
- the most recently measured round-trip latency for the socket(s) in milliseconds, or -1 if unknown
-
getChannelIdToChannelName
Note: this map does not dynamically update unlikeTwitchChat.getChannelIdToChannelName()- Specified by:
getChannelIdToChannelNamein interfaceITwitchChat- Returns:
- cached mappings of channel ids to names
-
getChannelNameToChannelId
Note: this map does not dynamically update unlikeTwitchChat.getChannelNameToChannelId()- Specified by:
getChannelNameToChannelIdin interfaceITwitchChat- Returns:
- cached mappings of channel names to ids
-
builder
-