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); } } }