Skip to content

Commit

Permalink
Merge pull request #119 from Bukk94/feature/ImplementBreakingChangesF…
Browse files Browse the repository at this point in the history
…romCommunicationLib

Updated TwitchLib.Communication to newest version
  • Loading branch information
Syzuna authored Jun 8, 2023
2 parents 658e305 + dcef147 commit 3385627
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 20 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,6 @@ $RECYCLE.BIN/
*.msp

# Windows shortcuts
*.lnk
*.lnk

.idea/*
21 changes: 21 additions & 0 deletions TwitchLib.PubSub/Interfaces/ITwitchPubSub.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using TwitchLib.PubSub.Events;

namespace TwitchLib.PubSub.Interfaces
Expand Down Expand Up @@ -169,10 +170,22 @@ public interface ITwitchPubSub
/// Connects this instance.
/// </summary>
void Connect();

/// <summary>
/// Connects this instance.
/// </summary>
Task ConnectAsync();

/// <summary>
/// Disconnects this instance.
/// </summary>
void Disconnect();

/// <summary>
/// Disconnects this instance.
/// </summary>
Task DisconnectAsync();

/// <summary>
/// Listens to bits events.
/// </summary>
Expand Down Expand Up @@ -248,6 +261,14 @@ public interface ITwitchPubSub
/// <param name="oauth">The oauth.</param>
/// <param name="unlisten">if set to <c>true</c> [unlisten].</param>
void SendTopics(string oauth = null, bool unlisten = false);

/// <summary>
/// Sends the topics.
/// </summary>
/// <param name="oauth">The oauth.</param>
/// <param name="unlisten">if set to <c>true</c> [unlisten].</param>
Task SendTopicsAsync(string oauth = null, bool unlisten = false);

/// <summary>
/// Tests the message parser.
/// </summary>
Expand Down
8 changes: 4 additions & 4 deletions TwitchLib.PubSub/TwitchLib.PubSub.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>TwitchLib.PubSub</PackageId>
<VersionPrefix>3.2.6</VersionPrefix>
<VersionPrefix>4.0.0</VersionPrefix>
<VersionSuffix>$(VersionSuffix)</VersionSuffix>
<Description>PubSub component of TwitchLib. This component allows you to access Twitch's PubSub event system and subscribe/unsubscribe from topics.</Description>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
Expand All @@ -18,13 +18,13 @@
<RepositoryType>Git</RepositoryType>
<PackageTags>twitch library irc chat c# csharp api events pubsub net standard 2.0</PackageTags>
<NeutralLanguage>en-US</NeutralLanguage>
<AssemblyVersion>3.2.6</AssemblyVersion>
<FileVersion>3.2.6</FileVersion>
<AssemblyVersion>4.0.0</AssemblyVersion>
<FileVersion>4.0.0</FileVersion>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
<PackageReference Include="TwitchLib.Communication" Version="1.0.4" />
<PackageReference Include="TwitchLib.Communication" Version="2.0.0-preview-8108207851b6bbef87062f15a497036183ffea70" />
</ItemGroup>
</Project>
51 changes: 36 additions & 15 deletions TwitchLib.PubSub/TwitchPubSub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using TwitchLib.Communication.Clients;
using TwitchLib.Communication.Enums;
Expand Down Expand Up @@ -288,7 +289,7 @@ public TwitchPubSub(ILogger<TwitchPubSub> logger = null)
{
_logger = logger;

var options = new ClientOptions { ClientType = ClientType.PubSub };
var options = new ClientOptions(clientType: ClientType.PubSub);
_socket = new WebSocketClient(options);

_socket.OnConnected += Socket_OnConnected;
Expand All @@ -297,7 +298,7 @@ public TwitchPubSub(ILogger<TwitchPubSub> logger = null)
_socket.OnDisconnected += Socket_OnDisconnected;

_pongTimer.Interval = 15000; //15 seconds, we should get a pong back within 10 seconds.
_pongTimer.Elapsed += PongTimerTick;
_pongTimer.Elapsed += PongTimerTickAsync;
}

/// <summary>
Expand Down Expand Up @@ -345,7 +346,7 @@ private void Socket_OnConnected(object sender, EventArgs e)
{
_logger?.LogInformation("PubSub Websocket connection established");
_pingTimer.Interval = 180000;
_pingTimer.Elapsed += PingTimerTick;
_pingTimer.Elapsed += PingTimerTickAsync;
_pingTimer.Start();
OnPubSubServiceConnected?.Invoke(this, null);
}
Expand All @@ -355,7 +356,7 @@ private void Socket_OnConnected(object sender, EventArgs e)
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="ElapsedEventArgs"/> instance containing the event data.</param>
private void PingTimerTick(object sender, ElapsedEventArgs e)
private async void PingTimerTickAsync(object sender, ElapsedEventArgs e)
{
//Reset pong state.
_pongReceived = false;
Expand All @@ -364,7 +365,8 @@ private void PingTimerTick(object sender, ElapsedEventArgs e)
var data = new JObject(
new JProperty("type", "PING")
);
_socket.Send(data.ToString());

await _socket.SendAsync(data.ToString());

//Start pong timer.
_pongTimer.Start();
Expand All @@ -375,7 +377,7 @@ private void PingTimerTick(object sender, ElapsedEventArgs e)
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="ElapsedEventArgs"/> instance containing the event data.</param>
private void PongTimerTick(object sender, ElapsedEventArgs e)
private async void PongTimerTickAsync(object sender, ElapsedEventArgs e)
{
//Stop the pong timer.
_pongTimer.Stop();
Expand All @@ -388,7 +390,7 @@ private void PongTimerTick(object sender, ElapsedEventArgs e)
else
{
//Otherwise we're disconnected so close the socket.
_socket.Close();
await _socket.CloseAsync();
}
}

Expand Down Expand Up @@ -680,7 +682,11 @@ private void ParseMessage(string message)
case "pong":
_pongReceived = true;
return;
case "reconnect": _socket.Close(); break;
case "reconnect":
// This does not fit here. This method parses message, it shouldn't do any action.
// TODO: Fire event to trigger socket close
_socket.CloseAsync().GetAwaiter().GetResult();
break;
}
UnaccountedFor(message);
}
Expand Down Expand Up @@ -720,13 +726,19 @@ private void ListenToTopics(params string[] topics)
}
}

/// <inheritdoc />
public void SendTopics(string oauth = null, bool unlisten = false)
{
SendTopicsAsync(oauth, unlisten).GetAwaiter().GetResult();
}

/// <inheritdoc />
/// <summary>
/// Sends the topics.
/// </summary>
/// <param name="oauth">The oauth.</param>
/// <param name="unlisten">if set to <c>true</c> [unlisten].</param>
public void SendTopics(string oauth = null, bool unlisten = false)
public async Task SendTopicsAsync(string oauth = null, bool unlisten = false)
{
if (oauth != null && oauth.Contains("oauth:"))
{
Expand Down Expand Up @@ -764,7 +776,7 @@ public void SendTopics(string oauth = null, bool unlisten = false)
((JObject)jsonData.SelectToken("data"))?.Add(new JProperty("auth_token", oauth));
}

_socket.Send(jsonData.ToString());
await _socket.SendAsync(jsonData.ToString());

_topicList.Clear();
}
Expand Down Expand Up @@ -968,16 +980,25 @@ public void ListenToPredictions(string channelTwitchId)
/// </summary>
public void Connect()
{
_socket.Open();
ConnectAsync().GetAwaiter().GetResult();
}

/// <inheritdoc />
public async Task ConnectAsync()
{
await _socket.OpenAsync();
}

/// <inheritdoc />
/// <summary>
/// What do you think it does? :)
/// </summary>
public void Disconnect()
{
_socket.Close();
DisconnectAsync().GetAwaiter().GetResult();
}

/// <inheritdoc />
public async Task DisconnectAsync()
{
await _socket.CloseAsync();
}

/// <inheritdoc />
Expand Down

0 comments on commit 3385627

Please sign in to comment.