|
- using Microsoft.AspNetCore.SignalR.Client;
-
- namespace BPASmartClient.HubHelper
- {
- /// <summary>
- /// 客户端
- /// </summary>
- public class HubHelper
- {
- private volatile static HubHelper _Instance;
- public static HubHelper GetInstance => _Instance ?? (_Instance = new HubHelper());
- private HubHelper() { }
-
- public Action<object> Report { get; set; }
- public Action<object> Upstreamrequest { get; set; }
-
- HubConnection hubConnection;
-
- public void Connect(string ip, int port)
- {
- hubConnection = new HubConnectionBuilder().WithAutomaticReconnect().WithUrl($"http://{ip}:{port}/personhub").Build();//连接
-
- hubConnection.On<object>("Report", (s) => { Report?.Invoke(s); });//客户端注册方法
-
- hubConnection.On<object>("Upstreamrequest", (s) => { Upstreamrequest?.Invoke(s); });//客户端注册方法
- try
- {
- hubConnection.StartAsync();
- }
- catch (Exception ex)
- {
- throw;
- }
- }
-
- public void SendMessage(string info)
- {
- hubConnection.SendAsync("Send", info);
- }
-
- }
- }
|