registerEventListener() public method

Registers the given listener so it can receive events. Returns the generated id for this new listener. You can pass in a an IEventListener, a Closure, and an array containing the object and name of the method to invoke. Can specify an optional predicate to invoke before calling the callback.
public registerEventListener ( mixed $listener, Closure | null $predicate = null ) : string
$listener mixed
$predicate Closure | null
return string
示例#1
0
文件: example.php 项目: alesf/PAMI
            echo 'Unrecognized encoding - printing message in this encoding :  ';
            $event->getContentEncoding();
            echo '\\n Message:  ';
            $event->getContent();
        }
    }
}
////////////////////////////////////////////////////////////////////////////////
// Code STARTS.
////////////////////////////////////////////////////////////////////////////////
error_reporting(E_ALL);
ini_set('display_errors', 1);
try {
    $options = array('host' => $argv[1], 'port' => $argv[2], 'username' => $argv[3], 'secret' => $argv[4], 'connect_timeout' => 60, 'read_timeout' => 60);
    $a = new ClientImpl($options);
    $a->registerEventListener(new A());
    $a->open();
    // SMS
    $sms = new VGSMSMSTxAction();
    $sms->setContentType('text/plain; charset=ASCII');
    $msg = $argv[5];
    $phone = $argv[6];
    $sms->setContent($msg);
    $sms->setTo($phone);
    // SMS multipart MSG - This is used to send 1 big message splitted in several parts, up to 255 messages
    if ($argv[7] == 1) {
        $sms->setConcatRefId('58');
        $sms->setConcatTotalMsg('2');
        $sms->setConcatSeqNum('1');
        $a->send($sms);
        $sms->setContent('---Testing Multipart message ');
示例#2
0
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $pamiClientOptions = array('log4php.properties' => $input->getOption('log4php-configuration'), 'host' => $input->getOption('ami-host'), 'scheme' => 'tcp://', 'port' => $input->getOption('ami-port'), 'username' => $input->getOption('ami-username'), 'secret' => $input->getOption('ami-password'), 'connect_timeout' => $input->getOption('ami-connect-timeout'), 'read_timeout' => $input->getOption('ami-read-timeout'));
     $pamiClient = new ClientImpl($pamiClientOptions);
     $output->write("<comment>Opening asterisk connection... </comment>");
     $pamiClient->open();
     $output->writeln('<info>done</info>');
     $output->write("<comment>Opening rabbitmq connection... </comment>");
     $amqpConn = new AMQPConnection($input->getOption('rabbit-host'), $input->getOption('rabbit-port'), $input->getOption('rabbit-username'), $input->getOption('rabbit-password'), $input->getOption('rabbit-vhost'));
     $ch = $amqpConn->channel();
     $exchange = $input->getOption('rabbit-exchange-name');
     $ch->exchange_declare($exchange, 'fanout', false, true, false);
     $output->writeln('<info>done</info>');
     $i = 0;
     $counter = 0;
     $pamiClient->registerEventListener(function (EventMessage $event) use($output, $ch, $exchange, &$counter, &$i) {
         // Send to RabbitMQ
         $msg = new AMQPMessage(json_encode($event->getKeys()), array('content_type' => 'application/json', 'timestamp' => time(), 'delivery_mode' => 2));
         $ch->basic_publish($msg, $exchange);
         gc_collect_cycles();
         $output->writeln(sprintf(" >> <comment>[%s] [%s bytes]</comment> <info>%s</info>", date('Y-m-d G:i:s') . substr((string) microtime(), 1, 8), memory_get_usage(), $event->getName()));
         $counter = 0;
         $i = 0;
     });
     $closer = function ($autoExit = true) use($pamiClient, $amqpConn, $ch, $output) {
         $output->writeln('');
         $output->write('<comment>Closing ami connection... </comment>');
         $pamiClient->close();
         // send logoff and close the connection.
         $output->writeln("<info>done</info>");
         $output->write('<comment>Closing rabbitmq connection... </comment>');
         $ch->close();
         $amqpConn->close();
         $output->writeln("<info>done</info>");
         if ($autoExit) {
             exit(0);
         }
     };
     declare (ticks=1) {
         pcntl_signal(\SIGINT, $closer);
         pcntl_signal(\SIGTERM, $closer);
         while (true) {
             try {
                 usleep(1000);
                 // 1ms delay
                 $i++;
                 if ($i == 10000) {
                     // show some feedback every 10000 iterations or so, roughly every 10 seconds if nothing is processed
                     $counter++;
                     $output->writeln(sprintf(" >> <comment>Waiting for events... [%d seconds]</comment> <info>Ping...</info>", $counter * 10));
                     $i = 0;
                     // for every 10 seconds that go by, send a ping/pong event to the asterisk server
                     // if send times out, it'll throw an exception, which will end this script... supervisor should restart
                     /** @var ResponseMessage $pong  */
                     $pong = $pamiClient->send(new \PAMI\Message\Action\PingAction());
                     if ('Success' == $pong->getKey('response')) {
                         // Send the pong event onto RabbitMQ.  This has a faux keep-alive effect on the RabbitMQ connection
                         $msg = new AMQPMessage(json_encode($pong->getKeys()), array('content_type' => 'application/json', 'timestamp' => time(), 'delivery_mode' => 2));
                         $ch->basic_publish($msg, $exchange);
                         $output->writeln(sprintf(" >> <comment>[%s] [%s bytes]</comment> <info>%s</info>", date('Y-m-d G:i:s') . substr((string) microtime(), 1, 8), memory_get_usage(), 'Pong'));
                     }
                 }
                 $pamiClient->process();
             } catch (\Exception $e) {
                 // try to close any connections
                 $closer(false);
                 // rethrow the exception
                 throw $e;
             }
         }
     }
     $closer(false);
     return 0;
 }
$pamiClient->registerEventListener(function (EventMessage $event) {
    if ($event instanceof PAMI\Message\Event\BridgeEvent) {
        // $listeners[ext] = [jid][jid][jid]
        global $listeners;
        global $users;
        $channel1 = $event->getChannel1();
        $channel2 = $event->getChannel2();
        echo "Calls now {$channel1}~{$channel2}:> \n";
        if ($listeners) {
            if (isset($listeners[bare_ext($channel1)])) {
                if (is_array($listeners[bare_ext($channel1)])) {
                    foreach ($listeners[bare_ext($channel1)] as $listener) {
                        echo "listener {$listener} got for channel {$channel1}. Bridge \n";
                        $response = json_encode(array('Action' => 'BridgeEvent', 'Success' => 'True', 'Channel' => $channel2, 'Extension' => bare_ext($channel2)));
                        sendMessage($listener, $response);
                    }
                }
            }
            if (isset($listeners[bare_ext($channel2)])) {
                if (is_array($listeners[bare_ext($channel2)])) {
                    foreach ($listeners[bare_ext($channel2)] as $listener) {
                        echo "listener {$listener} got for channel {$channel2}. Bridge \n";
                        $response = json_encode(array('Action' => 'BridgeEvent', 'Success' => 'True', 'Channel' => $channel1, 'Extension' => bare_ext($channel1)));
                        sendMessage($listener, $response);
                    }
                }
            }
        }
        if ($users != null) {
            $from_jid = array_search(bare_ext($channel1), $users);
            if ($from_jid != null) {
                $response = json_encode(array('Action' => 'BridgeEvent', 'Success' => 'True', 'Channel' => $channel2, 'Extension' => bare_ext($channel2)));
                sendMessage($from_jid, $response);
            }
            $with_jid = array_search(bare_ext($channel2), $users);
            if ($with_jid != null) {
                $response = json_encode(array('Action' => 'BridgeEvent', 'Success' => 'True', 'Channel' => $channel1, 'Extension' => bare_ext($channel2)));
                sendMessage($with_jid, $response);
            }
        }
        // var_dump($users);
    }
    if ($event instanceof PAMI\Message\Event\HangupEvent) {
        global $users;
        global $listeners;
        $channel = $event->getChannel();
        echo "Hangup channel {$channel}";
        if (isset($listeners[bare_ext($channel)])) {
            if (is_array($listeners[bare_ext($channel)])) {
                foreach ($listeners[bare_ext($channel)] as $listener) {
                    echo "listener {$listener} got for channel {$channel}. Hangup \n";
                    $response = json_encode(array('Action' => 'BridgeEvent', 'Success' => 'True', 'Channel' => $channel));
                    sendMessage($listener, $response);
                }
            }
        }
        if ($users) {
            $jid = array_search(bare_ext($channel), $users);
            if ($jid != null) {
                $response = json_encode(array('Action' => 'HangupEvent', 'Success' => 'True', 'Channel' => $channel));
                sendMessage($jid, $response);
            }
        }
    }
    if ($event instanceof PAMI\Message\Event\DialEvent) {
        global $users;
        global $db;
        global $astdb;
        $destination = bare_ext($event->getDestination());
        $channel = $event->getChannel();
        $from = bare_ext($channel);
        // strange bug?
        if ($from != $destination && $destination != "") {
            $sub_event = $event->getSubEvent();
            $from_caller_id = $event->getCallerIDName();
            $from_jid = $astdb->getJid($from);
            echo "\nDial event -> \n";
            echo "from {$from} to {$destination}\n";
            echo "still alive \n";
            try {
                if ($sub_event && $destination && $users) {
                    if ($sub_event == "Begin") {
                        $jid = array_search($destination, $users);
                        if ($jid != null) {
                            $response = json_encode(array('Action' => 'IncomingCallEvent', 'Success' => 'True', 'From' => $from_caller_id, 'FromJid' => $from_jid, 'FromExt' => $from, 'Channel' => $event->getDestination()));
                            sendMessage($jid, $response);
                        }
                    }
                }
            } catch (Exception $e) {
                echo "Excepition in DialEvent: {$e}\n";
            }
        }
    }
});
示例#4
0
 /**
  * Opens the connection to ami. Also calls _load if it has to.
  *
  * @return void
  */
 public function open()
 {
     if (!$this->_init) {
         $this->_load();
     }
     $this->_ami->registerEventListener($this);
     $this->_ami->open();
 }