tdlight-team / tdlight-java

Complete Bot and Userbot Telegram library based on TDLib
https://t.me/TDLight
GNU Lesser General Public License v3.0
261 stars 43 forks source link

Client is not fully close when call client.sendClose() #207

Closed ShafiqSadat closed 7 months ago

ShafiqSadat commented 7 months ago

Client not fully close when call client.sendClose()

package org.example;

import it.tdlight.Init;
import it.tdlight.Log;
import it.tdlight.Slf4JLogMessageHandler;
import it.tdlight.client.*;
import it.tdlight.jni.TdApi;
import it.tdlight.jni.TdApi.AuthorizationState;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

public final class SessionChecker {

    public static void checkSession(int APP_ID, String APP_HASH, String sessionName) throws Exception {

        Init.init();

        Log.setLogMessageHandler(1, new Slf4JLogMessageHandler());

        try (SimpleTelegramClientFactory clientFactory = new SimpleTelegramClientFactory()) {
            APIToken apiToken = new APIToken(APP_ID, APP_HASH);
            TDLibSettings settings = TDLibSettings.create(apiToken);
            Path sessionPath = Paths.get(sessionName);
            settings.setDatabaseDirectoryPath(sessionPath.resolve("data"));
            settings.setDownloadedFilesDirectoryPath(sessionPath.resolve("downloads"));

            SimpleTelegramClientBuilder clientBuilder = clientFactory.builder(settings);
            ConsoleInteractiveAuthenticationData authenticationData = AuthenticationSupplier.consoleLogin();
            try (SimpleTelegramClient client = clientBuilder.build(authenticationData)) {
                client.addUpdateHandler(TdApi.UpdateAuthorizationState.class, (update) -> {
                    onUpdateAuthorizationState(update, client);
                });
                try {
                    TdApi.User me = client.getMeAsync().get(1, TimeUnit.MINUTES);
                    System.out.println(me.toString());
                } catch (Exception e) {
                    throw new RuntimeException(e);

                }
            }
        }

    }

    private static void onUpdateAuthorizationState(TdApi.UpdateAuthorizationState update, SimpleTelegramClient client) {
        AuthorizationState authorizationState = update.authorizationState;
        if (authorizationState instanceof TdApi.AuthorizationStateReady) {
            System.out.println("Logged in");
            client.sendClose();
        } else if (authorizationState instanceof TdApi.AuthorizationStateClosing) {
            System.out.println("Closing...");
        } else if (authorizationState instanceof TdApi.AuthorizationStateClosed) {
            System.out.println("Closed");
        } else if (authorizationState instanceof TdApi.AuthorizationStateLoggingOut) {
            System.out.println("Logging out...");
        } else if (authorizationState instanceof TdApi.AuthorizationStateWaitPhoneNumber) {
            System.out.println("WaitPhoneNumber");
            client.sendClose();
        }
    }
}

image

cavallium commented 7 months ago

Fixed in https://github.com/tdlight-team/tdlight-java/releases/tag/v3.4.0%2Btd.1.8.26