如果我的SignalR客户端连接中断,我希望客户端尝试重新连接。使用关闭事件来重新启动连接是否是实现此功能的好方法?
public class OnPremiseWebHubClient
{
private HubConnection _hubConnection;
private IHubProxy _hubProxy;
private OnPremiseWebHubClient() { }
static OnPremiseWebHubClient() { }
private static readonly OnPremiseWebHubClient _instance = new OnPremiseWebHubClient();
public static OnPremiseWebHubClient Instance { get { return _instance; } }
public async Task Start()
{
_hubConnection = new HubConnection("http://OnPremiseWeb/");
_hubProxy = _hubConnection.CreateHubProxy("OnPremiseHub");
// IS THIS A GOOD PATTERN FOR KEEPING THE CONNECTION ALIVE?
_hubConnection.Closed += async () =>
{
// reconnect if we close
await _hubConnection.Start();
};
await _hubConnection.Start();
}
}