AlgosGitHub / KindleTwitchController

Twitch Chat Moderation and OBS Control Panel tailored for Kindle devices.
MIT License
2 stars 0 forks source link

Last to Disconnect, Please Unsubscribe! #42

Closed AlgosGitHub closed 6 months ago

AlgosGitHub commented 6 months ago

TwitchChannelManager is not shutting down the respective twitchClient when the last kindle-user disconnects. We want to persist the secret code indefinitely (for now).

If the user de-authorizes manually, or their authorization expires by other means, there should be an exception produced when we use their cached OAuth. Catch that, and tell the user their secret key has expired and they'll need to go re-authenticate for a new one.

MarioMottl commented 6 months ago

In the TwitchChatClient.java file, specifically on line 20 - 38, the following code snippet is present:

TwitchChat client = twitchClient.getChat();

// source the user's name from their authorization token
client.joinChannel(twitchChannel);

System.out.println("Connected to Twitch Chat Channel: " + twitchChannel);

EventManager eventManager = twitchClient.getEventManager();

eventManager.onEvent(IRCMessageEvent.class, event -> {
    Optional<String> message = event.getMessage();
    Optional<String> userDisplayName = event.getUserDisplayName();

    if(message.isPresent() && userDisplayName.isPresent()) {
        System.out.println(event.getUser().getName() + " > " + message.get());
        onMessage.accept(new ChatMessage(userDisplayName.get(), message.get()));
    }

});

Issue Description:

The client object is allocated for every subscription to a channel. Consequently, if all existing connections to a channel, such as AlgoBro, are closed and a new connection to the chat is subsequently opened, a new client will be spawned. The previous client instance will persist in memory, leading to potential issues and inefficiencies. This lingering object in memory is colloquially referred to as Limbo. We would need to have the client and eventmanager disconnect from the twitchChannel after all connections are closed.

Proposed Solution: In TwitchClientRegistry.java add a function that gets the Twitchclient by channelName e.g. getClientByChannelName. e.g.

public static TwitchClient getClientByChannel(String twitchChannel) {
    for (Map.Entry<String, TwitchClient> entry : clients.entrySet()) {
        if (entry.getValue().getChat().getChannels().contains(twitchChannel)) {
            return entry.getValue();
        }
    }
    return null;
}

also in TwitchChannelManager.java

public void removeConnectionFromChannel(WebSocket conn) {
....
//If all connections to a channel are closed
TwitchClient toBeRemoved = getClientByChannelName(channelName)
toBeRemoved.getChat().leaveChannel(entry.getKey());
AlgosGitHub commented 6 months ago

Rewrote from scratch. Got it workin, but it's a little messy.