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