【问题标题】:asterisk events in codeignitercodeigniter 中的星号事件
【发布时间】:2017-11-04 07:31:46
【问题描述】:

我正在使用 Codeigniter 框架 (php-jquery) 开发一个联系人管理系统,并且我正在使用 Asterisk(11 或更高版本)来管理呼叫。

在这个系统中,有来电和去电。在拨出电话的情况下,我想通过单击一个按钮给某人打电话,在来电的情况下,我想在发生这种情况时将它们显示为通知弹出窗口。为此,我正在编写一个库类,如下所示,它只管理传出呼叫。

class Asterisk {
public $server;
public $port;
public $socket;
public $error;
const NOSOCKET      = 'No Socket Defined';
const CONNECTFAILED = 'Connection Failed';
const AUTHFAIL      = 'Authentication Failed';
const NORESP        = 'Server Didn\'t Respond';

public function __construct($params = array()) {

    $this->server = $_SERVER['HTTP_HOST'];
    $this->port = 5038;

    if (isset($params['server']))
        $this->server = $params['server'];
    if (isset($params['port'])) {
        $this->port = $params['port'];
    }
}

private function _check() {
    if ($this->socket)
        return true;

    $this->error = Asterisk::NOSOCKET;
    redirect('');
}

private function _command($query, $expect = null, $error = null) {
    $this->_check();

    fputs($this->socket, $query."\r\n");
    $response = fgets($this->socket);

    if (!$response) {
        $this->error = Asterisk::NORESP;
        return false;
    }

    if ($expect == null)
        return true;

    if (strpos($response, $expect) != false)
        return true;

    $this->error = $error;
    return false;
}

/* ************************************************************************************************************** */

public function connect($server = null, $port = null) {
    if ($this->socket)
        $this->close();

    if ($server != null && $port != null) {
        $this->server = $server;
        $this->port = $port;
    }

    $this->socket = fsockopen($this->server, $this->port, $errno, $errstr, 1);
    if (!$this->socket) {
        $this->error =  Asterisk::CONNECTFAILED . " - $errstr ($errno)";
        return false;
    }

    stream_set_timeout($this->socket, 3);
    return true;
}

public function close() {
    $this->_check();

    fclose($this->socket);
    return true;
}

public function login($username, $password) {
    return $this->_command(
        "Action: Login\r\n".
        "UserName: $username\r\n".
        "Secret: $password\r\n".
        "Events: off\r\n",
        "Message: Authentication accepted",
        Asterisk::AUTHFAIL
    );
}

public function logout() {
    return $this->_command(
        "Action: Logoff\r\n"
    );
}

/* ************************************************************************************************************** */

public function call($channel, $context, $extension, $callerId, $priority = 1, $async = true, $timeout = 30000) {
    return $this->_command(
        "Action: Originate\r\n".
        "Channel: $channel\r\n".
        "Context: $context\r\n".
        "Exten: $extension\r\n".
        "Priority: $priority\r\n".
        "Async: $async\r\n".
        "CallerId: $callerId\r\n".
        "TimeOut: $timeout\r\n"
    );
}
}

如何更新此类以处理来电及其事件?

我知道有一些现有的库,例如 PAMI,但它不能与 codeigniter 和 mvc 模型一起正常工作。

如何管理这些来电?有人可以发布一些示例代码吗?

谢谢。

【问题讨论】:

    标签: php codeigniter event-handling asterisk


    【解决方案1】:

    我已经在一些实现中做到了这一点,使用 PHP 和 Laravel 以及 PAMI(以及用于引导自定义 AGI 交互的 PAGI)。

    简而言之:AMI 事件流需要主动侦听一系列事件并解析流中的数据。为了处理流,我总是创建一个小的单独程序(在 PHP 和 PAGI 中),它处理事件流并通过 WebSocket 将事件发送回主机应用程序(在两端使用 HOA Websocket)。

    采用这种做法的原因是:

    • 允许小型轻量级程序完成侦听所有 AMI 事件的工作。
    • 将“实时”系统与 Web 应用程序分离,并以与 Web 应用程序习惯被交谈的方式相称的方式与 Web 应用程序对话
    • 取消阻止使用来自 AMI 的事件流,因为您只需要查看事件,并通过套接字连接发送消息而不确认响应。
    • 使用出色的库(如 PAMI)来处理 AMI 并共享自定义 DTO 库,以便将 AMI 原始信息映射到特定于应用程序的 DTO 只发生在 AMI 消费者程序上,而我的其他应用程序不必这样做了解 AMI。
    • 最重要的是:PHP 作为 Web 应用程序的运行时配置很可能与旨在使用实时事件流的程序的配置有很大不同。如果您错过了一个活动,您将不会再获得它!

    一旦设置了 AMI 网关程序,该程序接收事件并将它们传递给主应用程序,您就可以使用 AMI 网关程序向 Asterisk(操作)发送指令,并且您已经有了处理响应的方法从行动开始。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-21
      • 1970-01-01
      • 2014-05-27
      相关资源
      最近更新 更多