终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HubHelper.cs 1.1 KiB

2 years ago
2 years ago
2 years ago
2 years ago
12345678910111213141516171819202122232425262728293031323334353637383940
  1. using Microsoft.AspNetCore.SignalR.Client;
  2. namespace BPASmartClient.HubHelper
  3. {
  4. public class HubHelper
  5. {
  6. private volatile static HubHelper _Instance;
  7. public static HubHelper GetInstance => _Instance ?? (_Instance = new HubHelper());
  8. private HubHelper() { }
  9. public Action<object> Report { get; set; }
  10. public Action<object> Upstreamrequest { get; set; }
  11. HubConnection hubConnection;
  12. public void Connect(string ip, int port)
  13. {
  14. hubConnection = new HubConnectionBuilder().WithAutomaticReconnect().WithUrl($"http://{ip}:{port}/personhub").Build();//连接
  15. hubConnection.On<object>("Report", (s) => { Report?.Invoke(s); });
  16. hubConnection.On<object>("Upstreamrequest", (s) => { Upstreamrequest?.Invoke(s); });
  17. try
  18. {
  19. hubConnection.StartAsync();
  20. }
  21. catch (Exception ex)
  22. {
  23. throw;
  24. }
  25. }
  26. public void SendMessage(string info)
  27. {
  28. hubConnection.SendAsync("Send", info);
  29. }
  30. }
  31. }