Created
August 24, 2019 15:06
-
-
Save davehorton/31444a5aac69dc7f265473a0fee27d7e to your computer and use it in GitHub Desktop.
handling hangup on A when outdialing B and creating uas/uac dialogs indepently
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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