Search code examples
c#socketsirctwitch

TcpClient disconnects after awhile from Twitch IRC


My issue is that after I connect to the IRC and I am receiving raw text from the IRC and logging it but then it just decides to disconnect from the server after inactivity or 2-3 seconds after I start it (unconfirmed).

Any help would be appreciated. Here's my code (had issues posting it here): http://pastebin.com/Ls5rv0RP

I need it to stop disconnecting but I cant really find a way to. I know the popular mIRC client disconnects from Twitch after x amount of time but reconnects and that's fine to as long as it KNOWS to reconnect in a timely manner (2-5 seconds).

The part were it replys to PING/PONG's:

if (buf.StartsWith("PING ")) output.Write(buf.Replace("PING", "PONG") + "\r\n"); output.Flush();

Any help is appreciated.


Solution

  • This code below helps keep my Twitch bot from disconnecting by running a separate thread that pings to the IRC server.

    PingSender Class:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    
    namespace TwitchBot
    {
        /*
        * Class that sends PING to irc server every 5 minutes
        */
        class PingSender
        {
            static string PING = "PING ";
            private Thread pingSender;
    
            // Empty constructor makes instance of Thread
            public PingSender() 
            {
                pingSender = new Thread (new ThreadStart (this.Run) ); 
            } 
            // Starts the thread
            public void Start() 
            { 
                pingSender.Start(); 
            } 
            // Send PING to irc server every 5 minutes
            public void Run()
            {
                while (true)
                {
                    Program.irc.sendIrcMessage(PING + "irc.twitch.tv");
                    Thread.Sleep(300000); // 5 minutes
                }
            }
        }
    }
    

    Usage:

    /* Ping to twitch server to prevent auto-disconnect */
    PingSender ping = new PingSender();
    ping.Start();