Skip to content

Instantly share code, notes, and snippets.

@davehorton
Created August 24, 2019 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davehorton/94f8301594fc67e7efa04388534c75a7 to your computer and use it in GitHub Desktop.
Save davehorton/94f8301594fc67e7efa04388534c75a7 to your computer and use it in GitHub Desktop.
handling hangup on A when outdialing B and creating uas/uac dialogs indepently
const SipError = require('drachtio-srf').SipError;
srf.invite(async(req, res) => {
let uas, uac, ep;
try {
let invite; // will be used to track invite we send on B leg
// connect A leg
const {endpoint, dialog} = await ms.connectCaller(req, res);
uas = dialog;
ep = endpoint;
// temp listener: if A leg hangs up during outdial, cancel B
uas.on('destroy', () => {
console.log('caller hung up during outdial');
if (invite) invite.cancel();
endpoint.destroy();
});
await endpoint.play(`test.wav`);
endpoint.execute('endless_playback', 'tone_stream://%(400,200,400,425);%(400,2000,400,425)')
.then(() => console.log(`Finished playing back ringing`));
uac = await srf.createUAC(`sip:${config.to_number}@${config.outbound_sbc}`,
{
localSdp: b_endpoint.local.sdp
},
{
cbRequest: (err, req) => invite = req,
});
// remove temporary listener during outdial
uas.removeAllListeners('destroy');
// since we didnt use createB2BUA, point dialogs to each other
uas.other = uac;
uac.other = uas;
// set the usual handlers
[uas, uac].forEach((dlg) => {
dlg.on('destroy', () => {
dlg.other.destroy();
endpoint.destroy();
});
});
} catch (err) {
if (err instanceof SipError) {
if (err.status === 487) {
// caller hung up and we canceled B, no need to log
}
else {
console.log(`outdial failed with status ${err.status}`);
}
}
else {
console.log(`unexpected error: ${err}`);
}
// need to explicitly hang up A since we didn't use createB2BUA
if (uas) uas.destroy();
if (ep) ep.destroy();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment