【问题标题】:Java Telegram API RpcCall TimeoutJava Telegram API RpcCall 超时
【发布时间】:2014-11-10 15:55:25
【问题描述】:

我有一个关于 Telegram API 的问题
每当我执行 RpcCall 时,它总是给我一个 TimeoutException,除非我执行 NonAuth Call。

我已经可以使用数字登录,并且我在 AbsApiState 中将 Authenticated 设置为 true,但仍然只能进行 NonAuth 调用

这是我的代码:

private void startApi() throws Exception
{
    api = new TelegramApi(new MyApiStorage(Moin.config.getProp("useTest").equalsIgnoreCase("true") ? true : false),
            new AppInfo(Moin.api_id, "console", "???", "???", "en"),
            new ApiCallback()
    {

        @Override
        public void onAuthCancelled(TelegramApi arg0) 
        {
            System.out.println("AuthCancelled");
        }

        @Override
        public void onUpdate(TLAbsUpdates update) 
        {
            System.out.println("Updated | " + update.getClass());
        }

        @Override
        public void onUpdatesInvalidated(TelegramApi arg0) 
        {
            System.out.println("Updatefailed");
        }

    });

    TLConfig config = null;
    config = api.doRpcCallNonAuth(new TLRequestHelpGetConfig());

    if(config != null)
        api.getState().updateSettings(config);
    else
        throw new Exception("config is null, could not update DC List");
    login();
}


private void login() throws IOException
{
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        TLSentCode code = null;
        String defaultNumber = Moin.config.getProp("phoneNumber");
        System.out.println("Enter a Phone Number (Default ist " + defaultNumber + "):");
        String number = reader.readLine();
        if(number.equals(" "))
            number = defaultNumber;

        System.out.println("Sending to " + number + " ...");
        try 
        {
            code = api.doRpcCallNonAuth(new TLRequestAuthSendCode(number, 0, Moin.api_id, Moin.api_hash, "en"));
        }
        catch (RpcException e) 
        {
            if (e.getErrorCode() == 303) 
            {
                int destDC = 0;
                if (e.getErrorTag().startsWith("NETWORK_MIGRATE_")) 
                {
                    destDC = Integer.parseInt(e.getErrorTag().substring("NETWORK_MIGRATE_".length()));
                } 
                else if (e.getErrorTag().startsWith("PHONE_MIGRATE_")) 
                {
                    destDC = Integer.parseInt(e.getErrorTag().substring("PHONE_MIGRATE_".length()));
                }
                else if (e.getErrorTag().startsWith("USER_MIGRATE_")) 
                {
                    destDC = Integer.parseInt(e.getErrorTag().substring("USER_MIGRATE_".length()));
                }
                else 
                {
                    e.printStackTrace();
                }
                api.switchToDc(destDC);
                code = api.doRpcCallNonAuth(new TLRequestAuthSendCode(number, 0, Moin.api_id, Moin.api_hash, "en"));
            } 
            else
                e.printStackTrace();
        }

        String hash = code.getPhoneCodeHash();

        System.out.println("Please Enter the Code:");
        String smsCode = reader.readLine();

        TLAuthorization auth = api.doRpcCallNonAuth(new TLRequestAuthSignIn(number, hash, smsCode));
        api.getState().setAuthenticated(api.getState().getPrimaryDc(), true);


        //This is where I get the Error
        TLExportedAuthorization test = api.doRpcCall(new TLRequestAuthExportAuthorization(api.getState().getPrimaryDc()));
        System.out.println(test.getId());


        FileOutputStream stream = new FileOutputStream(Paths.get("").toAbsolutePath().toString() + File.separator + "test.txt");
        try 
        {
            stream.write(test.getBytes().getData());
        } 
        finally 
        {
            stream.close();
        }

        TLState state = api.doRpcCall(new TLRequestUpdatesGetState());
        System.out.println(state.getDate() + "  |  " + state.getPts() + "  |  " + state.getQts() + "  |  " + state.getUnreadCount());




        TLAbsUser user = auth.getUser();

}

这里是错误:

TelegramApi#1001:Timeout Iteration
TelegramApi#1001:RPC #3: Timeout (14999 ms)
TelegramApi#1001:Timeout Iteration
org.telegram.api.engine.TimeoutException
        at org.telegram.api.engine.TelegramApi.doRpcCall(TelegramApi.java:364)
        at org.telegram.api.engine.TelegramApi.doRpcCall(TelegramApi.java:309)
        at org.telegram.api.engine.TelegramApi.doRpcCall(TelegramApi.java:400)
        at org.telegram.api.engine.TelegramApi.doRpcCall(TelegramApi.java:396)
        at at.nonon.telegram.telegram.Telegram.login(Telegram.java:165)
        at at.nonon.telegram.telegram.Telegram.startApi(Telegram.java:105)
        at at.nonon.telegram.telegram.Telegram.<init>(Telegram.java:54)
        at at.nonon.telegram.telegram.ApiManager.startNew(ApiManager.java:21)
        at at.nonon.telegram.Moin.onApiStart(Moin.java:31)

我从电报机器人 (https://github.com/ex3ndr/telegram-bot) 中获取了很多代码,但即使我复制粘贴他的代码,它仍然无法正常工作...

提前致谢

【问题讨论】:

  • 这里有同样的问题。

标签: java timeout telegram


【解决方案1】:

这个问题也发生在我身上。我假设您使用的是 debian 或其他一些 linux 机器。问题是 Oracle JDK 使用 linux 随机数生成器。

为了让它工作,像这样运行你的应用程序:

java -Djava.security.egd=file:/dev/./urandom -jar foo.jar

... 意思是,您必须指定 java.security.egd 参数。

详情请见:https://github.com/ex3ndr/telegram-api/issues/9#issuecomment-38175765 和这里: http://www.virtualzone.de/2011/10/javas-securerandomgenerateseed-on-linux.html

【讨论】:

    【解决方案2】:

    在我更改 MemoryApiState 中的服务器 IP 地址后,它开始为我工作,如下所示

     public void start(boolean isTest) {
        connections = new HashMap<>();
        connections.put(1, new ConnectionInfo[]{
                new ConnectionInfo(1, 0, isTest ? "149.154.175.10" : "149.154.175.50", 443),
    
        });
    }
    

    【讨论】:

      【解决方案3】:

      您的超时可能有多种原因 - 请查看我对
      TimeoutException on telegram java client

      的回复

      【讨论】:

      • 如果你觉得你已经回答了这个问题,最好将问题标记为重复。
      • @PetterFriberg - 这个问题并不是真正的重复,因为他们在问不同的事情,但我认为根本问题是相同的。这就是为什么我在这里放一个链接。我已经使用这个网站很长时间了,但从未发布过 - 所以我可能不太清楚规则
      猜你喜欢
      • 1970-01-01
      • 2021-08-30
      • 2023-03-28
      • 1970-01-01
      • 2015-03-25
      • 1970-01-01
      • 2018-01-19
      • 2018-04-07
      • 1970-01-01
      相关资源
      最近更新 更多