使用 ARI 监视与桥梁相关的事件

Watch for bridge-related events using ARI

我正在尝试使用 Asterisk ARI 来监视与桥相关的事件。我正在使用 Asterisk 13.6.0.

具体来说,我想知道桥何时被创建或销毁,以及用户(频道)何时加入或离开桥。在我的服务器上,桥是在有人拨入时动态创建的,并在最后一个成员离开桥时自动销毁。

使用 Asterisk 项目中的 node-ari-client 库,并遵循他们的一些示例代码,这就是我目前所拥有的。

var client = require("ari-client");
var util = require("util");

client.connect("http://localhost:8088", "username", "password")

.then(function (ari) {

    ari.once("StatisStart", channelJoined);

    function channelJoined (event, incoming) {

        incoming.on("BridgeCreated", function(event, bridge) {
            console.log(util.format("Bridge created: %s", bridge.id));
        });

        incoming.on("BridgeDestroyed", function(event, bridge) {
            console.log(util.format("Bridge destroyed: %s", bridge.id));
        });

        incoming.on("ChannelEnteredBridge", function(event, channel) {
            console.log(util.format("Bridge was joined by: %s", channel.id));
        });

        incoming.on("ChannelLeftBridge", function(event, channel) {
            console.log(util.format("Bridge was joined by: %s", channel.id));
        });
    }

    ari.start("bridge-watcher");
})

.done();

我预计 .on() 处理程序会在各种事件发生时打印到控制台。然而,调用一座桥,离开一座桥,没有任何东西被打印到控制台。

如果重要,这里是 npm ls 的输出,显示了我正在使用的版本。节点是 v0.10.36.

├─┬ ari-client@0.5.0
│ ├── backoff-func@0.1.2
│ ├── bluebird@2.9.34
│ ├── node-uuid@1.4.1
│ ├─┬ swagger-client@2.0.26
│ │ ├── btoa@1.1.1
│ │ └─┬ shred@0.8.10
│ │   ├── ax@0.1.8
│ │   ├── cookiejar@1.3.1
│ │   ├── iconv-lite@0.2.11
│ │   └── sprintf@0.1.1
│ ├── underscore@1.6.0
│ └─┬ ws@0.4.31
│   ├── commander@0.6.1
│   ├── nan@0.3.2
│   ├── options@0.0.5
│   └── tinycolor@0.0.1
├── bluebird@3.1.1
└─┬ util@0.10.3
  └── inherits@2.0.1

Specifically, I want to know when a bridge has been created or destroyed, and when a user (channel) has joined or left the bridge. On my server, bridges are created dynamically when someone dials in, and destroyed automatically when the last member leaves the bridge.

记住:ARI 的主要 目的是 build your own dialplan applications, not to monitor the entirety of Asterisk. As such, by default, your external application is not subscribed to the resources in Asterisk. As the Channels in a Stasis Application 部分说明:

Resources in Asterisk do not, by default, send events about themselves to a connected ARI application. In order to get events about resources, one of three things must occur:

  1. The resource must be a channel that entered into a Stasis dialplan application. A subscription is implicitly created in this case. The subscription is implicitly destroyed when the channel leaves the Stasis dialplan application.

  2. While a channel is in a Stasis dialplan application, the channel may interact with other resources - such as a bridge. While channels interact with the resource, a subscription is made to that resource. When no more channels in a Stasis dialplan application are interacting with the resource, the implicit subscription is destroyed.

  3. At any time, an ARI application may make a subscription to a resource in Asterisk through application operations. While that resource exists, the ARI application owns the subscription.

如果您希望自动获取 Asterisk 中通道在 bridge-watcher 应用程序之外使用的资源的事件,除非您执行以下两种操作之一,否则您将无法获取它们:

  1. 使用 applications resource 显式订阅资源。这适用于相对静态 and/or 长期存在的资源,例如端点、静态桥(例如用于会议的桥)、邮箱和设备状态。它不适用于临时资源。

  2. 在 Asterisk 13.6.0 及更高版本中,您现在可以在连接 WebSocket 时订阅所有事件源。在 node-ari-client 中,您将执行以下操作:

    ari.start(bridge-watcher, 真);

但是您应该注意,即使您订阅了所有资源,您也不会明确地拥有它们。您只能自动观看它们。所有权的概念在 ARI 中非常重要,特别是因为它涉及您可以和不能对渠道做什么以及何时。我链接的维基页面提供了一些关于其工作原理的合理文档。