2015-10-15 54 views
0

我目前正試圖控制停滯功能運行的時間,E.G單擊靜音然後靜音。目前,當一個頻道被添加或刪除時,所有內容都會運行,但是我的lope完全沒有做任何事情。在ARI/Stasis應用程序中調用函數時的控制

我只是想控制下面將會看到的代碼在服務器端調用,而不需要添加或刪除通道。

我使用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) 
    }); 

}); 

回答

0

匝我可以uite只是像任何正常的功能和控制它。

例如。

客戶端

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

} 

希望這有助於。

相關問題