Skip to content

Commit

Permalink
added cancellation token for websocket send async method
Browse files Browse the repository at this point in the history
  • Loading branch information
kerryjiang committed Apr 15, 2024
1 parent 6e33402 commit 4cea48c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/SuperSocket.WebSocket.Server/WebSocketPackageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public async ValueTask Handle(IAppSession session, WebSocketPackage package, Can

try
{
await websocketSession.SendAsync(package);
await websocketSession.SendAsync(package, cancellationToken);
}
catch (InvalidOperationException)
{
Expand All @@ -146,7 +146,7 @@ public async ValueTask Handle(IAppSession session, WebSocketPackage package, Can
else if (package.OpCode == OpCode.Ping)
{
package.OpCode = OpCode.Pong;
await websocketSession.SendAsync(package);
await websocketSession.SendAsync(package, cancellationToken);
return;
}
else if (package.OpCode == OpCode.Pong)
Expand Down
38 changes: 21 additions & 17 deletions src/SuperSocket.WebSocket.Server/WebSocketSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Buffers;
using System.Collections.Specialized;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using SuperSocket.ProtoBase;
using SuperSocket.Server;
Expand Down Expand Up @@ -33,30 +34,32 @@ public string Path

internal IPackageEncoder<WebSocketPackage> MessageEncoder { get; set; }

public virtual ValueTask SendAsync(WebSocketPackage message)
public virtual ValueTask SendAsync(WebSocketPackage message, CancellationToken cancellationToken = default)
{
return this.Connection.SendAsync(MessageEncoder, message);
return this.Connection.SendAsync(MessageEncoder, message, cancellationToken);
}

public virtual ValueTask SendAsync(string message)
public virtual ValueTask SendAsync(string message, CancellationToken cancellationToken = default)
{
return SendAsync(new WebSocketPackage
{
OpCode = OpCode.Text,
Message = message,
});
{
OpCode = OpCode.Text,
Message = message,
},
cancellationToken);
}

public virtual ValueTask SendAsync(ReadOnlyMemory<byte> data)
public virtual ValueTask SendAsync(ReadOnlyMemory<byte> data, CancellationToken cancellationToken = default)
{
return SendAsync(new WebSocketPackage
{
OpCode = OpCode.Binary,
Data = new ReadOnlySequence<byte>(data),
});
{
OpCode = OpCode.Binary,
Data = new ReadOnlySequence<byte>(data),
},
cancellationToken);
}

public ValueTask CloseAsync(CloseReason reason, string reasonText = null)
public ValueTask CloseAsync(CloseReason reason, string reasonText = null, CancellationToken cancellationToken = default)
{
var closeReasonCode = (short)reason;

Expand Down Expand Up @@ -90,10 +93,11 @@ public ValueTask CloseAsync(CloseReason reason, string reasonText = null)
OnCloseHandshakeStarted();

return SendAsync(new WebSocketPackage
{
OpCode = OpCode.Close,
Data = new ReadOnlySequence<byte>(buffer, 0, length)
});
{
OpCode = OpCode.Close,
Data = new ReadOnlySequence<byte>(buffer, 0, length)
},
cancellationToken);
}

private void OnCloseHandshakeStarted()
Expand Down

0 comments on commit 4cea48c

Please sign in to comment.