TwitchLib / TwitchLib

C# Twitch Chat, Whisper, API and PubSub Library. Allows for chatting, whispering, stream event subscription and channel/account modification. Supports everything that supports .NETStandard 2.0
https://twitchlib.github.io
MIT License
836 stars 149 forks source link

Client cant reconnect #1063

Open RNGIllSkillz opened 2 years ago

RNGIllSkillz commented 2 years ago

If client get disconnected, runing client.reconnect or client.connect doesnt reconnect.

swiftyspiffy commented 2 years ago

What environment is the library running in? Can you share as much environment details as possible

WrithemTwine commented 2 years ago

I'm having the same issue: https://github.com/WrithemTwine/TwineChatBot/blob/main/StreamerBotLib/BotClients/Twitch/TwitchBotChatClient.cs

Windows 10 & 11, .NET 5 C#.

It appears: "initialize w/channel(s)" then "connect" will connect to channel I/O. "initialize, connect, add channels ( JoinChannel("channelname") )" does not appear connect to channel I/O - no I/O logging.

any "reconnect"/ "disconnect/connect" doesn't appear to connect to any I/O either.

this is a difficult situation, when trying to build fault tolerance into the overall bot client and adding features to permit the user to keep the app running but disconnect the chat client when not streaming. the apparent workaround is a complete "TwitchClient" instantiation each time trying to reconnect.

JulanDeAlb commented 2 years ago

Which TwitchLib.Client Nuget Version are you running ?

WrithemTwine commented 2 years ago

CurrentTwitchLibversions

JulanDeAlb commented 2 years ago

Besides from the TwitchLib.Api and TwitchLib.Client, do you use another thing ? If not, try to remove the TwitchLib one and install the TwitchLib.Client nuget with the latest preRelease and test if this would work. There was an issue in the Communication the last 1 or 2 years and i tried to resolve it, it is now in the prerelease since 3 months. The latest PreRelease is from April 28, 2022. Make sure you can see the preReleases

image

WrithemTwine commented 2 years ago

I'm using Helix for 'LiveStream' and 'Follows' services, expanded for a "new Clips" service, I've expanded for PubSub (Channel Point Redemptions), and under Helix some (streamer/other user) 'category', user ids, 'stream' settings, and other similar Helix calls (since some of TwitchLib is private, had to recreate some of it for expanding).

My bots section (and in development, is a streamer media overlay bot server to react to viewer events/ channel points, commands etc): https://github.com/WrithemTwine/TwineChatBot/tree/main/StreamerBotLib/BotClients

JulanDeAlb commented 2 years ago

The Issue with reconnecting in the TwitchLib.Client should be fixed in the latest PreRelease, try to get it into your custom made or do it seperate. This is the only thing i can help.

WrithemTwine commented 2 years ago

I could maybe do a temp rollback or just do a comment switch for the prior method calls (same method names, switch code).

The other point of concern was https://github.com/TwitchLib/TwitchLib/issues/1074, with adding new JoinedChannels.

As I recall:

works: connect channel w/channel in initialization (Twitch connect script) connection established

doesn't work: connect client add new Channel(s) (no Twitch connect script) no connection language, just "should be connected"

Hence, when I needed to reconnect I opted for a newly initialized client w/channel name and not even try to add a new JoinedChannel.

JulanDeAlb commented 2 years ago

I dont know if i dont understand what you want to tell me, im sry for that.

Are you having problems with the TwitchLib.Client section ?

If so, the nuget Package TwitchLib (includes all Packages from TwitchLib) has the normal TwitchLib.Client Version, it could be also an older version as the seperate TwitchLib.Client Nuget Package.

In the newest seperate TwitchLib.Client PreRelease Nuget Package there is an fix for the reconnect issue mentioned above if i understand that correct.

If client get disconnected, runing client.reconnect or client.connect doesnt reconnect.

It should autoreconnect and if it does, it will join all previously joined channel.

WrithemTwine commented 2 years ago

when you said try to use the reconnect code again, I'll add the prior code and do a check to see if the updated pre-release TwitchLib.Client does perform the reconnect. I don't have to do a full replace, just comment out the current code I have and plug-in the prior code for the test.

I also was explaining how using the JoinedChannels method didn't seem to work either, as in, connecting the client, then adding additional joined channels. If that's not intended, then perhaps "AddJoinedChannels" (whatever the method name) should have a different privacy.

I can't look for specifics at the moment, just started a stream. I did have time to drop in the pre-release TwitchLib client/pubsub (without the reconnect coding) and am using it in my bot right now for an online stream - the online active stream part can be difficult to test offline (the best bot test is usually an online stream to know what is coming into the bot). The bot hasn't crashed yet and is connected to channel.

JulanDeAlb commented 2 years ago

Ok then im sry for misunderstanding you.

WrithemTwine commented 2 years ago

ok, I don't believe the pre-release is functioning properly or I'm missing something critical.

I actually had "#if, #elseif, #endif" code blocks so I could use the prior code, defined here: https://github.com/WrithemTwine/TwineChatBot/blob/294850d4a6b40a8e666a97f70bafb6cc2fcd96ca/StreamerBotLib/BotClients/Twitch/TwitchBotChatClient.cs#L2, and changing the name reverts to earlier connect/disconnect code to the TwitchClient.

Using TwitchClient connect/disconnect:

The "connect, user list, disconnect, and connect" is not followed by a user list received. The code doesn't recreate any connection options, just connects again and the bot status log doesn't report receiving a user list after several minutes <- when a disconnect, connect should receive a user list.

My workaround code recreates a new TwitchClient with ClientOptions and most of the time functions properly. Every so often, I review the status log and there is no Twitch connect lingo, but a stop bot and start bot does correct this issue.

Correcting other issues and the current version of this issue code, I'm about to push a fix/updated release to the main branch (to help fix a new user crash issue).

JulanDeAlb commented 2 years ago

That it didn't recreate any connection option makes sense, at the point i tryed to fix it, it didnt recreate the joined channels because it got deleted somewhere so i fixed that and didnt look into the connection options if there is the same thing. While testing it myself for a week i didn't use the connection options so i couldn't have noticed anything there.

moo-the-cow commented 2 years ago

more like a hint: I constantly got disconnected silently and couldn't reconnect back. so I built my own logic to track keepalive (periodically every minute)

I stored the date when I last got a response from twitch and if it exceeded some time (2minutes) it was an indicator for me that I got silently disconnected. but not just that - connect/reconnect didn't seem to work. so I had to actually use the microsoft core WebSocketClient class for better State handling. I added something that if re-connect AND connect failed to completely drop/close/dispose everything and re-initialize (microsoft best practices) and connect. I think the logs are self explaining


[08/24/2022 18:51 |  3: Information ] 16:51: Writing: PING :tmi.twitch.tv

[08/24/2022 18:52 |  5: Warning     ] PONG received: 18:53 | Now: 18:52  Compare: 1

[08/24/2022 18:52 |  3: Information ] 16:52: Writing: PING :tmi.twitch.tv

[08/24/2022 18:53 |  5: Warning     ] PONG received: 18:53 | Now: 18:53  Compare: 1

[08/24/2022 18:53 |  3: Information ] 16:53: Writing: PING :tmi.twitch.tv

[08/24/2022 18:54 |  5: Warning     ] PONG received: 18:53 | Now: 18:54  Compare: -1

[08/24/2022 18:54 |  2: Critical    ] Last PONG from twitch missing for a while - lets reconnect the Client

[08/24/2022 18:54 |  3: Information ] 16:54: Reconnecting to Twitch

[08/24/2022 18:55 |  5: Warning     ] PONG received: 18:53 | Now: 18:55  Compare: -1

[08/24/2022 18:55 |  2: Critical    ] Reconnect didn't work. Lets try to connect the Client

[08/24/2022 18:55 |  3: Information ] 16:55: Connecting to: wss://irc-ws.chat.twitch.tv:443

[08/24/2022 18:55 |  3: Information ] 16:55: Should be connected!
WrithemTwine commented 2 years ago

@moo-the-cow

my solution for the stable TwitchLib Client version was to handle the "ClientDisconnected" event. That started the "create a new client and connect to channel" program flow. So I didn't bother to write in a 'client status' like you did, not saying it's wrong, I just don't have time for extra code banking on future TwitchLib versions correct the issue.

I haven't checked for a new pre-release version, but the couple versions I tried wouldn't let my client send "Disconnect()" and stay disconnected, it would always reconnect. The comments to my issue, [https://github.com/TwitchLib/TwitchLib/issues/1093], they're looking into: "client disconnects from a lag or Twitch problem, TwitchClient should reconnect (based on option setting)" while "code requests client to disconnect, TwitchClient should stay disconnected". For my Twitch bot app, I give the option to suspend Twitch Client with offline stream to prevent the ping/pong traffic.

What I've had trouble with is the TwitchLib library program flow diagrams sometimes change, where "Disconnect()" in one version doesn't act the same in another version. I know they work hard on the code to get it very stable, it's a big deal. I also understand there's an overall TwitchLib rewrite to handle current Twitch API. I haven't tried to step into TwitchLib coding because I have a lot to do in my own bot and I want the TwitchLib code to be independent for my bot (like if I'm involved, then my own bot code would change to use what I know from TwitchLib).

RNGIllSkillz commented 2 years ago

Thats exactly what I did. But the problem with creating new client on ClientDisconnect event is that for some reason sometime old client reconnects. So I end up connected to the same channel multiple time which is really bad.