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