Skip to content

Commit

Permalink
added the interface IWebSocket for easier mock up
Browse files Browse the repository at this point in the history
  • Loading branch information
kerryjiang committed Jul 20, 2024
1 parent 904d5e9 commit 8bbb61b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
23 changes: 23 additions & 0 deletions src/WebSocket4Net/IWebSocket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Buffers;
using System.Threading;
using System.Threading.Tasks;
using SuperSocket.WebSocket;

namespace WebSocket4Net
{
public interface IWebSocket
{
ValueTask<bool> OpenAsync(CancellationToken cancellationToken = default);

ValueTask<WebSocketPackage> ReceiveAsync();

ValueTask SendAsync(string message);

ValueTask SendAsync(ReadOnlyMemory<byte> data);

ValueTask SendAsync(ref ReadOnlySequence<byte> sequence);

ValueTask CloseAsync(CloseReason closeReason, string message = null);
}
}
9 changes: 2 additions & 7 deletions src/WebSocket4Net/WebSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

namespace WebSocket4Net
{
public class WebSocket : EasyClient<WebSocketPackage>
public class WebSocket : EasyClient<WebSocketPackage>, IWebSocket
{
private static readonly Encoding _asciiEncoding = Encoding.ASCII;
private static readonly Encoding _utf8Encoding = new UTF8Encoding(false);
Expand Down Expand Up @@ -294,11 +294,6 @@ public ValueTask SendAsync(ref ReadOnlySequence<byte> sequence)
return SendAsync(_packageEncoder, package);
}

public async ValueTask CloseAsync(CloseReason closeReason)
{
await CloseAsync(closeReason, string.Empty);
}

private byte[] GetBuffer(int size)
{
return new byte[size];
Expand All @@ -309,7 +304,7 @@ public override async ValueTask CloseAsync()
await CloseAsync(CloseReason.NormalClosure, string.Empty);
}

public async ValueTask CloseAsync(CloseReason closeReason, string message)
public async ValueTask CloseAsync(CloseReason closeReason, string message = null)
{
var package = new WebSocketPackage();

Expand Down

0 comments on commit 8bbb61b

Please sign in to comment.