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
Modifier and TypeClassDescriptionstatic class
TwitchChatConnectionPool.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
Modifier and TypeFieldDescriptionprotected io.github.bucket4j.Bandwidth
Custom RateLimit for AUTHprotected final boolean
Deprecated.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 newTwitchChat
instance.protected io.github.bucket4j.Bandwidth
Custom RateLimit for ChatMessagesprotected io.github.bucket4j.Bandwidth
Custom RateLimit for JOIN/PARTprotected io.github.bucket4j.Bandwidth
Custom RateLimit for Messages per Channelprotected io.github.bucket4j.Bandwidth[]
Custom RateLimit for WhispersFields inherited from class com.github.twitch4j.common.pool.TwitchModuleConnectionPool
advancedConfiguration, executor, proxyConfig
Fields inherited from class com.github.twitch4j.common.pool.SubscriptionConnectionPool
closed, disposeUnusedConnections, maxSubscriptionsPerConnection, saturatedConnections, subscriptions, unsaturatedConnections
-
Constructor Summary
ModifierConstructorDescriptionprotected
-
Method Summary
Modifier and TypeMethodDescriptionbuilder()
protected TwitchChat
protected void
disposeConnection
(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)long
protected String
protected int
protected String
handleDuplicateSubscription
(TwitchChat twitchChat, TwitchChat old, String s) protected String
handleSubscription
(TwitchChat twitchChat, String s) protected Boolean
handleUnsubscription
(TwitchChat twitchChat, String s) boolean
isChannelJoined
(String channelName) Check if Chat is currently in a channelvoid
joinChannel
(String channelName) Joins a channelboolean
leaveChannel
(String channelName) Parts from a channelboolean
Sends a message to the channel while including the specified message tags.boolean
sendMessage
(String channelToIdentifyChatInstance, String targetChannel, String message) Sends a message from theTwitchChat
identified either to a channel or directly on the socket.boolean
sendMessage
(String channelToIdentifyChatInstance, String targetChannel, String message, @Nullable Map<String, Object> tags) Sends a message from the identifiedTwitchChat
instance with the specified tags.boolean
sendMessage
(String channelToIdentifyChatInstance, String targetChannel, String message, String nonce, String replyMsgId) Sends a message from the identifiedTwitchChat
instance with an optional nonce or reply parent.boolean
sendPrivateMessage
(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, getExecutor
Methods inherited from class com.github.twitch4j.common.pool.SubscriptionConnectionPool
close, getConnections, numConnections, numSubscriptions
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods 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 newTwitchChat
instance. 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:ITwitchChat
Sends a message to the channel while including the specified message tags.- Specified by:
sendMessage
in 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 theTwitchChat
identified either to a channel or directly on the socket.- Parameters:
channelToIdentifyChatInstance
- the channel used to identify whichTwitchChat
instance 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
TwitchChat
instance 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 identifiedTwitchChat
instance with an optional nonce or reply parent.- Parameters:
channelToIdentifyChatInstance
- the channel used to identify whichTwitchChat
instance 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
TwitchChat
instance 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 identifiedTwitchChat
instance with the specified tags.- Parameters:
channelToIdentifyChatInstance
- the channel used to identify whichTwitchChat
instance 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
TwitchChat
instance 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 whichTwitchChat
instance 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
TwitchChat
instance was identified to send the message from. - Throws:
NullPointerException
- if the identifiedTwitchChat
does not have a valid chatCredential
-
subscribe
Joins a channel.- Specified by:
subscribe
in interfaceTransactionalSubscriber<String,
String, Boolean> - Overrides:
subscribe
in classSubscriptionConnectionPool<TwitchChat,
String, String, Boolean> - Parameters:
s
- the channel name- Returns:
- the channel name
-
joinChannel
Description copied from interface:ITwitchChat
Joins a channel- Specified by:
joinChannel
in interfaceITwitchChat
- Parameters:
channelName
- The target channel name.
-
unsubscribe
Parts from a channel.- Specified by:
unsubscribe
in interfaceTransactionalSubscriber<String,
String, Boolean> - Overrides:
unsubscribe
in 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:ITwitchChat
Parts from a channel- Specified by:
leaveChannel
in interfaceITwitchChat
- Parameters:
channelName
- The target channel name.- Returns:
- whether the channel was previously joined
-
isChannelJoined
Description copied from interface:ITwitchChat
Check if Chat is currently in a channel- Specified by:
isChannelJoined
in interfaceITwitchChat
- Parameters:
channelName
- channel to check (without # prefix)- Returns:
- boolean
-
getChannels
Description copied from interface:ITwitchChat
Returns a set of all currently joined channels (without # prefix)- Specified by:
getChannels
in interfaceITwitchChat
- Returns:
- a set of channel names
-
handleSubscription
- Specified by:
handleSubscription
in classSubscriptionConnectionPool<TwitchChat,
String, String, Boolean>
-
handleDuplicateSubscription
- Specified by:
handleDuplicateSubscription
in classSubscriptionConnectionPool<TwitchChat,
String, String, Boolean>
-
handleUnsubscription
- Specified by:
handleUnsubscription
in classSubscriptionConnectionPool<TwitchChat,
String, String, Boolean>
-
getRequestFromSubscription
- Specified by:
getRequestFromSubscription
in classSubscriptionConnectionPool<TwitchChat,
String, String, Boolean>
-
getSubscriptionSize
- Specified by:
getSubscriptionSize
in classSubscriptionConnectionPool<TwitchChat,
String, String, Boolean>
-
createConnection
- Specified by:
createConnection
in classAbstractConnectionPool<TwitchChat>
-
disposeConnection
- Specified by:
disposeConnection
in classAbstractConnectionPool<TwitchChat>
-
getLatency
public long getLatency()- Specified by:
getLatency
in 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:
getChannelIdToChannelName
in interfaceITwitchChat
- Returns:
- cached mappings of channel ids to names
-
getChannelNameToChannelId
Note: this map does not dynamically update unlikeTwitchChat.getChannelNameToChannelId()
- Specified by:
getChannelNameToChannelId
in interfaceITwitchChat
- Returns:
- cached mappings of channel names to ids
-
builder
-