rtckit/react-esl

异步 FreeSWITCH 事件套接字层 (ESL) 库

0.8.6 2022-03-31 23:21 UTC

This package is auto-updated.

Last update: 2024-08-29 05:13:19 UTC


README

Build Status Latest Stable Version Test Coverage Maintainability License

快速入门

FreeSWITCH 的事件套接字层是一个 TCP 控制接口,它使得开发复杂动态拨号计划/工作流程成为可能。您可以在 FreeSWITCH 网站上了解更多关于其 入站模式 以及其 出站模式 的信息。

此库建立在 ReactPHPRTCKit\ESL 的基础上,并提供了四个 ESL 元素(入站客户端和出站服务器,以及入站服务器和出站客户端)的类。前者是一对更常见的元素,它与 FreeSWITCH 集成以构建 RTC 应用程序。后者可以用来模拟 FreeSWITCH,例如在测试套件中,实现消息中继,安全研究等。方向术语(入站/出站)相对于 FreeSWITCH。


                         Inbound               Outbound

                 ┌──────────────────────┬─────────────────────┐
                 │                      │                     │
     Application │ InboundClient.php    │ OutboundServer.php  │
                 │                      │                     │
                 ├──────────────────────┼─────────────────────┤
                 │                      │                     │
     FreeSWITCH  │ InboundServer.php    │ OutboundClient.php  │
                 │                      │                     │
                 └──────────────────────┴─────────────────────┘

入站客户端示例

此使用模式指的是 FreeSWITCH 的入站模式(通常监听 TCP 8021),我们的应用程序作为客户端。典型的交互包括发出各种请求并等待传入事件。

/* Instantiate the object */
$client = new \RTCKit\React\ESL\InboundClient('127.0.0.1', 8021, 'ClueCon');
$client
    ->connect() /* Initiate the connection; the library handles the authentication process */
    ->then(function (\RTCKit\React\ESL\InboundClient $client) {
        /* At this point our connection is established and authenticated; we can fire up any requests */
        $request = new \RTCKit\ESL\Request\Api;
        $request->setParameters('switchname');

        return $client->api($request);
    })
    ->then(function (\RTCKit\ESL\Response $response) use ($client, $stdio) {
        $switchname = trim($response->getBody());

        echo 'Connected to ' . $switchname . PHP_EOL;

        /* Issue more requests here! */
    })
    ->otherwise(function (Throwable $t) {
        echo 'Something went wrong: ' . $t->getMessage() . PHP_EOL;
    });

出站服务器示例

在此模式下,当拨号计划调用 socket 应用程序时,FreeSWITCH(作为客户端)会连接到我们的应用程序(通常监听 TCP 8084)。

/* Instantiate the object */
$server = new \RTCKit\React\ESL\OutboundServer('127.0.0.1', 8084);
/* Configure the handler */
$server->on('connect', function (\RTCKit\React\ESL\RemoteOutboundClient $client, \RTCKit\ESL\Response\CommandReply $response) {
    /* The library already sent the `connect` request at our behalf.
     * $response holds initial response. */
    $vars = $response->getHeaders();
    echo 'Outbound connection from ' . $vars['core-uuid'] . PHP_EOL;
    echo 'Call UUID ' . $vars['channel-unique-id'] . PHP_EOL;

    /* Issue requests */
    $client->resume();
    $client->linger();
    $client->myEvents('json');
    $client->divertEvents('on');

    /* Listen to events */
    $client->on('event', function (\RTCKit\ESL\Response\TextEventJson $response): void {
        /* Consume the event here! */
    });

    /* Add your business logic here */
    /* ... */

    /* Disconnect client */
    $client->close();
});

最后,提供的 示例 是一个好的起点。

要求

RTCKit\React\ESL 与 PHP 7.4+ 兼容。

安装

您可以使用 Composer 将库作为项目依赖项添加。

composer require rtckit/react-esl

如果您仅在开发期间需要库,例如在您的测试套件中使用,那么您应该将其添加为仅开发依赖项。

composer require --dev rtckit/react-esl

测试

要运行测试套件,请克隆此存储库,然后通过 Composer 安装依赖项。

composer install

然后,转到项目根目录并运行

composer phpunit

静态分析

为了确保高代码质量,RTCKit\React\ESL 使用 PHPStanPsalm

composer phpstan
composer psalm

许可

MIT 许可,请参阅 LICENSE 文件

鸣谢

  • FreeSWITCH,FreeSWITCH 是 Anthony Minessale II 的注册商标

贡献

可以通过 问题跟踪器 提交错误报告(和小的补丁)。对于实质性的补丁,更喜欢从仓库分叉并提交拉取请求。