Add extension to recycle websockets (#50)

Signed-off-by: Macocian Alexandru Victor <amacocian@yahoo.com>
This commit is contained in:
2023-05-15 11:21:29 +02:00
committed by GitHub
parent 0de73a967b
commit fe049d4c00
3 changed files with 10 additions and 2 deletions
@@ -7,7 +7,7 @@
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<RootNamespace>System</RootNamespace>
<Version>1.6.1</Version>
<Version>1.6.2</Version>
<Authors>Alexandru Macocian</Authors>
<RepositoryUrl>https://github.com/AlexMacocian/SystemExtensions</RepositoryUrl>
<Description>Extensions for the System namespace</Description>
@@ -9,7 +9,7 @@ namespace System.Net.WebSockets;
public sealed class ClientWebSocket<TScope> : IClientWebSocket<TScope>, IDisposable
{
private readonly ILogger<TScope>? logger;
private readonly ClientWebSocket internalWebSocket = new();
private ClientWebSocket internalWebSocket = new();
public ClientWebSocketOptions Options => this.internalWebSocket.Options;
@@ -89,4 +89,10 @@ public sealed class ClientWebSocket<TScope> : IClientWebSocket<TScope>, IDisposa
scopedLogger?.LogDebug($"Type: {messageType}\nCount: {buffer.Count}\nEndOfMessage: {endOfMessage}");
return this.internalWebSocket.SendAsync(buffer, messageType, endOfMessage, cancellationToken);
}
public void RefreshSocket(ClientWebSocket? clientWebSocket = null)
{
this.internalWebSocket?.Dispose();
this.internalWebSocket = clientWebSocket ?? new ClientWebSocket();
}
}
@@ -28,4 +28,6 @@ public interface IClientWebSocket<TScope>
Task<WebSocketReceiveResult> ReceiveAsync(ArraySegment<byte> buffer, CancellationToken cancellationToken);
Task SendAsync(ArraySegment<byte> buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken cancellationToken);
void RefreshSocket(ClientWebSocket? clientWebSocket = default);
}