Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/Nullinside.Api.Common/Extensions/WebSocketExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Net.WebSockets;
using System.Text;

namespace Nullinside.Api.Common.Extensions;

/// <summary>
/// Extensions for the <see cref="WebSocket" /> class.
/// </summary>
public static class WebSocketExtensions {
/// <summary>
/// Sends text over a web socket.
/// </summary>
/// <param name="webSocket">The web socket to send the message over.</param>
/// <param name="message">The message to send.</param>
/// <param name="cancelToken">The cancellation token.</param>
public static async Task SendTextAsync(this WebSocket webSocket, string message, CancellationToken cancelToken = new()) {
await webSocket.SendAsync(Encoding.ASCII.GetBytes(message), WebSocketMessageType.Text, true, cancelToken);
}

/// <summary>
/// Receives text over a web socket.
/// </summary>
/// <param name="webSocket">The web socket to send the message over.</param>
/// <param name="cancelToken">The cancellation token.</param>
/// <returns></returns>
public static async Task<string> ReceiveTextAsync(this WebSocket webSocket, CancellationToken cancelToken = new()) {
WebSocketReceiveResult response;
var fullMessage = new List<byte>();

do {
var data = new ArraySegment<byte>(new byte[1024]);
response = await webSocket.ReceiveAsync(data, cancelToken);
fullMessage.AddRange(data);
} while (null == response.CloseStatus);

return Encoding.ASCII.GetString(fullMessage.ToArray());
}
}
4 changes: 2 additions & 2 deletions src/Nullinside.Api.Common/Nullinside.Api.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

<ItemGroup>
<PackageReference Include="log4net" Version="3.1.0"/>
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.7" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.7"/>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3"/>
<PackageReference Include="SSH.NET" Version="2025.0.0"/>
<PackageReference Include="TwitchLib.Api" Version="3.9.0"/>
<PackageReference Include="TwitchLib.Client" Version="3.4.0" />
<PackageReference Include="TwitchLib.Client" Version="3.4.0"/>
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Nullinside.Api.Common/Twitch/ITwitchClientProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public interface ITwitchClientProxy : IDisposable, IAsyncDisposable {
/// <param name="callback">The callback to remove.</param>
/// <returns>An asynchronous task.</returns>
void RemoveRaidCallback(string channel, Action<OnRaidNotificationArgs> callback);

/// <summary>
/// Adds a callback for being disconnected from the twitch chat server.
/// </summary>
Expand Down
14 changes: 7 additions & 7 deletions src/Nullinside.Api.Common/Twitch/TwitchClientProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ public class TwitchClientProxy : ITwitchClientProxy {
/// The callback(s) to invoke when a channel receives a ban message.
/// </summary>
private readonly Dictionary<string, Action<OnUserBannedArgs>?> _onUserBanReceived = new();

/// <summary>
/// The callback(s) to invoke when the twitch chat client is disconnected from twitch chat.
/// </summary>
private Action? _onDisconnected;

/// <summary>
/// A timer used to re-connect the Twitch chat client.
Expand All @@ -73,13 +68,18 @@ public class TwitchClientProxy : ITwitchClientProxy {
/// </summary>
private TwitchClient? _client;

/// <summary>
/// The callback(s) to invoke when the twitch chat client is disconnected from twitch chat.
/// </summary>
private Action? _onDisconnected;

/// <summary>
/// The web socket to connect to twitch chat with.
/// </summary>
private WebSocketClient? _socket;

/// <summary>
/// The twitch OAuth token to use to connect.
/// The twitch OAuth token to use to connect.
/// </summary>
private string? _twitchOAuthToken;

Expand Down Expand Up @@ -235,7 +235,7 @@ public void RemoveBannedCallback(string channel, Action<OnUserBannedArgs> callba
_onUserBanReceived.Remove(channel);
}
}

/// <inheritdoc />
public void AddDisconnectedCallback(Action callback) {
_onDisconnected -= callback;
Expand Down