Skip to content

Commit

Permalink
Add Twitch log. Fix various TwitchPlugin problems (#5)
Browse files Browse the repository at this point in the history
- it would crash if we did a reconnect() from one of the
TwitchLib.Client event handlers
- connect/reconnect logic was wierd: It would announce multiple
TwitchDisconnected() events, even if it wasnt connected meanwhile
  • Loading branch information
dennis committed Dec 30, 2020
1 parent 683cfc9 commit a10b4ed
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 86 deletions.
3 changes: 3 additions & 0 deletions App.config
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
<setting name="TwitchChannel" serializeAs="String">
<value />
</setting>
<setting name="TwitchLog" serializeAs="String">
<value>False</value>
</setting>
<setting name="TxrxIpPort" serializeAs="String">
<value />
</setting>
Expand Down
114 changes: 74 additions & 40 deletions Backend/Plugins/TwitchPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class TwitchPlugin : BasePlugin
private string? TwitchUsername;
private string? TwitchChannel;
private string? TwitchToken;
private bool TwitchLog;
private bool RequestReconnect;
private bool AnnouncedConnected = false;

public TwitchPlugin(string id, IEventBus eventBus, TwitchSettings settings) : base(id, "TwitchPlugin", "TwitchPlugin", "TwitchPlugin")
{
Expand All @@ -43,14 +46,18 @@ public TwitchPlugin(string id, IEventBus eventBus, TwitchSettings settings) : ba
private void OnTwitchSettings(TwitchSettings settings)
{
{
if (TwitchUsername != settings.TwitchUsername || TwitchToken != settings.TwitchToken || TwitchChannel != settings.TwitchChannel)
if (TwitchUsername != settings.TwitchUsername || TwitchToken != settings.TwitchToken || TwitchChannel != settings.TwitchChannel || TwitchLog != settings.TwitchLog)
{
TwitchUsername = settings.TwitchUsername;
TwitchChannel = settings.TwitchChannel;
TwitchToken = settings.TwitchToken;
TwitchLog = settings.TwitchLog;

if (String.IsNullOrEmpty(TwitchChannel))
TwitchChannel = TwitchUsername;

Disconnect();
Connnect();
Connect();
}
};
}
Expand All @@ -62,79 +69,107 @@ public override void OnDisable()

private void Disconnect()
{
AnnounceDisconnected();
Client?.Disconnect();
Client = null;
}

public override void OnEnable()
{
Connnect();
Connect();
}

public override void Loop()
{
if(RequestReconnect)
{
Disconnect();
Connect();

RequestReconnect = false;
}

System.Threading.Thread.Sleep(500);
}

private void AnnounceConnected()
{
if (!AnnouncedConnected)
{
EventBus.PublishEvent(new TwitchConnected());
AnnouncedConnected = true;
}
}

private void Connnect()
private void AnnounceDisconnected()
{
if (AnnouncedConnected)
{
EventBus.PublishEvent(new TwitchDisconnected());
AnnouncedConnected = false;
}
}

private void Connect()
{
if (!Enabled)
return;

if (TwitchUsername == null || TwitchToken == null || TwitchUsername.Length == 0 || TwitchToken.Length == 0)
if (string.IsNullOrEmpty(TwitchUsername) || string.IsNullOrEmpty(TwitchToken))
return;

if (Client != null && Client.IsConnected)
return;

if (Client == null)
ConnectionCredentials credentials = new ConnectionCredentials(TwitchUsername, TwitchToken, "ws://irc-ws.chat.twitch.tv:80");
var clientOptions = new ClientOptions
{
ConnectionCredentials credentials = new ConnectionCredentials(TwitchUsername, TwitchToken, "ws://irc-ws.chat.twitch.tv:80");
var clientOptions = new ClientOptions
{
MessagesAllowedInPeriod = 750,
ThrottlingPeriod = TimeSpan.FromSeconds(30)
};

WebSocketClient customClient = new WebSocketClient(clientOptions);
Client = new TwitchClient(customClient);
Client.Initialize(credentials, TwitchChannel);

Client.OnConnected += OnConnected;
Client.OnChatCommandReceived += OnChatCommandReceived;
Client.OnDisconnected += OnDisconnect;
Client.OnError += Client_OnError;
Client.OnIncorrectLogin += Client_OnIncorrectLogin;
Client.OnReconnected += Client_OnReconnected;
Client.OnLog += Client_OnLog;

Client.Connect();
}
MessagesAllowedInPeriod = 750,
ThrottlingPeriod = TimeSpan.FromSeconds(30)
};

WebSocketClient customClient = new WebSocketClient(clientOptions);
Client = new TwitchClient(customClient);
Client.Initialize(credentials, TwitchChannel);

Client.OnConnected += OnConnected;
Client.OnChatCommandReceived += OnChatCommandReceived;
Client.OnDisconnected += OnDisconnect;
Client.OnError += OnError;
Client.OnIncorrectLogin += OnIncorrectLogin;
Client.OnJoinedChannel += OnJoinedChannel;
if(TwitchLog)
Client.OnLog += OnLog;

Client.Connect();
}

private void Client_OnLog(object sender, OnLogArgs e)
private void OnJoinedChannel(object sender, OnJoinedChannelArgs e)
{
EventBus.PublishEvent(new CommandWriteToConsole { Message = $"Twitch: {e.Data}" });
AnnounceConnected();
}

private void Client_OnReconnected(object sender, OnReconnectedEventArgs e)
private void OnLog(object sender, OnLogArgs e)
{
EventBus.PublishEvent(new TwitchConnected());
EventBus.PublishEvent(new CommandWriteToConsole { Message = $"Twitch connected as {TwitchUsername} to channel {TwitchChannel}" });
EventBus.PublishEvent(new CommandWriteToConsole { Message = $"Twitch log: {e.Data}" });
}

private void Client_OnIncorrectLogin(object sender, OnIncorrectLoginArgs e)
private void OnIncorrectLogin(object sender, OnIncorrectLoginArgs e)
{
EventBus.PublishEvent(new CommandWriteToConsole { Message = $"Twitch Error: {e.Exception.Message}" });
EventBus.PublishEvent(new Shared.Events.Internal.CommandPluginDisable() { Id = this.Id });
}

private void Client_OnError(object sender, OnErrorEventArgs e)
private void OnError(object sender, OnErrorEventArgs e)
{
EventBus.PublishEvent(new CommandWriteToConsole { Message = $"Twitch Error: {e.Exception.Message}" });
EventBus.PublishEvent(new Shared.Events.Internal.CommandPluginDisable() { Id = this.Id });
}

private void OnDisconnect(object sender, OnDisconnectedEventArgs e)
{
EventBus.PublishEvent(new TwitchDisconnected());
if (Enabled)
Client?.Reconnect();
AnnounceDisconnected();
RequestReconnect = true;
}

private void OnChatCommandReceived(object sender, OnChatCommandReceivedArgs e)
Expand All @@ -158,8 +193,7 @@ private void OnChatCommandReceived(object sender, OnChatCommandReceivedArgs e)

private void OnConnected(object sender, OnConnectedArgs e)
{
EventBus.PublishEvent(new TwitchConnected());
EventBus.PublishEvent(new CommandWriteToConsole { Message = $"Twitch connected as {TwitchUsername}" });
EventBus.PublishEvent(new CommandWriteToConsole { Message = $"Twitch connected as {TwitchUsername} to channel {TwitchChannel}" });
}
}
}
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
- Replace DebugOutputPlugin with a Lua script (LuaScript/debug.lua)
- Twitch: Allow Twitchchannel to be configured (it was assumed that username and channel would be the same)
- Added TransmitterPlugin and ReceiverPlugin. Allows one instance of Slipstream to forward its events to another instance using a TCP/IP connection. This features SHOULD ONLY be used locally. There is no authentication or encryption of the events.
- Twitch: Optionally show twitch log
- TwitchConnected event are now sent when we joined the channel (not just connected to twitch servers)

**Bugfixes**
- Auto-create both "Audio" and "Scripts" directories

## [0.1.0](https://github.com/dennis/slipstream/releases/tag/v0.1.0) (2020-12-23)
[Full Changelog](https://github.com/dennis/slipstream/compare/be57351b1d0c5ff75a87ece10b3e7c272a980446...v0.1.0)

**Initial version**
**Initial version**
3 changes: 2 additions & 1 deletion Frontend/ApplicationConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public TwitchSettings GetTwitchSettingsEvent()
{
TwitchUsername = Settings.TwitchUsername,
TwitchChannel = Settings.TwitchChannel,
TwitchToken = Settings.TwitchToken
TwitchToken = Settings.TwitchToken,
TwitchLog = Settings.TwitchLog,
};
}

Expand Down
91 changes: 52 additions & 39 deletions Frontend/SettingsForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a10b4ed

Please sign in to comment.