简体   繁体   中英

Control when functions are called in ARI/Stasis applications

I am currently trying to control when stasis functions run, EG Click mute then mute the call. At the moment everything runs when a channel is added or removed but my for lope doesn't do anything at all.

I simply want to control when the code you will see below is called on the server side, without adding or removing a channel if that makes sense.

I am using express, ARI js client, JQuery and asterisk ARI.

Can anyone shed any light on how I might do this.

Simply need to run that function that is and mute all channels located in the array.

Server side code:

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);
}

The snippet that will not run/nothing happens:

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

}

And my client side 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)
    });

});

Turns out I can it quite simply like any normal function and control it.

For example.

Client Side

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

Server Side

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

Stasis function

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

}

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM