/**
* @param array $options
* @param LoopInterface $loop
* @throws \Exception
*/
function __construct(array $options, LoopInterface $loop = null, LoggerInterface $logger = null)
{
$this->options = $options;
$this->client = new Client($options['realm'], $loop);
/*
* Add the transport provider
* TODO: Allow for multiple transport providers
*/
$url = isset($options['url']) ? $options['url'] : null;
$pawlTransport = new PawlTransportProvider($url);
if ($logger) {
$pawlTransport->getManager()->setLogger($logger);
}
$this->client->addTransportProvider($pawlTransport);
$this->client->setReconnectOptions($options);
/*
* Authentication on challenge callback
*/
if (isset($options['onChallenge']) && is_callable($options['onChallenge']) && isset($options['authmethods']) && is_array($options['authmethods'])) {
$this->client->setAuthMethods($options['authmethods']);
$this->client->on('challenge', function (ClientSession $session, ChallengeMessage $msg) use($options) {
$token = $options['onChallenge']($session, $msg->getAuthMethod());
$session->sendMessage(new AuthenticateMessage($token));
});
}
if (isset($this->options['onClose']) && is_callable($this->options['onClose'])) {
$this->on('close', $this->options['onClose']);
}
/*
* Handle On Open event
*
*/
$this->client->on('open', function (ClientSession $session, TransportInterface $transport) {
$this->transport = $transport;
$this->emit('open', [$session]);
});
/*
* Handle On Close event
*/
$this->client->on('close', function ($reason) {
$this->emit('close', [$reason]);
});
$this->client->on('error', function ($reason) {
$this->emit('error', [$reason]);
});
}