终端一体化运控平台
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.2 KiB

2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
2 jaren geleden
1234567891011121314151617181920212223242526272829303132333435363738394041
  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. }