UISignalRClient.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using Microsoft.AspNet.SignalR.Client;
  2. using System.Collections.Concurrent;
  3. using System.Threading;
  4. using System.Threading.Tasks.Dataflow;
  5. namespace Communicator
  6. {
  7. public class UISignalRClient : ICommunicator
  8. {
  9. private string _connectionUrl;
  10. private string _hubName;
  11. private HubConnection? _hubConnection;
  12. private IHubProxy? _hubProxy;
  13. private readonly BufferBlock<KeyValuePair<string, object>> _changedDataItems;
  14. private readonly CancellationTokenSource _cancellationTokenSource;
  15. private ICommunicatorProvider? _provider;
  16. private bool disposedValue;
  17. public UISignalRClient()
  18. {
  19. _connectionUrl = "http://localhost:9999/NewUI";
  20. _hubName = "UIHub";
  21. _changedDataItems = new();
  22. _cancellationTokenSource = new();
  23. Task processReceivedDataTask = new(ProcessReceivedData);
  24. processReceivedDataTask.Start();
  25. }
  26. public bool Initialize(ICommunicatorProvider provider)
  27. {
  28. ArgumentNullException.ThrowIfNull(provider, nameof(provider));
  29. _provider = provider;
  30. if (_hubConnection is not null)
  31. {
  32. return true;
  33. }
  34. try
  35. {
  36. _hubConnection = new HubConnection(_connectionUrl);
  37. _hubConnection.StateChanged += OnStateChanged;
  38. _hubProxy = _hubConnection.CreateHubProxy(_hubName);
  39. _hubProxy.On<Dictionary<string, object>>(nameof(ReceiveChangedDataItems), ReceiveChangedDataItems);
  40. _hubConnection.Start().Wait();
  41. return true;
  42. }
  43. catch (Exception)
  44. {
  45. return false;
  46. }
  47. }
  48. private void ReceiveChangedDataItems(Dictionary<string, object> dataItems)
  49. {
  50. if (dataItems.Count <= 0)
  51. {
  52. return;
  53. }
  54. foreach (var item in dataItems)
  55. {
  56. _changedDataItems.Post(item);
  57. }
  58. }
  59. private async void ProcessReceivedData()
  60. {
  61. while (await _changedDataItems.OutputAvailableAsync() && !_cancellationTokenSource.IsCancellationRequested)
  62. {
  63. var item = await _changedDataItems.ReceiveAsync();
  64. _provider?.DataChangedNotify(item.Key, item.Value);
  65. }
  66. }
  67. private void OnStateChanged(StateChange obj)
  68. {
  69. //TODO:
  70. }
  71. protected virtual void Dispose(bool disposing)
  72. {
  73. if (!disposedValue)
  74. {
  75. if (disposing)
  76. {
  77. // TODO: dispose managed state (managed objects)
  78. if (_hubConnection is not null)
  79. {
  80. _hubConnection.StateChanged -= OnStateChanged;
  81. _hubConnection.Dispose();
  82. _changedDataItems.Complete();
  83. _cancellationTokenSource.Cancel();
  84. _cancellationTokenSource.Dispose();
  85. }
  86. }
  87. // TODO: free unmanaged resources (unmanaged objects) and override finalizer
  88. // TODO: set large fields to null
  89. disposedValue = true;
  90. }
  91. }
  92. // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
  93. // ~SignalRClient()
  94. // {
  95. // // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  96. // Dispose(disposing: false);
  97. // }
  98. public void Dispose()
  99. {
  100. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  101. Dispose(disposing: true);
  102. GC.SuppressFinalize(this);
  103. }
  104. }
  105. }