Skip to content

Commit 4e78aba

Browse files
committed
feat: 增加 SendAsync 扩展方法
1 parent d32f100 commit 4e78aba

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License
3+
// See the LICENSE file in the project root for more information.
4+
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
5+
6+
using System.Text;
7+
8+
namespace BootstrapBlazor.Components;
9+
10+
/// <summary>
11+
/// <see cref="ITcpSocketClient"/> 扩展方法类
12+
/// </summary>
13+
public static class ITcpSocketClientExtensions
14+
{
15+
/// <summary>
16+
/// Sends the specified string content to the connected TCP socket client asynchronously.
17+
/// </summary>
18+
/// <remarks>This method converts the provided string content into a byte array using the specified
19+
/// encoding (or UTF-8 by default) and sends it to the connected TCP socket client. Ensure the client is connected
20+
/// before calling this method.</remarks>
21+
/// <param name="client">The TCP socket client to which the content will be sent. Cannot be <see langword="null"/>.</param>
22+
/// <param name="content">The string content to send. Cannot be <see langword="null"/> or empty.</param>
23+
/// <param name="encoding">The character encoding to use for converting the string content to bytes. If <see langword="null"/>, UTF-8
24+
/// encoding is used by default.</param>
25+
/// <param name="token">A <see cref="CancellationToken"/> to observe while waiting for the operation to complete.</param>
26+
/// <returns>A <see cref="ValueTask{TResult}"/> that represents the asynchronous operation. The result is <see
27+
/// langword="true"/> if the content was sent successfully; otherwise, <see langword="false"/>.</returns>
28+
public static ValueTask<bool> SendAsync(this ITcpSocketClient client, string content, Encoding? encoding = null, CancellationToken token = default)
29+
{
30+
var buffer = encoding?.GetBytes(content) ?? Encoding.UTF8.GetBytes(content);
31+
return client.SendAsync(buffer, token);
32+
}
33+
}

0 commit comments

Comments
 (0)