Package com.github.twitch4j.chat
Class TwitchChatConnectionPool
java.lang.Object
com.github.twitch4j.common.pool.AbstractConnectionPool<C>
com.github.twitch4j.common.pool.SubscriptionConnectionPool<C,X,Y,Z>
com.github.twitch4j.common.pool.TwitchModuleConnectionPool<TwitchChat,java.lang.String,java.lang.String,java.lang.Boolean,TwitchChatBuilder>
com.github.twitch4j.chat.TwitchChatConnectionPool
- All Implemented Interfaces:
ITwitchChat,com.github.twitch4j.common.pool.TransactionalSubscriber<java.lang.String,java.lang.String,java.lang.Boolean>,java.lang.AutoCloseable
public class TwitchChatConnectionPool extends com.github.twitch4j.common.pool.TwitchModuleConnectionPool<TwitchChat,java.lang.String,java.lang.String,java.lang.Boolean,TwitchChatBuilder> implements ITwitchChat
A pool for
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 Classes Modifier and Type Class Description static classTwitchChatConnectionPool.TwitchChatConnectionPoolBuilder<C extends TwitchChatConnectionPool,B extends TwitchChatConnectionPool.TwitchChatConnectionPoolBuilder<C,B>>Nested classes/interfaces inherited from class com.github.twitch4j.common.pool.TwitchModuleConnectionPool
com.github.twitch4j.common.pool.TwitchModuleConnectionPool.TwitchModuleConnectionPoolBuilder<C extends java.lang.Object,X extends java.lang.Object,Y extends java.lang.Object,Z extends java.lang.Object,B extends java.lang.Object,C2 extends com.github.twitch4j.common.pool.TwitchModuleConnectionPool<C,X,Y,Z,B>,B2 extends com.github.twitch4j.common.pool.TwitchModuleConnectionPool.TwitchModuleConnectionPoolBuilder<C,X,Y,Z,B,C2,B2>>Nested classes/interfaces inherited from class com.github.twitch4j.common.pool.SubscriptionConnectionPool
com.github.twitch4j.common.pool.SubscriptionConnectionPool.SubscriptionConnectionPoolBuilder<C extends java.lang.Object,S extends java.lang.Object,T extends java.lang.Object,U extends java.lang.Object,C2 extends com.github.twitch4j.common.pool.SubscriptionConnectionPool<C,S,T,U>,B extends com.github.twitch4j.common.pool.SubscriptionConnectionPool.SubscriptionConnectionPoolBuilder<C,S,T,U,C2,B>>Nested classes/interfaces inherited from class com.github.twitch4j.common.pool.AbstractConnectionPool
com.github.twitch4j.common.pool.AbstractConnectionPool.AbstractConnectionPoolBuilder<C extends java.lang.Object,C2 extends com.github.twitch4j.common.pool.AbstractConnectionPool<C>,B extends com.github.twitch4j.common.pool.AbstractConnectionPool.AbstractConnectionPoolBuilder<C,C2,B>> -
Field Summary
Fields Modifier and Type Field Description protected booleanautomaticallyPartOnBanWhether chat connections should automatically part from channels they have been banned from.protected @NonNull java.util.function.Supplier<com.github.philippheuer.credentialmanager.domain.OAuth2Credential>chatAccountProvides a chat account to be used when constructing a newTwitchChatinstance.Fields inherited from class com.github.twitch4j.common.pool.TwitchModuleConnectionPool
advancedConfiguration, executor, proxyConfigFields inherited from class com.github.twitch4j.common.pool.SubscriptionConnectionPool
disposeUnusedConnections, maxSubscriptionsPerConnection, saturatedConnections, subscriptions, unsaturatedConnections -
Constructor Summary
Constructors Modifier Constructor Description protectedTwitchChatConnectionPool(TwitchChatConnectionPool.TwitchChatConnectionPoolBuilder<?,?> b) -
Method Summary
Modifier and Type Method Description static TwitchChatConnectionPool.TwitchChatConnectionPoolBuilder<?,?>builder()protected TwitchChatcreateConnection()protected voiddisposeConnection(TwitchChat connection)java.util.Map<java.lang.String,java.lang.String>getChannelIdToChannelName()Note: this map does not dynamically update unlikeTwitchChat.getChannelIdToChannelName()java.util.Map<java.lang.String,java.lang.String>getChannelNameToChannelId()Note: this map does not dynamically update unlikeTwitchChat.getChannelNameToChannelId()java.util.Set<java.lang.String>getChannels()Returns a set of all currently joined channels (without # prefix)protected java.lang.StringgetRequestFromSubscription(java.lang.String s)protected intgetSubscriptionSize(java.lang.String s)protected java.lang.StringhandleDuplicateSubscription(TwitchChat twitchChat, TwitchChat old, java.lang.String s)protected java.lang.StringhandleSubscription(TwitchChat twitchChat, java.lang.String s)protected java.lang.BooleanhandleUnsubscription(TwitchChat twitchChat, java.lang.String s)booleanisChannelJoined(java.lang.String channelName)Check if Chat is currently in a channelvoidjoinChannel(java.lang.String channelName)Joins a channelbooleanleaveChannel(java.lang.String channelName)Parts from a channelbooleansendMessage(java.lang.String channel, java.lang.String message, @Nullable java.util.Map<java.lang.String,java.lang.Object> tags)Sends a message to the channel while including the specified message tags.booleansendMessage(java.lang.String channelToIdentifyChatInstance, java.lang.String targetChannel, java.lang.String message)Sends a message from theTwitchChatidentified either to a channel or directly on the socket.booleansendMessage(java.lang.String channelToIdentifyChatInstance, java.lang.String targetChannel, java.lang.String message, @Nullable java.util.Map<java.lang.String,java.lang.Object> tags)Sends a message from the identifiedTwitchChatinstance with the specified tags.booleansendMessage(java.lang.String channelToIdentifyChatInstance, java.lang.String targetChannel, java.lang.String message, java.lang.String nonce, java.lang.String replyMsgId)Sends a message from the identifiedTwitchChatinstance with an optional nonce or reply parent.booleansendPrivateMessage(java.lang.String channelToIdentifyChatInstance, java.lang.String toChannel, java.lang.String message)Sends a whisper.java.lang.Stringsubscribe(java.lang.String s)Joins a channel.java.lang.Booleanunsubscribe(java.lang.String s)Parts from a channel.Methods inherited from class com.github.twitch4j.common.pool.TwitchModuleConnectionPool
createEventManager, getConnectionEventManager, getDefaultConnectionEventManager, getEventManager, getExecutorMethods inherited from class com.github.twitch4j.common.pool.SubscriptionConnectionPool
getConnections, numConnections, numSubscriptionsMethods inherited from class com.github.twitch4j.common.pool.AbstractConnectionPool
closeMethods 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, sendMessage, sendMessage, setEmoteOnly, setFollowersOnly, setSlowMode, setSubscribersOnly, setUniqueChat, timeout, unban
-
Field Details
-
chatAccount
@NonNull protected final @NonNull java.util.function.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
protected final boolean automaticallyPartOnBanWhether 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.
-
-
Constructor Details
-
TwitchChatConnectionPool
protected TwitchChatConnectionPool(TwitchChatConnectionPool.TwitchChatConnectionPoolBuilder<?,?> b)
-
-
Method Details
-
sendMessage
public boolean sendMessage(java.lang.String channel, java.lang.String message, @Unofficial @Nullable @Nullable java.util.Map<java.lang.String,java.lang.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 (unofficial).- Returns:
- whether the message was added to the queue
-
sendMessage
public boolean sendMessage(java.lang.String channelToIdentifyChatInstance, java.lang.String targetChannel, java.lang.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
@Unofficial public boolean sendMessage(java.lang.String channelToIdentifyChatInstance, java.lang.String targetChannel, java.lang.String message, java.lang.String nonce, java.lang.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(java.lang.String channelToIdentifyChatInstance, java.lang.String targetChannel, java.lang.String message, @Unofficial @Nullable @Nullable java.util.Map<java.lang.String,java.lang.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 (unofficial).- Returns:
- whether a
TwitchChatinstance was found and used to send the message
-
sendPrivateMessage
public boolean sendPrivateMessage(java.lang.String channelToIdentifyChatInstance, java.lang.String toChannel, java.lang.String message)Sends 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:
java.lang.NullPointerException- if the identifiedTwitchChatdoes not have a valid chatCredential
-
subscribe
public java.lang.String subscribe(java.lang.String s)Joins a channel.- Specified by:
subscribein interfacecom.github.twitch4j.common.pool.TransactionalSubscriber<java.lang.String,java.lang.String,java.lang.Boolean>- Overrides:
subscribein classcom.github.twitch4j.common.pool.SubscriptionConnectionPool<TwitchChat,java.lang.String,java.lang.String,java.lang.Boolean>- Parameters:
s- the channel name- Returns:
- the channel name
-
joinChannel
public void joinChannel(java.lang.String channelName)Description copied from interface:ITwitchChatJoins a channel- Specified by:
joinChannelin interfaceITwitchChat- Parameters:
channelName- The target channel name.
-
unsubscribe
public java.lang.Boolean unsubscribe(java.lang.String s)Parts from a channel.- Specified by:
unsubscribein interfacecom.github.twitch4j.common.pool.TransactionalSubscriber<java.lang.String,java.lang.String,java.lang.Boolean>- Overrides:
unsubscribein classcom.github.twitch4j.common.pool.SubscriptionConnectionPool<TwitchChat,java.lang.String,java.lang.String,java.lang.Boolean>- Parameters:
s- the channel name- Returns:
- a non-null class if able to part, null otherwise
-
leaveChannel
public boolean leaveChannel(java.lang.String channelName)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
public boolean isChannelJoined(java.lang.String channelName)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
public java.util.Set<java.lang.String> 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 classcom.github.twitch4j.common.pool.SubscriptionConnectionPool<TwitchChat,java.lang.String,java.lang.String,java.lang.Boolean>
-
handleDuplicateSubscription
protected java.lang.String handleDuplicateSubscription(TwitchChat twitchChat, TwitchChat old, java.lang.String s)- Specified by:
handleDuplicateSubscriptionin classcom.github.twitch4j.common.pool.SubscriptionConnectionPool<TwitchChat,java.lang.String,java.lang.String,java.lang.Boolean>
-
handleUnsubscription
- Specified by:
handleUnsubscriptionin classcom.github.twitch4j.common.pool.SubscriptionConnectionPool<TwitchChat,java.lang.String,java.lang.String,java.lang.Boolean>
-
getRequestFromSubscription
protected java.lang.String getRequestFromSubscription(java.lang.String s)- Specified by:
getRequestFromSubscriptionin classcom.github.twitch4j.common.pool.SubscriptionConnectionPool<TwitchChat,java.lang.String,java.lang.String,java.lang.Boolean>
-
getSubscriptionSize
protected int getSubscriptionSize(java.lang.String s)- Specified by:
getSubscriptionSizein classcom.github.twitch4j.common.pool.SubscriptionConnectionPool<TwitchChat,java.lang.String,java.lang.String,java.lang.Boolean>
-
createConnection
- Specified by:
createConnectionin classcom.github.twitch4j.common.pool.AbstractConnectionPool<TwitchChat>
-
disposeConnection
- Specified by:
disposeConnectionin classcom.github.twitch4j.common.pool.AbstractConnectionPool<TwitchChat>
-
getChannelIdToChannelName
public java.util.Map<java.lang.String,java.lang.String> getChannelIdToChannelName()Note: this map does not dynamically update unlikeTwitchChat.getChannelIdToChannelName()- Specified by:
getChannelIdToChannelNamein interfaceITwitchChat- Returns:
- cached mappings of channel ids to names
-
getChannelNameToChannelId
public java.util.Map<java.lang.String,java.lang.String> getChannelNameToChannelId()Note: this map does not dynamically update unlikeTwitchChat.getChannelNameToChannelId()- Specified by:
getChannelNameToChannelIdin interfaceITwitchChat- Returns:
- cached mappings of channel names to ids
-
builder
-