UISignalRClient.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 readonly ConcurrentDictionary<string,List<Action<object>>> _subscribedKeys;
  16. private bool disposedValue;
  17. public UISignalRClient()
  18. {
  19. _connectionUrl = "http://localhost:9999/NewUI";
  20. _hubName = "UIHub";
  21. _changedDataItems = new BufferBlock<KeyValuePair<string, object>>();
  22. _cancellationTokenSource = new CancellationTokenSource();
  23. _subscribedKeys = new ConcurrentDictionary<string, List<Action<object>>>();
  24. Task.Run(() =>
  25. {
  26. ProcessReceivedData();
  27. });
  28. }
  29. public bool Initialize()
  30. {
  31. try
  32. {
  33. if (_hubConnection is not null)
  34. {
  35. return true;
  36. }
  37. _hubConnection = new HubConnection(_connectionUrl);
  38. _hubConnection.StateChanged += OnStateChanged;
  39. _hubProxy = _hubConnection.CreateHubProxy(_hubName);
  40. _hubProxy.On<Dictionary<string, object>>("ReceiveChangedDataItems", items =>
  41. {
  42. if(items.Count<=0)
  43. {
  44. return;
  45. }
  46. foreach (var item in items)
  47. {
  48. _changedDataItems.Post(item);
  49. }
  50. });
  51. return true;
  52. }
  53. catch (Exception)
  54. {
  55. return false;
  56. }
  57. }
  58. public void SubscribeDataItem(string dataKey, Action<object> callback)
  59. {
  60. if(!_subscribedKeys.ContainsKey(dataKey))
  61. {
  62. _subscribedKeys.TryAdd(dataKey, new List<Action<object>>());
  63. }
  64. _subscribedKeys[dataKey].Add(callback);
  65. }
  66. public void UnsubscribeDataItem(string dataKey, Action<object> callback)
  67. {
  68. if( _subscribedKeys.ContainsKey(dataKey))
  69. {
  70. _subscribedKeys[dataKey].Remove(callback);
  71. }
  72. }
  73. private async void ProcessReceivedData()
  74. {
  75. while (await _changedDataItems.OutputAvailableAsync() && !_cancellationTokenSource.IsCancellationRequested)
  76. {
  77. var item = await _changedDataItems.ReceiveAsync();
  78. if (!_subscribedKeys.ContainsKey(item.Key))
  79. {
  80. continue;
  81. }
  82. foreach (var action in _subscribedKeys[item.Key])
  83. {
  84. action?.Invoke(item.Value);
  85. }
  86. }
  87. }
  88. private void OnStateChanged(StateChange obj)
  89. {
  90. //TODO:
  91. }
  92. protected virtual void Dispose(bool disposing)
  93. {
  94. if (!disposedValue)
  95. {
  96. if (disposing)
  97. {
  98. // TODO: dispose managed state (managed objects)
  99. if (_hubConnection is not null)
  100. {
  101. _hubConnection.StateChanged -= OnStateChanged;
  102. _hubConnection.Dispose();
  103. _changedDataItems.Complete();
  104. _cancellationTokenSource.Cancel();
  105. _cancellationTokenSource.Dispose();
  106. }
  107. }
  108. // TODO: free unmanaged resources (unmanaged objects) and override finalizer
  109. // TODO: set large fields to null
  110. disposedValue = true;
  111. }
  112. }
  113. // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
  114. // ~SignalRClient()
  115. // {
  116. // // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  117. // Dispose(disposing: false);
  118. // }
  119. public void Dispose()
  120. {
  121. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  122. Dispose(disposing: true);
  123. GC.SuppressFinalize(this);
  124. }
  125. }
  126. }