|
| 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.Net; |
| 7 | + |
| 8 | +namespace BootstrapBlazor.Server.Components.Samples.Sockets; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// 数据适配器示例 |
| 12 | +/// </summary> |
| 13 | +public partial class Adapters : IDisposable |
| 14 | +{ |
| 15 | + [Inject, NotNull] |
| 16 | + private ITcpSocketFactory? TcpSocketFactory { get; set; } |
| 17 | + |
| 18 | + private ITcpSocketClient _client = null!; |
| 19 | + |
| 20 | + private List<ConsoleMessageItem> _items = []; |
| 21 | + |
| 22 | + private readonly IPEndPoint _serverEndPoint = new(IPAddress.Loopback, 8900); |
| 23 | + |
| 24 | + private CancellationTokenSource _connectTokenSource = new(); |
| 25 | + private CancellationTokenSource _sendTokenSource = new(); |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// <inheritdoc/> |
| 29 | + /// </summary> |
| 30 | + protected override void OnInitialized() |
| 31 | + { |
| 32 | + base.OnInitialized(); |
| 33 | + |
| 34 | + // 从服务中获取 Socket 实例 |
| 35 | + _client = TcpSocketFactory.GetOrCreate("demo-adapter", options => |
| 36 | + { |
| 37 | + options.IsAutoReceive = false; |
| 38 | + options.LocalEndPoint = new IPEndPoint(IPAddress.Loopback, 0); |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + private async Task OnConnectAsync() |
| 43 | + { |
| 44 | + if (_client is { IsConnected: false }) |
| 45 | + { |
| 46 | + await _client.ConnectAsync(_serverEndPoint, _connectTokenSource.Token); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + private async Task SendAsync() |
| 51 | + { |
| 52 | + if (_client is { IsConnected: false }) |
| 53 | + { |
| 54 | + // 准备通讯数据 |
| 55 | + var data = new byte[1024]; |
| 56 | + await _client.SendAsync(data, _sendTokenSource.Token); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private async Task OnCloseAsync() |
| 61 | + { |
| 62 | + if (_client is { IsConnected: true }) |
| 63 | + { |
| 64 | + await _client.CloseAsync(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private Task OnClear() |
| 69 | + { |
| 70 | + _items = []; |
| 71 | + return Task.CompletedTask; |
| 72 | + } |
| 73 | + |
| 74 | + private async ValueTask OnReceivedAsync(ReadOnlyMemory<byte> data) |
| 75 | + { |
| 76 | + // 将数据显示为十六进制字符串 |
| 77 | + var payload = System.Text.Encoding.UTF8.GetString(data.Span); |
| 78 | + _items.Add(new ConsoleMessageItem |
| 79 | + { |
| 80 | + Message = $"接收到来自站点的数据为 {payload}" |
| 81 | + }); |
| 82 | + |
| 83 | + // 保持队列中最大数量为 50 |
| 84 | + if (_items.Count > 50) |
| 85 | + { |
| 86 | + _items.RemoveAt(0); |
| 87 | + } |
| 88 | + await InvokeAsync(StateHasChanged); |
| 89 | + } |
| 90 | + |
| 91 | + private void Dispose(bool disposing) |
| 92 | + { |
| 93 | + if (disposing) |
| 94 | + { |
| 95 | + if (_client is { IsConnected: true }) |
| 96 | + { |
| 97 | + |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// <inheritdoc/> |
| 104 | + /// </summary> |
| 105 | + public void Dispose() |
| 106 | + { |
| 107 | + Dispose(true); |
| 108 | + GC.SuppressFinalize(this); |
| 109 | + } |
| 110 | +} |
0 commit comments