Skip to content

Commit

Permalink
added node example that shows how to log start and end of calls
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCK committed May 12, 2015
1 parent 62ca9b0 commit 6a9b159
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions examples/nodejs/plain/log_calls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// example that show how to get sipgate.io to also push the end of the call

var http = require('http');
var queryString = require('querystring');

// requires xmlbuilder: npm install xmlbuilder
var builder = require('xmlbuilder');

http.createServer(function (req, res) {
if (req.method == 'POST') {
var data = '';

req.on('data', function(currentData) {
data += currentData;
});

req.on('end', function () {
var post = queryString.parse(data);

if (post['event'] == 'newCall') {
console.log('New call with id ' + post['callId'] + ' started.');

var response = builder.create('Response',{version: '1.0', encoding: 'UTF-8'},{});
response.att('onHangup', 'http://' + req.headers.host + '/');

var body = response.toString();

res.writeHead(200, {
'Content-Length': body.length,
'Content-Type': 'application/xml'
});

res.write(body);
res.end();
}
else if (post['event'] == 'hangup' ) {
console.log('Call with id ' + post['callId'] + ' ended.');

res.writeHead(200);
res.end();
}
});
}

}).listen(3000);

0 comments on commit 6a9b159

Please sign in to comment.