2016-09-17 75 views
1

我正在使用TLSharp庫實現自定義電報客戶端。當我運行下面的代碼:TLSharp數據中心(dc)異常

public async Task<string> SendCodeRequest(string phoneNumber) 
    { 
     var completed = false; 

     TL.AuthSendCodeRequest request = null; 

     while (!completed) 
     { 
      request = new TL.AuthSendCodeRequest(phoneNumber, 5, _apiId, _apiHash, "en"); 
      await _sender.Send(request); 
      await _sender.Receive(request); 

      completed = true; 
     } 

     // TODO handle other types (such as SMS) 
     if (request.Result is TL.AuthSentCodeType) 
     { 
      var result = (TL.AuthSentCodeType)request.Result; 
      return result.PhoneCodeHash; 
     } 
     else 
     { 
      var result = (TL.AuthSentAppCodeType)request.Result; 
      return result.PhoneCodeHash; 
     } 

    } 

我給了我以下異常:註冊{} dcIdx直流

您的電話號碼。請更新設置。 請參閱https://github.com/sochix/TLSharp#i-get-an-error-migrate_x對於 的詳細信息。

提到的github頁面說TLSharp自己處理這些異常。所以我猜這個庫核心有些問題,因爲代碼本身不會產生異常來解決數據中心IP問題。

任何幫助,將不勝感激。

回答

0

TlSharp當前不處理此異常,您必須捕獲異常並獲取數據中心號然後嘗試使用ReconnectToDc()函數重新連接到數據中心。

MtProtoSender.cs文件,你可以找到下面的代碼行產生異常:

throw new InvalidOperationException($"Your phone number registered to {dcIdx} dc. Please update settings. See https://github.com/sochix/TLSharp#i-get-an-error-migrate_x for details."); 

用下面的代碼替換它,這樣所產生的異常具有所需的數據中心號碼連接到它。

InvalidOperationException exception = new InvalidOperationException($"Your phone number registered to {dcIdx} dc. Please update settings. See https://github.com/sochix/TLSharp#i-get-an-error-migrate_x for details."); 
exception.Data.Add("dcId", dcIdx); 
throw exception; 

更改你的代碼是這樣的:

while (!completed) 
{ 
    request = new TL.AuthSendCodeRequest(phoneNumber, 5, _apiId, _apiHash, "en"); 
    try 
    { 
     await _sender.Send(request); 
     await _sender.Receive(request); 

     completed = true; 
    } 
    catch (InvalidOperationException ex) 
    { 
     if (ex.Message.StartsWith("Your phone number registered to") && ex.Data["dcId"] != null) 
     { 
      await ReconnectToDc((int)ex.Data["dcId"]); 
     } 
     else 
     { 
      throw; 
     } 
    } 
} 

在上面附着到異常數據中心數量的代碼被用於重新連接到數據中心。

0

可能您的電話號碼不符合電報接受的格式。

電話號碼必須以加號開頭,國家代碼和電話號碼無間隙,例如:+989333333333