2016-06-24 2 views
2

SignalR 클라이언트 연결이 끊어지면 클라이언트가 다시 연결하려고합니다. 이것을 구현하기위한 좋은 패턴입니까? 나는 연결을 다시 시작하여 SignalR 연결에서 Closed 이벤트를 처리하는 것에 대해 언급하고 있습니다.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(); 
    } 
} 

답변

1

SignalR에는 자체 재 연결 메커니즘이 있습니다. 하지만 재시도 중 일부는 연결이 끊긴 상태로 바뀝니다. Disconnected/Closed 상태는 신호기가 재접속을 시도 하였음을 의미 하나 실제로는 도달 할 수 없습니다. 따라서 다시 연결하여 계속 다시 연결하는 것이 좋습니다.

단점이 있습니다. 모바일에서는 배터리를 다시 사용합니다.

자세한 내용은 here을 확인하십시오.