我正在使用Laravel开发PBX监视应用程序。在PBX端,Asterisk正在管理呼叫。
我可以使用PAMI客户端来接收来自Asterisk Manager Interface的所有事件。如果仅从控制台运行此脚本,则Asterisk上的所有事件都会实时显示在屏幕上:
<?php
require __DIR__ . '/vendor/autoload.php';
use PAMI\Client\Impl\ClientImpl as PamiClient;
use PAMI\Message\Event\EventMessage;
use PAMI\Listener\IEventListener;
$pamiClientOptions = array(
'host' => '',
'scheme' => 'tcp://',
'port' => 5038,
'username' => '',
'secret' => '',
'connect_timeout' => 10000,
'read_timeout' => 10000
);
$pamiClient = new PamiClient($pamiClientOptions);
// Open the connection
$pamiClient->open();
$pamiClient->registerEventListener(function (EventMessage $event) {
var_dump($event);
});
$running = true;
// Main loop
while($running) {
$pamiClient->process();
usleep(1000);
}
// Close the connection
$pamiClient->close();
我想做的是在Laravel中使用PAMI实现事件和监听器。但这似乎不起作用。
我已经在EventServiceProvider
类上注册了事件和侦听器:
protected $listen = [
'App\Events\AmiEventOccurred' => [
'App\Listeners\LogAmiEvent',
],
我的活动课:
<?php
namespace App\Events;
use Illuminate\Queue\SerializesModels;
use Illuminate\Foundation\Events\Dispatchable;
class AmiEventOccurred
{
use Dispatchable, SerializesModels;
public $message;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct()
{
$pamiClientOptions = array(
'host' => getenv('PAMI_HOST'),
'scheme' => getenv('PAMI_SCHEME'),
'port' => getenv('PAMI_PORT'),
'username' => getenv('PAMI_USERNAME'),
'secret' => getenv('PAMI_SECRET'),
'connect_timeout' => getenv('PAMI_CONNECT_TIMEOUT'),
'read_timeout' => getenv('PAMI_READ_TIMEOUT'),
);
$pamiClient = new \PAMI\Client\Impl\ClientImpl($pamiClientOptions);
// Open the connection
$pamiClient->open();
$pamiClient->registerEventListener(function (\PAMI\Message\Event\EventMessage $message) {
$this->message = $message;
});
$running = true;
// Main loop
while($running) {
$pamiClient->process();
usleep(1000);
}
// Close the connection
$pamiClient->close();
}
}
我的听众课:
<?php
namespace App\Listeners;
use App\Events\AmiEventOccurred;
class LogAmiEvent
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param AmiEventOccurred $event
* @return void
*/
public function handle(AmiEventOccurred $event)
{
Log::debug(print_r($event, true));
}
}
这是正确的方法吗?是否可以通过Laravel事件主动地实时侦听其他服务,还是我应该开发另一个应用程序使其在后台实时运行并在必要时触发Laravel事件?
答案 0 :(得分:1)
所有这些代码都不属于事件。一个事件只是为了宣布某件事已经发生,并包含有关那件事的所有重要信息。您用于运行客户端和使其休眠的代码应位于其他位置,这可能是内核不断运行的artisan命令。然后在该代码中,当发生某些情况时,您会触发一条简单事件,并带有一条消息,供任何侦听器操作:
$pamiClient->registerEventListener(function (\PAMI\Message\Event\EventMessage $message) {
new AmiMessageReceived($message);
});