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