gramjs icon indicating copy to clipboard operation
gramjs copied to clipboard

[Disconnecting...] multiple times??

Open ProductOfAmerica opened this issue 1 year ago • 2 comments

Why am I receiving multiple [Disconnecting...] messages?

 ✓ Compiled /api/test2 in 393ms (1696 modules)
[2024-05-27T02:36:51.853] [INFO] - [Running gramJS version 2.21.1]
[2024-05-27T02:36:51.856] [INFO] - [Connecting to 149.154.175.58:80/TCPFull...]
[2024-05-27T02:36:51.892] [INFO] - [Connection to 149.154.175.58:80/TCPFull complete!]
[2024-05-27T02:36:51.893] [INFO] - [Using LAYER 179 for initial connect]
disconnecting...
[2024-05-27T02:36:52.410] [WARN] - [Disconnecting...]
[2024-05-27T02:36:52.410] [INFO] - [Disconnecting from 149.154.175.58:80/TCPFull...]
destroying...
[2024-05-27T02:36:52.411] [WARN] - [Disconnecting...]
[2024-05-27T02:36:52.411] [INFO] - [Disconnecting from 149.154.175.58:80/TCPFull...]
 POST /api/test2 200 in 1548ms
[2024-05-27T02:36:52.413] [INFO] - [connection closed]

// HERE IT WAITS A FEW SECONDS BEFORE SENDING A THIRD [Disconnecting...] MESSAGE:

[2024-05-27T02:37:00.992] [WARN] - [Disconnecting...]
[2024-05-27T02:37:00.992] [INFO] - [Disconnecting from 149.154.175.58:80/TCPFull...]

Full code context:

import { type NextRequest, NextResponse } from 'next/server';
import { TelegramClient } from 'telegram';
import { StringSession } from 'telegram/sessions';
import { z } from 'zod';
import parsePhoneNumberFromString from 'libphonenumber-js';
import { createClient } from '@/lib/supabase/server';

const phoneValidationSchema = z.string().transform((phone) => {
   const parsedPhone = parsePhoneNumberFromString(phone, 'US');
   if (parsedPhone && parsedPhone.isValid()) {
      return parsedPhone.format('E.164');
   }
   throw new Error('Invalid phone number');
});

const apiId = Number(process.env.TELEGRAM_API_ID!);
const apiHash = process.env.TELEGRAM_API_HASH!;

export async function POST(req: NextRequest) {
   let client;
   try {
      const data = await req.json();
      const phoneNumber = data['phoneNumber'];
      const phoneCode = data['phoneCode'];
      const session = data['session'];

      if (!phoneNumber || !phoneCode || !session) throw new Error('Bad request');

      const supabase = createClient();

      const {
         data: { user },
      } = await supabase.auth.getUser();

      if (!user) {
         return NextResponse.json('Unauthorized', { status: 401 });
      }

      client = new TelegramClient(new StringSession(session), apiId, apiHash, {
         connectionRetries: 3,
      });

      await client.connect();
      if (await client.checkAuthorization()) {
         throw new Error('checkAuthorization() failed - session already logged in');
      }

      await client.signInUser(
         {
            apiId,
            apiHash,
         },
         {
            phoneNumber: async () => phoneValidationSchema.parse(phoneNumber),
            phoneCode: async () => phoneCode || '',
            onError: (err) => console.log(err),
         },
      );

      console.log(client.session.save());

      return NextResponse.json({}, { status: 200 });
   } catch (e: any) {
      console.error(e);
      return NextResponse.json(e?.message || {}, { status: 500 });
   } finally {
      if (client) {
         console.log('disconnecting...');
         await client.disconnect();
         console.log('destroying...');
         await client.destroy();
      }
   }
}

ProductOfAmerica avatar May 27 '24 07:05 ProductOfAmerica

Because you are calling disconnect and then destroy which also calls disconnect internally

vladrmnv avatar Oct 03 '24 03:10 vladrmnv

Because you are calling disconnect and then destroy which also calls disconnect internally

@vladrmnv That doesn’t explain this (look at my logs above):

// HERE IT WAITS A FEW SECONDS BEFORE SENDING A THIRD [Disconnecting...] MESSAGE:

[2024-05-27T02:37:00.992] [WARN] - [Disconnecting...]
[2024-05-27T02:37:00.992] [INFO] - [Disconnecting from 149.154.175.58:80/TCPFull...]

What you’re saying explains the first two… but not the third.

ProductOfAmerica avatar Oct 10 '24 15:10 ProductOfAmerica