/// <summary> /// Disconnects from Twitch's IRC client using the current instance of an IrcClient. /// </summary> protected void Disconnect() { if (_ircStatus != null && _ircStatus.State == Types.StatusLevel.Stopping) { EventLogger.AddEvent(Types.EventLevel.Error, "A request to disconnect from the Twitch chat services has already been received, ignoring duplicate request.", "Connect()"); return; } _ircStatus = new IrcStatus { State = Types.StatusLevel.Stopping }; _ircClient?.Close(); }
/// <summary> /// Creates and runs a new IrcClient upon instantiating. /// </summary> /// <param name="nickname">The bot's Twitch username.</param> /// <param name="passwordToken">A password token generated by "https://twitchapps.com/tmi/".</param> /// <param name="channel">The bot's target channel (the "broadcaster").</param> protected IrcClient(string nickname, string passwordToken, string channel) { if (_ircClient != null) { EventLogger.AddEvent(Types.EventLevel.Error, "A connection was already established. Ignoring request to start an additional one.", "IrcClient()"); return; } _ircStatus = new IrcStatus {State = Types.StatusLevel.Starting}; _nickname = nickname; _passwordToken = passwordToken; _channel = channel; Connect(); }
/// <summary> /// Creates and runs a new IrcClient upon instantiating. /// </summary> /// <param name="nickname">The bot's Twitch username.</param> /// <param name="passwordToken">A password token generated by "https://twitchapps.com/tmi/".</param> /// <param name="channel">The bot's target channel (the "broadcaster").</param> protected IrcClient(string nickname, string passwordToken, string channel) { if (_ircClient != null) { EventLogger.AddEvent(Types.EventLevel.Error, "A connection was already established. Ignoring request to start an additional one.", "IrcClient()"); return; } _ircStatus = new IrcStatus { State = Types.StatusLevel.Starting }; _nickname = nickname; _passwordToken = passwordToken; _channel = channel; Connect(); }
/// <summary> /// Connects to Twitch's IRC client using the current instance of an IrcClient. /// </summary> private void Connect() { var connectCount = 1; _ircClient = new TcpClient(); // TODO: Add check for internet connection. while (!_ircClient.Connected) { if (connectCount == 1) { EventLogger.AddEvent(Types.EventLevel.Info, "Attempting to connect to the Twitch chat services.", "Connect()"); } else { EventLogger.AddEvent(Types.EventLevel.Warning, "Initial connection failed, attempting to reconnect." + Environment.NewLine + "Current amount of tries: " + connectCount, "Connect()"); } try { _ircClient.Connect("irc.twitch.tv", 6667); _streamReader = new StreamReader(_ircClient.GetStream()); _streamWriter = new StreamWriter(_ircClient.GetStream()) {AutoFlush = true}; AddToQueue("PASS " + _passwordToken); AddToQueue("NICK " + _nickname); AddToQueue("JOIN #" + _channel); } catch (SocketException exception) { EventLogger.AddEvent(Types.EventLevel.Error, "Unable to connect to the Twitch chat services, reattempting in 5 seconds.", exception, "Connect()"); _ircClient.Close(); connectCount++; Thread.Sleep(5000); } catch (Exception exception) { EventLogger.AddEvent(Types.EventLevel.Exception, "An exception occured while trying to connect.", exception, "Connect()"); Disconnect(); } if (_ircClient.Connected) { EventLogger.AddEvent(Types.EventLevel.Info, "Successfully connected to the Twitch chat services.", "Connect()"); StartThreads(); _ircStatus = new IrcStatus {State = Types.StatusLevel.Running}; } } }
/// <summary> /// Connects to Twitch's IRC client using the current instance of an IrcClient. /// </summary> private void Connect() { var connectCount = 1; _ircClient = new TcpClient(); // TODO: Add check for internet connection. while (!_ircClient.Connected) { if (connectCount == 1) { EventLogger.AddEvent(Types.EventLevel.Info, "Attempting to connect to the Twitch chat services.", "Connect()"); } else { EventLogger.AddEvent(Types.EventLevel.Warning, "Initial connection failed, attempting to reconnect." + Environment.NewLine + "Current amount of tries: " + connectCount, "Connect()"); } try { _ircClient.Connect("irc.twitch.tv", 6667); _streamReader = new StreamReader(_ircClient.GetStream()); _streamWriter = new StreamWriter(_ircClient.GetStream()) { AutoFlush = true }; AddToQueue("PASS " + _passwordToken); AddToQueue("NICK " + _nickname); AddToQueue("JOIN #" + _channel); } catch (SocketException exception) { EventLogger.AddEvent(Types.EventLevel.Error, "Unable to connect to the Twitch chat services, reattempting in 5 seconds.", exception, "Connect()"); _ircClient.Close(); connectCount++; Thread.Sleep(5000); } catch (Exception exception) { EventLogger.AddEvent(Types.EventLevel.Exception, "An exception occured while trying to connect.", exception, "Connect()"); Disconnect(); } if (_ircClient.Connected) { EventLogger.AddEvent(Types.EventLevel.Info, "Successfully connected to the Twitch chat services.", "Connect()"); StartThreads(); _ircStatus = new IrcStatus { State = Types.StatusLevel.Running }; } } }