UISignalRClient.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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(ReceiveAllDataItems), ReceiveAllDataItems);
  40. _hubProxy.On<Dictionary<string, object>>(nameof(ReceiveChangedDataItems), ReceiveChangedDataItems);
  41. _hubConnection.Start().Wait();
  42. return true;
  43. }
  44. catch (Exception)
  45. {
  46. return false;
  47. }
  48. }
  49. private void ReceiveAllDataItems(Dictionary<string, object> dataItems)
  50. {
  51. if (dataItems.Count <= 0)
  52. {
  53. return;
  54. }
  55. _provider?.NotifyAllDataReceived(dataItems);
  56. }
  57. private void ReceiveChangedDataItems(Dictionary<string, object> dataItems)
  58. {
  59. if (dataItems.Count <= 0)
  60. {
  61. return;
  62. }
  63. foreach (var item in dataItems)
  64. {
  65. _changedDataItems.Post(item);
  66. }
  67. }
  68. private async void ProcessReceivedData()
  69. {
  70. while (await _changedDataItems.OutputAvailableAsync() && !_cancellationTokenSource.IsCancellationRequested)
  71. {
  72. var item = await _changedDataItems.ReceiveAsync();
  73. _provider?.NotifyDataChanged(item.Key, item.Value);
  74. }
  75. }
  76. private void OnStateChanged(StateChange obj)
  77. {
  78. //TODO:
  79. }
  80. protected virtual void Dispose(bool disposing)
  81. {
  82. if (!disposedValue)
  83. {
  84. if (disposing)
  85. {
  86. // TODO: dispose managed state (managed objects)
  87. if (_hubConnection is not null)
  88. {
  89. _hubConnection.StateChanged -= OnStateChanged;
  90. _hubConnection.Dispose();
  91. _changedDataItems.Complete();
  92. _cancellationTokenSource.Cancel();
  93. _cancellationTokenSource.Dispose();
  94. }
  95. }
  96. // TODO: free unmanaged resources (unmanaged objects) and override finalizer
  97. // TODO: set large fields to null
  98. disposedValue = true;
  99. }
  100. }
  101. // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
  102. // ~SignalRClient()
  103. // {
  104. // // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  105. // Dispose(disposing: false);
  106. // }
  107. public void Dispose()
  108. {
  109. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  110. Dispose(disposing: true);
  111. GC.SuppressFinalize(this);
  112. }
  113. }
  114. }