终端一体化运控平台
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

42 řádky
1.2 KiB

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