Skip to content

Commit ffac796

Browse files
Merge pull request #110 from nullinside-development-group/feat/login
feat: adding web socket extensions
2 parents ad4e87a + afc14ee commit ffac796

File tree

4 files changed

+48
-10
lines changed

4 files changed

+48
-10
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Net.WebSockets;
2+
using System.Text;
3+
4+
namespace Nullinside.Api.Common.Extensions;
5+
6+
/// <summary>
7+
/// Extensions for the <see cref="WebSocket" /> class.
8+
/// </summary>
9+
public static class WebSocketExtensions {
10+
/// <summary>
11+
/// Sends text over a web socket.
12+
/// </summary>
13+
/// <param name="webSocket">The web socket to send the message over.</param>
14+
/// <param name="message">The message to send.</param>
15+
/// <param name="cancelToken">The cancellation token.</param>
16+
public static async Task SendTextAsync(this WebSocket webSocket, string message, CancellationToken cancelToken = new()) {
17+
await webSocket.SendAsync(Encoding.ASCII.GetBytes(message), WebSocketMessageType.Text, true, cancelToken);
18+
}
19+
20+
/// <summary>
21+
/// Receives text over a web socket.
22+
/// </summary>
23+
/// <param name="webSocket">The web socket to send the message over.</param>
24+
/// <param name="cancelToken">The cancellation token.</param>
25+
/// <returns></returns>
26+
public static async Task<string> ReceiveTextAsync(this WebSocket webSocket, CancellationToken cancelToken = new()) {
27+
WebSocketReceiveResult response;
28+
var fullMessage = new List<byte>();
29+
30+
do {
31+
var data = new ArraySegment<byte>(new byte[1024]);
32+
response = await webSocket.ReceiveAsync(data, cancelToken);
33+
fullMessage.AddRange(data);
34+
} while (null == response.CloseStatus);
35+
36+
return Encoding.ASCII.GetString(fullMessage.ToArray());
37+
}
38+
}

src/Nullinside.Api.Common/Nullinside.Api.Common.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818

1919
<ItemGroup>
2020
<PackageReference Include="log4net" Version="3.1.0"/>
21-
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.7" />
21+
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.7"/>
2222
<PackageReference Include="Newtonsoft.Json" Version="13.0.3"/>
2323
<PackageReference Include="SSH.NET" Version="2025.0.0"/>
2424
<PackageReference Include="TwitchLib.Api" Version="3.9.0"/>
25-
<PackageReference Include="TwitchLib.Client" Version="3.4.0" />
25+
<PackageReference Include="TwitchLib.Client" Version="3.4.0"/>
2626
</ItemGroup>
2727

2828
<ItemGroup>

src/Nullinside.Api.Common/Twitch/ITwitchClientProxy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public interface ITwitchClientProxy : IDisposable, IAsyncDisposable {
7373
/// <param name="callback">The callback to remove.</param>
7474
/// <returns>An asynchronous task.</returns>
7575
void RemoveRaidCallback(string channel, Action<OnRaidNotificationArgs> callback);
76-
76+
7777
/// <summary>
7878
/// Adds a callback for being disconnected from the twitch chat server.
7979
/// </summary>

src/Nullinside.Api.Common/Twitch/TwitchClientProxy.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ public class TwitchClientProxy : ITwitchClientProxy {
5252
/// The callback(s) to invoke when a channel receives a ban message.
5353
/// </summary>
5454
private readonly Dictionary<string, Action<OnUserBannedArgs>?> _onUserBanReceived = new();
55-
56-
/// <summary>
57-
/// The callback(s) to invoke when the twitch chat client is disconnected from twitch chat.
58-
/// </summary>
59-
private Action? _onDisconnected;
6055

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

71+
/// <summary>
72+
/// The callback(s) to invoke when the twitch chat client is disconnected from twitch chat.
73+
/// </summary>
74+
private Action? _onDisconnected;
75+
7676
/// <summary>
7777
/// The web socket to connect to twitch chat with.
7878
/// </summary>
7979
private WebSocketClient? _socket;
8080

8181
/// <summary>
82-
/// The twitch OAuth token to use to connect.
82+
/// The twitch OAuth token to use to connect.
8383
/// </summary>
8484
private string? _twitchOAuthToken;
8585

@@ -235,7 +235,7 @@ public void RemoveBannedCallback(string channel, Action<OnUserBannedArgs> callba
235235
_onUserBanReceived.Remove(channel);
236236
}
237237
}
238-
238+
239239
/// <inheritdoc />
240240
public void AddDisconnectedCallback(Action callback) {
241241
_onDisconnected -= callback;

0 commit comments

Comments
 (0)