Skip to content

Instantly share code, notes, and snippets.

@davehorton
Last active June 9, 2019 07:10
Show Gist options
  • Save davehorton/c4d99329c43f8530c4e3f0ed95fe3189 to your computer and use it in GitHub Desktop.
Save davehorton/c4d99329c43f8530c4e3f0ed95fe3189 to your computer and use it in GitHub Desktop.
const calls = require('./calls');
srf.invite(async(req, res) => {
const ms = req.app.locals.ms;
const to_number = calls.convertToE164(req.calledNumber);
let ep, dlg;
try {
const {endpoint, dialog} = await ms.connectCaller(req, res);
ep = endpoint;
dlg = dialog;
const number = await calls.check_is_valid(to_number);
if (!number) {
console.log(`[${req.callid}] ${to_number} does not match`)
await calls.playback_failure(endpoint);
dialog.destroy();
endpoint.destroy();
}
else {
setHandlers(req, res, dialog, endpoint);
}
} catch (err) {
console.error(err, 'Error connecting call to media server');
res.send(503);
if (dlg) dlg.destroy();
if (ep) ep.destroy();
}
});
function setHandlers(req, res, dialog, endpoint) {
dialog.on('destroy', () => {
console.log(`Received BYE, deleting ${req.callid}`)
endpoint.destroy();
// calls.delete_call(req.call_id);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment