您好我正在尝试使用Node.JS获取星号中的对等状态。我在github上经历了几个API,发现了这个:
节点文件:
var io = require('socket.io').listen(app);
var AsteriskAmi = require('asterisk-ami');
var express= require('express');
var http=require('http');
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var ami = new AsteriskAmi( { port : '5038', host: 'host', username: 'uid', password: 'pwd' } );
app.get('/', function(req, res){
//send the index.html file for all requests
res.sendFile(__dirname + '/index.html');
});
http.listen(3001, function(){
console.log('listening on *:3001');
});
ami.on('ami_data', function(data){
console.log('AMI DATA', data);
io.sockets.emit('ami_data', data);
});
ami.connect(function(response){
console.log('connected to the AMI');
setInterval( function() {
ami.send({action: 'SIPpeerstatus'});
}, 10000);
});
process.on('SIGINT', function () {
ami.disconnect();
process.exit(0);
});
HTML文件:
<html>
<head></head>
<body>
<table border="1px" bgcolor="#FAFAD2">
<tr>
<th> Peer Id<pre id="ami_data"></pre></th>
<th> Peer Status<pre id="ami_data2"></pre></th>
</tr>
</table>
<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>
var socket = io();
socket.on('ami_data', function (data){
var str = document.getElementById("ami_data").innerHTML += JSON.stringify(data.peer) + "<br>";
var str = document.getElementById("ami_data2").innerHTML += JSON.stringify(data.peerstatus) + "<br>";
});
</script>
</body>
</html>
我想创建一个html页面,当我点击按钮时,它应该只显示该时刻的对等状态,而不是连续发送状态。