Package com.gmt2001.twitch.tmi
Class TwitchMessageInterface
- All Implemented Interfaces:
WsClientFrameHandler
,AutoCloseable
,Flow.Publisher<TMIMessage>
public final class TwitchMessageInterface
extends SubmissionPublisher<TMIMessage>
implements WsClientFrameHandler
A client for the Twitch Message Interface
- Author:
- gmt2001
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Closes the socketboolean
void
handleFrame
(ChannelHandlerContext ctx, WebSocketFrame frame) Handles the WebSocket frame and sends a response back to the server, if necessaryvoid
Handles the handshake complete eventvoid
onClose()
Handles the channel closing for any reason.void
Notifies subscribed processors that the socket has closed and initiates reconnecting if there wasn't an outbound 1000 close codevoid
onMessages
(String messages) Splits a string containing one or more lines of RFC1459-formatted IRC messages and submits each one to subscribed processorsint
privMsgMaxLength
(String channel, boolean isAction, String replyToId) Calculates the maximum length for the message content of a PRIVMSG to avoid droppingReturns theWindowedSwitchingRateLimiter
used to prevent PRIVMSG spamvoid
Closes the connection to TMI, then resets the outbound closing status and attempts to connect again after a 5 second delayvoid
sendActionPrivMessage
(String channel, String message) Sends an IRC PRIVMSG command with the/me ACTION
specifier.void
sendActionPrivMessage
(String channel, String message, String replyToId) Sends an IRC PRIVMSG command with the/me ACTION
specifier.void
sendChannelCommand
(String command, String channel, String parameter) Sends an IRC command with channel and parameter componentsvoid
sendCommand
(String command, String parameter) Sends an IRC command with parameter componentvoid
Sends an IRC command with tags and parameter componentvoid
Sends an IRC command with tags, channel, and parameter componentsvoid
sendPing()
Forces a PING to be sentvoid
sendPrivMessage
(String channel, String message) Sends an IRC PRIVMSG command.void
sendPrivMessage
(String channel, String message, String replyToId) Sends an IRC PRIVMSG command as a reply to another message.void
Sends a raw IRC messagevoid
shutdown()
Closes the connection normally.Methods inherited from class java.util.concurrent.SubmissionPublisher
close, closeExceptionally, consume, estimateMaximumLag, estimateMinimumDemand, getClosedException, getExecutor, getMaxBufferCapacity, getNumberOfSubscribers, getSubscribers, hasSubscribers, isClosed, isSubscribed, offer, offer, submit, subscribe
-
Constructor Details
-
TwitchMessageInterface
public TwitchMessageInterface()Initializes the Twitch Message Interface. Creates a newWSClient
, then initializes all processors and starts connecting
-
-
Method Details
-
privMsgMaxLength
Calculates the maximum length for the message content of a PRIVMSG to avoid dropping- Parameters:
channel
- The channel nameisAction
- If this is for a ACTION (/me) messagereplyToId
- Theid
tag from theTMIMessage.tags
of the message that is being replied to;null
if not used- Returns:
-
rateLimiter
Returns theWindowedSwitchingRateLimiter
used to prevent PRIVMSG spam- Returns:
- The rate limiter
-
sendPing
public void sendPing()Forces a PING to be sent -
sendActionPrivMessage
Sends an IRC PRIVMSG command with the/me ACTION
specifier. If there are no tokens left onrateLimiter
, the message is silently dropped- Parameters:
channel
- The channel to send tomessage
- The chat message
-
sendPrivMessage
Sends an IRC PRIVMSG command. If the message starts with/me
, then it is passed tosendActionPrivMessage(java.lang.String, java.lang.String)
instead. If there are no tokens left onrateLimiter
, the message is silently dropped- Parameters:
channel
- The channel to send tomessage
- The chat message
-
sendActionPrivMessage
Sends an IRC PRIVMSG command with the/me ACTION
specifier. If there are no tokens left onrateLimiter
, the message is silently dropped- Parameters:
channel
- The channel to send tomessage
- The chat messagereplyToId
- Theid
tag from theTMIMessage.tags
of the message that is being replied to
-
sendPrivMessage
Sends an IRC PRIVMSG command as a reply to another message. If the message starts with/me
, then it is passed tosendActionPrivMessage(java.lang.String, java.lang.String, java.lang.String)
instead. If there are no tokens left onrateLimiter
, the message is silently dropped- Parameters:
channel
- The channel to send tomessage
- The chat messagereplyToId
- Theid
tag from theTMIMessage.tags
of the message that is being replied to
-
sendCommand
Sends an IRC command with parameter component- Parameters:
command
- The IRC commandparameter
- The IRC parameter
-
sendCommand
Sends an IRC command with tags and parameter component- Parameters:
tags
- The IRCv3 tagscommand
- The IRC commandparameter
- The IRC parameter
-
sendChannelCommand
Sends an IRC command with channel and parameter components- Parameters:
command
- The IRC commandchannel
- The channel to send toparameter
- The IRC parameter
-
sendFullCommand
public void sendFullCommand(Map<String, String> tags, String command, String channel, String parameter) Sends an IRC command with tags, channel, and parameter components- Parameters:
tags
- The IRCv3 tags. Ifnull
or empty, the tags component is not includedcommand
- The IRC commandchannel
- The channel to send to. Ifnull
, empty, or blank, the channel component of the command is not includedparameter
- The IRC parameters. Ifnull
, the parameter component is not included
-
sendRaw
Sends a raw IRC message- Parameters:
message
- The message to send
-
reconnect
public void reconnect()Closes the connection to TMI, then resets the outbound closing status and attempts to connect again after a 5 second delay -
connected
public boolean connected()- Returns:
true
if the underlying socket is connected. Does not indicate anything about the state other than that the raw socket is connected
-
onMessages
Splits a string containing one or more lines of RFC1459-formatted IRC messages and submits each one to subscribed processors- Parameters:
messages
- A string containing one or more RFC1459-formatted IRC messages
-
onClose
Notifies subscribed processors that the socket has closed and initiates reconnecting if there wasn't an outbound 1000 close code- Parameters:
code
- The close codereason
- The textual close reason
-
handleFrame
Description copied from interface:WsClientFrameHandler
Handles the WebSocket frame and sends a response back to the server, if necessary- Specified by:
handleFrame
in interfaceWsClientFrameHandler
- Parameters:
ctx
- TheChannelHandlerContext
of the sessionframe
- TheWebSocketFrame
to process
-
handshakeComplete
Description copied from interface:WsClientFrameHandler
Handles the handshake complete event- Specified by:
handshakeComplete
in interfaceWsClientFrameHandler
- Parameters:
ctx
- TheChannelHandlerContext
of the session
-
onClose
public void onClose()Description copied from interface:WsClientFrameHandler
Handles the channel closing for any reason.- Specified by:
onClose
in interfaceWsClientFrameHandler
-
close
Closes the socket- Parameters:
code
- The close codereason
- The textual close reason
-
shutdown
public void shutdown()Closes the connection normally. Exceptions are discarded
-