2015-06-02 80 views
0

我試圖完成從控制器方法發送數據到我的客戶端方法,該方法在實例客戶端斷開連接時返回響應。信號R - 處理集線器外部的斷開連接

控制器 目前..

public void SomeMethod(){ 
    IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<MyHub>(); 

    while(true){ 
     hubContext.Clients.All.addNameValue(name,value); 
    } 
} 

我想..

bool someProperty = false 
public string SomeMethod(){ 
    IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<MyHub>(); 

    while(true & (someProperty == false)){ 
     hubContext.Clients.All.addNameValue(name,value); 
    } 
    return "done"; 
} 

因爲我們知道從輪轂overrided方法的客戶端斷開..

中心

public override Task OnDisconnected(bool stopCalled = true) 
    { 
     Console.WriteLine("Hub Disconnected: {0}\n", Context.ConnectionId); 
     return (base.OnDisconnected(stopCalled)); 
    } 

是否有任何方式從集線器外部跟蹤控制器實例,以便這樣的事情可以完成?

public override Task OnDisconnected(bool stopCalled = true) 
    { 
     SomeClass.someProperty = true; 
     Console.WriteLine("Hub OnDisconnected {0}\n", Context.ConnectionId); 
     return (base.OnDisconnected(stopCalled)); 
    } 

我們尋求利用信號R,當我們想流數據來回打開一個連接的能力,但是要知道,當客戶端斷開連接,這樣客戶端可以繼續做的事情的能力。

回答

0

如果您創建一個使用單例類來跟蹤客戶端的集線器,那麼如果需要,您可以存儲所有連接的客戶端,以便在服務器重新聯機時能夠重新連接。客戶端上確實沒有斷開事件,只有重新連接的事件。所以很難確定服務器是否已斷開連接。我解決這個問題的方法是訂閱connection.closed事件。對我來說,我嘗試重新連接,但失敗了,我通知用戶並退出應用程序。關於跟蹤客戶的單例方法的信息,我從這個鏈接開發了我的:http://www.asp.net/signalr/overview/getting-started/tutorial-server-broadcast-with-signalr

 tcHubProxy(HubConnection hubConnection, StreamWriter writer) 
    { 
     mhubConn = hubConnection; 
     mIHubProxy = hubConnection.CreateHubProxy("CentralHub"); 
     mWriter = writer; 
     mWriter.AutoFlush = true; 

     try 
     { 
      mhubConn.TraceLevel = TraceLevels.All; 
      mhubConn.TraceWriter = mWriter; 
      mhubConn.Closed += MhubConnOnClosed; 
      mhubConn.Start().Wait(); 
      LogEvent(DateTime.Now, "Info", "Client Connected"); 
      mIHubProxy.On<List<User>>("updateFmds", GetUserFmds); 
      connected = true; 
     } 
     catch (Exception e) 
     { 
      if (showMessage) 
      { 
       MessageBox.Show(
        "There is currently no connection to the server, this application will close, if this issue persists check your network connection or contact support.", 
        "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       showMessage = false; 
      } 
      connected = false; 
      showMessage = false; 
     } 
    } 

public void MhubConnOnClosed() 
    { 
     LogEvent(DateTime.Now, "Info", "Client connection to communication hub, attempting to redirect."); 
     try 
     { 
      mhubConn = new HubConnection(ConnectionString); 
      mhubConn.TraceLevel = TraceLevels.All; 
      mhubConn.TraceWriter = mWriter; 
      mIHubProxy = mhubConn.CreateHubProxy("CentralHub"); 
      mhubConn.Start().Wait(); 
      LogEvent(DateTime.Now, "Info", "Reconnection successful."); 
      mIHubProxy.On<List<User>>("updateFmds", GetUserFmds); 
     } 
     catch (Exception e) 
     { 
      LogEvent(DateTime.Now, "Error", "Reconnection attempt failed.", e.StackTrace, e.Message); 
      if (showMessage) 
      { 
       MessageBox.Show(
        "There is currently no connection to the server, this application will close, if this issue persists check your network connection or contact support.", 
        "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       showMessage = false; 
      } 
      connected = false; 
     } 
    }