Exemple #1
0
        public static async Task <string> MainAsync()
        {
            #if debug
            Console.WriteLine("Main thread before GetAPI await : " + Thread.CurrentThread.ManagedThreadId);
            #endif

            TwitchAPI twitchAPI = new TwitchAPI();
            twitchAPI.BuildConfiguration();
            twitchAPI.SetupHttpClient();

            bool success = await twitchAPI.BuildChannelDictionary();

            if (!success)
            {
                return("Error connecting to API");
            }

            #if debug
            Console.WriteLine("Main thread after GetAPI await : " + Thread.CurrentThread.ManagedThreadId);
            #endif

            StatisticsService ss = new StatisticsService(new PRIVMSG());
            success = await ss.StartService(twitchAPI);

            if (!success)
            {
                return("Error getting emoticons");
            }

            #if debug
            Console.WriteLine("Main thread before ReportStatistics : " + Thread.CurrentThread.ManagedThreadId);
            #endif
            //intentionally not awaited so we can fire and forget our while loop, yields to this calling code after first await
            var ignore = ss.ReportStatistics(5000);
            #if debug
            Console.WriteLine("Main thread after ReportStatistics: " + Thread.CurrentThread.ManagedThreadId);
            #endif

            return(await ConnectIRC(ss));//Await this loop its our main logic loop
        }
Exemple #2
0
        public static async Task <string> ConnectIRC(StatisticsService ss)
        {
            var configuration = new ConfigurationBuilder().AddIniFile(Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "..\\..\\..\\config.ini"))).Build();

            if (string.IsNullOrEmpty(configuration["twitchirc:servername"]) || string.IsNullOrEmpty(configuration["twitchirc:portnumber"]) ||
                string.IsNullOrEmpty(configuration["twitchirc:oauth"]) || string.IsNullOrEmpty(configuration["twitchirc:nick"]))
            {
                return("Could not read IRCConfiguration");
            }

            DateTime pingTime = new DateTime();

            using (TcpClient tcpClient = new TcpClient())
            {
                #if debug
                Console.WriteLine("ConnectIRC before await : " + Thread.CurrentThread.ManagedThreadId);
                #endif
                await tcpClient.ConnectAsync(configuration["twitchirc:servername"], Convert.ToInt32(configuration["twitchirc:portnumber"]));

                using (StreamReader streamReader = new StreamReader(tcpClient.GetStream()))
                {
                    using (StreamWriter streamWriter = new StreamWriter(tcpClient.GetStream())
                    {
                        AutoFlush = true
                    })
                    {
                        streamWriter.AutoFlush = true;
                        await streamWriter.WriteLineAsync($"PASS oauth:{configuration["twitchirc:oauth"]}");

                        await streamWriter.WriteLineAsync($"NICK {configuration["twitchirc:nick"]}");

                        foreach (string channelName in concurrentDictionary.Values)
                        {
                            await streamWriter.WriteLineAsync($"JOIN #{channelName}");
                        }

                        #if debug
                        Console.WriteLine("ConnectIRC after await : " + Thread.CurrentThread.ManagedThreadId);
                        #endif
                        string previousLine = "";
                        while (true)
                        {
                            string readLine = await streamReader.ReadLineAsync();

                            if (readLine.Contains("PING") || (DateTime.Now - pingTime).TotalMinutes > 4)
                            {
                                Console.WriteLine(readLine);
                                await streamWriter.WriteLineAsync("PING :tmi.twitch.tv");

                                await streamWriter.WriteLineAsync("PONG :tmi.twitch.tv");

                                pingTime = DateTime.Now;
                            }

                            if (string.IsNullOrEmpty(readLine))
                            {
                                return(previousLine);
                            }
                            //ss.DistributeInformation(readLine);
                            previousLine = readLine;
                        }
                    }
                }
            }
        }