我目前正试图控制何时停滞函数运行,例如单击静音然后静音呼叫。目前,当添加或删除通道时,一切都运行,但我的for lope根本不做任何事情。
我只是想控制你将在下面看到的代码何时在服务器端被调用,如果有意义的话,不需要添加或删除通道。
我使用express, ARI js客户端,JQuery和星号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)
});
});
事实证明,我可以像任何普通函数一样简单地控制它。
例如。
客户端$(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);
});
});
<<p> 瘀功能/strong> function hangup(hangval) {
console.log("Kicked:" + hangval);
client.channels.hangup
({
channelId : hangval
},
function (err) {
if (err) {
throw err;
}
});
}