在ARI / Stasis应用程序中调用函数时进行控制

时间:2015-10-15 09:36:56

标签: javascript jquery node.js express asterisk

我正在尝试控制stasis函数何时运行,E.G单击静音然后将呼叫静音。目前,当添加或删除某个频道时,所有内容都会运行,但我的for lope根本不会执行任何操作。

我只想控制何时在服务器端调用您将在下面看到的代码,如果有意义的话,无需添加或删除频道。

我正在使用express,ARI js客户端,JQuery和asterisk ARI。

任何人都可以了解我如何做到这一点。

只需运行该功能即可静音数组中的所有通道。

服务器端代码:

var ari = require('ari-client');
var util = require('util');
var chanArr = [];
var chanMute = [];
var test;
var mute;
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);

//ARI client
ari.connect('http://localhost:8088', 'asterisk', 'asterisk', clientLoaded);

function clientLoaded(err, client) {
    if (err) {
        throw err;
    }
    // find or create a holding bridges
    var bridge = null;
    client.bridges.list(function (err, bridges) {
        if (err) {
            throw err;
        }

        bridge = bridges.filter(function (candidate) {
                return candidate.bridge_type === 'mixing';
            })[0];

        if (bridge) {
            console.log(util.format('Using bridge %s', bridge.id));
        } else {
            client.bridges.create({
                type : 'mixing'
            }, function (err, newBridge) {
                if (err) {
                    throw err;
                }

                bridge = newBridge;
                console.log(util.format('Created bridge %s', bridge.id));
            });
        }
    });

    // handler for StasisStart event
    function stasisStart(event, channel) {
        console.log(util.format(
                'Channel %s just entered our application, adding it to bridge %s',
                channel.name,
                bridge.id));

        channel.answer(function (err) {
            if (err) {
                throw err;
            }

            bridge.addChannel({
                channel : channel.id
            }, function (err) {
                var id = chanArr.push(channel.name)
                    console.log("Value: " + test);
                test = channel.name;
                updateSip();

                if (err) {
                    throw err;
                }

                //If else statement to start music for first user entering channel, music will stop once more than 1 enters the channel.
                if (chanArr.length <= 1) {
                    bridge.startMoh(function (err) {
                        if (err) {
                            throw err;
                        }
                    });
                } else if (chanArr.length === 2) {
                    bridge.stopMoh(function (err) {
                        if (err) {
                            throw err;
                        }
                    });
                } else {}

            });

        });

        for (i = 0; i < chanMute.length; i++) {
            text += chanMute[i];
            console.log("Ran.");
            channel.mute({
                channelId : chanMute
            },
                function (err) {
                console.log("didnt run");
            });

        }

        /*channel.hangup({
        channelId : channel.name
        },
        function (err) {});*/

    }

    // handler for StasisEnd event
    function stasisEnd(event, channel) {
        console.log(util.format(
                'Channel %s just left our application', channel.name));
        console.log(channel.name);

        var index = chanArr.indexOf(channel.name);
        chanArr.splice(index, 1);
        updateSip();
    }
    client.on('StasisStart', stasisStart);
    client.on('StasisEnd', stasisEnd);
    client.start('bridge-hold');
}

//Socket.io logic here
server.listen(3009, function () {
    console.log('listening on *:3009');
});

app.use(express.static(__dirname + '/public'));
app.get('/', function (req, res) {
    res.sendfile(__dirname + "/testPage.html");
});

io.sockets.on('connection', function (socket) {
    updateSip();
    socket.on('muting', function (data) {
        mute = data;
        chanMute.push(data);
        console.log("client side:" + chanMute);
    });
});

function updateSip() {
    console.log("Value: " + test);
    io.sockets.emit('sip', chanArr);
}

无法运行的片段/没有任何反应:

for (i = 0; i < chanMute.length; i++) {
    text += chanMute[i];
    console.log("Ran.");
    channel.mute({
        channelId : chanMute
      },
        function (err) {
          console.log("didnt run");
        });

}

我的客户端JQuery:

jQuery(function ($) {
    var socket = io.connect();
    console.log("scope" + socket);
    var mute = false;
    var $sip = $('#sip');
    var customerId;

    socket.on('sip', function (data) {
        var sip = '';
        $(".exe").remove();
        for (i = 0; i < data.length; i++) {
            sip += data[i];
            if (sip) {
                $sip.append('<tr class="exe">\
                                                        <td id="siptd">' + sip + '</td>\
                                                        <td><button class="btn btn-default mute" id="mute" type="submit">Mute</button></td>\
                                                        <td><button class="btn btn-default kick" id="kicks" data-toggle="modal" data-target="#myModal" type="submit">Kick</button></td>\
                                                        </tr>');
            } else {
                $sip.append('Currently no extensions');
            }
            sip = '';
        }

    });

    $('.kick').click(function () {
        $('#myInput').focus()
    });

    $(document).on('click', '#mute', function () {
        mute = $(this).closest('td').siblings(':first-child').text();
        socket.emit('muting', mute);
        console.log(mute)
    });

});

1 个答案:

答案 0 :(得分:0)

原来我可以完全像任何正常的功能一样控制它。

例如。

客户端

$(document).on('click', '.kick', function () {
        var hangup = $(this).closest('td').siblings(':first-child').text();
        socket.emit('hangup', hangup);
    });

服务器端

io.sockets.on('connection', function (socket) {
        updateSip();
        socket.on('hangup', function (data) {
            hangup(data);
        });
    });

停滞功能

function hangup(hangval) {
    console.log("Kicked:" + hangval);
    client.channels.hangup
    ({
        channelId : hangval
    },
        function (err) {
        if (err) {
            throw err;
        }
    });

}

希望这有帮助。