Skip to content

Commit

Permalink
Multiple session files support
Browse files Browse the repository at this point in the history
  • Loading branch information
xtrime-ru committed Mar 8, 2019
1 parent 7e41c52 commit 2f92fa6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
16 changes: 8 additions & 8 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 14 additions & 6 deletions server.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,31 @@
throw new \Exception('Start in CLI');
}

$shortopts = 'a::p::';
$shortopts = 'a::p::s::';
$longopts = [
'address::', // ip адресс сервера, необязательное значение
'port::', // порт сервера, необязательное значение
'session::', //префикс session файла
'help', //нужна ли справка?
];
$options = getopt($shortopts, $longopts);
$options = [
'address' => $options['address'] ?? $options['a'] ?? '',
'port' => $options['port'] ?? $options['p'] ?? '',
'help' => isset($options['help']),
'address' => $options['address'] ?? $options['a'] ?? '',
'port' => $options['port'] ?? $options['p'] ?? '',
'session' => $options['session'] ?? $options['s'] ?? '',
'help' => isset($options['help']),
];

if ($options['help']) {
$help = 'Fast, simple, async php telegram parser: MadelineProto + Swoole Server
usage: php server.php [--help] [-a|--address=127.0.0.1] [-p|--port=9503]
usage: php server.php [--help] [-a|--address=127.0.0.1] [-p|--port=9503] [-s|--session=]
Options:
--help Show this message
-a --address Server ip (optional) (example: 127.0.0.1)
-p --port Server port (optional) (example: 9503)
-s --session Prefix for session file (optional) (example: xtrime)
Also all options can be set in .env file (see .env.example)
Expand All @@ -38,6 +41,11 @@
echo $help;
exit;
}
if ($options['session']) {
$sessionFile = "{$root}/{$options['session']}_session.madeline";
} else {
$sessionFile = "{$root}/session.madeline";
}

$client = new \TelegramSwooleClient\Client($root);
$client = new \TelegramSwooleClient\Client($sessionFile);
new TelegramSwooleClient\Server($client, $options);
21 changes: 12 additions & 9 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,31 @@
class Client {

public $MadelineProto;

private $sessionFile;
/**
* Client constructor.
*/
public function __construct($root)
public function __construct($sessionFile)
{
$sessionFile = $root . '/session.madeline';

$config = Config::getInstance()->get('telegram');
$this->config = Config::getInstance()->get('telegram');

if (empty($config['connection_settings']['all']['proxy_extra']['address'])) {
unset($config['connection_settings']);
if (empty($this->config['connection_settings']['all']['proxy_extra']['address'])) {
unset($this->config['connection_settings']);
}

$this->sessionFile = $sessionFile;
$this->connect();

}

public function connect(){
//При каждой инициализации настройки обновляются из массива $config
echo PHP_EOL . 'Starting telegram client ...' . PHP_EOL;
$time = microtime(true);
$this->MadelineProto = new MadelineProto\API($sessionFile, $config);
$this->MadelineProto = new MadelineProto\API($this->sessionFile, $this->config);
$this->MadelineProto->start();
$time = round(microtime(true) - $time, 3);
echo PHP_EOL . "Client started: $time sec" . PHP_EOL;

}

/**
Expand Down

0 comments on commit 2f92fa6

Please sign in to comment.