<?php
namespace Teleguru\Client\Server;
use Socket\Socket;
/**
* Undocumented class
*/
class Application
{
protected $config;
protected $client;
protected $serverSocket;
public function __construct(array $config)
{
$this->config = \array_merge([
"app_id" => \getenv("APP_ID"),
"app_hash" => \getenv("APP_HASH"),
"server_port" => \getenv("SERVER_PORT"),
"server_max_connections" => \getenv("SERVER_MAX_CONNECTIONS")
], $config);
$this->init();
}
protected function init()
{
$this->client = new Client([
"app_id" => $this->config['app_id'],
"app_hash" => $this->config['app_hash']
]);
$this->serverSocket = new ServerSocket();
$this->serverSocket->setReusedPort(true)
->setMaxConnection($this->config['server_max_connections'])
->bind($this->config['server_port']);
$this->serverSocket->setConnectListener(new class($this->client) extends ConnectListener {
public function __invoke(Socket $socket)
{
// ..
$socket->close();
}
});
}
public function run()
{
$this->client->boot();
$this->serverSocket->listen()->run();
}
public function shutdown()
{
$this->serverSocket->close();
$this->client->close();
}
public function __destruct()
{
$this->shutdown();
}
}