class AsteriskDaemon { private $asterisk; private $memcache; public function __construct() { $this->asterisk = new ClientImpl([ ... ]); $memcache = new Memcached; $memcache->connect('127.0.0.1', '11211'); $this->memcache = $memcache; } public function start() { $asterisk = $this->asterisk; $loop = Factory::create(); // add periodic timer $loop->addPeriodicTimer(1, function () use (&$asterisk) { $pid = \pcntl_fork(); if ($pid < 0) { // exit; }elseif ($pid) { // , \pcntl_waitpid($pid, $status, WUNTRACED); if ($status > 0) { // , $asterisk->close(); usleep(1000); $asterisk->open(); } return; } else { // try { $asterisk->process(); exit(0); } catch (\Exception $e) { exit(1); } } }); // $loop->addPeriodicTimer(30, function () { while (($pid = \pcntl_waitpid(0, $status, WNOHANG)) > 0) { echo "process exit. pid:" . $pid . ". exit code:" . $status . "\n"; } }); $loop->run(); } }
... $this->asterisk->registerEventListener(new AsteriskEventListener($memcache), function (EventMessage $event) { // return $event instanceof BridgeEvent; }); $this->asterisk->open(); ...
class AsteriskEventListener implements IEventListener { private $memcache; private $bridges = []; public function __construct($memcache) { $this->memcache = $memcache; } private function addBridge($phone1, $phone2) { $bFind = false; if ($this->bridges) { foreach ($this->bridges as $bridge) { if (in_array($phone1, $bridge) && in_array($phone2, $bridge)) { $bFind = true; } } } if (!$bFind) { $this->bridges[] = [ $phone1, $phone2 ]; $bFind = true; } return $bFind; } private function deleteBridge($phone1, $phone2 = null) { if ($this->bridges) { foreach ($this->bridges as $key => $bridge) { if (in_array($phone1, $bridge) && (!$phone2 || ($phone2 && in_array($phone2, $bridge)))) { unset($this->bridges[$key]); } } } } public function handle(EventMessage $event) { // , / if ($event instanceof BridgeEvent) { $this->bridges = $this->memcache->getKey('asterisk-bridges'); $state = $event->getBridgeState(); $caller1 = $event->getCallerID1(); $caller2 = $event->getCallerID2(); if ($state == 'Link') { // $this->addBridge($caller1, $caller2); } else { // $this->deleteBridge($caller1, $caller2); } $this->memcache->setKey('asterisk-bridges', $this->bridges); } } }
... // try { $message = $asterisk->send(new CoreShowChannelsAction()); $events = $message->getEvents(); $this->parse($events); $asterisk->process(); exit(0); } catch (\Exception $e) { exit(1); } ...
private function parse($events) { foreach ($events as $event) { if ($event instanceof CoreShowChannelEvent) { $caller1 = $event->getKey('CallerIDnum'); $caller2 = $event->getKey('ConnectedLineNum'); $this->bridges = $this->memcache->getKey('asterisk-bridges'); $this->addBridge($caller1, $caller2); $this->memcache->setKey('asterisk-bridges', $this->bridges); } } }
... $this->asterisk->registerEventListener(new AsteriskEventListener(), function (EventMessage $event) { return $event instanceof HangupEvent; }); $this->asterisk->open(); ...
... public function handle(EventMessage $event) { if ($event instanceof HangupEvent) { $this->bridges = $this->memcache->getKey('asterisk-bridges'); $caller1 = $event->getKey('CallerIDNum'); $caller2 = $event->getKey('ConnectedLineNum'); $this->deleteBridge($caller1); $this->deleteBridge($caller2); $this->memcache->setKey('asterisk-bridges', $this->bridges); } } ...
(function ($) { $.getCall = function () { if (localStorage.callTitle !== undefined && localStorage.callSuccess === undefined) { var notification, title = localStorage.callTitle, options = { body: localStorage.callText, icon: localStorage.callImage }, eventNotification = function () { window.open(localStorage.callUrl); }; if (!('Notification' in window)) { console.error('This browser does not support desktop notification'); } else if (Notification.permission === 'granted') { notification = new Notification(title, options); notification.onclick = eventNotification; } else if (Notification.permission !== 'denied') { Notification.requestPermission(function (permission) { if (permission === 'granted') { notification = new Notification(title, options); notification.onclick = eventNotification; } }); } localStorage.callSuccess = true; } }; // wormhole().on('master', function () { var es = new EventSource('/check-call'); es.addEventListener('message', function (res) { var data = JSON.parse(res.data); if (data['id']) { localStorage.callTitle = data['title']; localStorage.callText = data['text']; localStorage.callImage = data['img']; localStorage.callUrl = data['url']; } else { delete localStorage.callTitle; delete localStorage.callText; delete localStorage.callImage; delete localStorage.callUrl; delete localStorage.callSuccess; } }); }); })(jQuery); setInterval(function () { $.getCall(); }, 1000);
public function checkCall() { header('Content-Type: text/event-stream'); header('Cache-Control: no-cache'); header('Access-Control-Allow-Origin: *'); // $managerPhone = $_SESSION['phone']; $user = null; $clientPhone = $this->getPhone($managerPhone); if ($clientPhone) { $user = User::find()->where(['phone' => $clientPhone])->one(); } if ($user) { // echo "retry: 30000\n"; } else { echo "retry: 3000\n"; } echo 'id: ' . $managerPhone . "\n"; $data = []; if ($user) { $data = [ 'id' => $user['id'], 'title' => ' ' . $user['name'], 'text' => ' ', 'img' => '/phone.png', 'url' => '/user/' . $user['id'] ]; } echo "data: " . json_encode($data) . "\n\n"; } // public function getPhone($managerPhone) { $memcache = new Memcached; $memcache->addServer('127.0.0.1', '11211'); $extPhone = ''; if (!$managerPhone) { return $extPhone; } $bridges = $memcache->getKey('asterisk-bridges'); if (!isset($bridges) || !is_array($bridges)) { return $extPhone; } foreach ($bridges as $bridge) { if (($key = array_search($managerPhone, $bridge)) !== false) { $extPhone = $bridge[!$key]; break; } } return $extPhone; }
Source: https://habr.com/ru/post/278859/
All Articles