Skip to content

Instantly share code, notes, and snippets.

@WhiteyDude
Created August 24, 2019 11:22
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 WhiteyDude/c88d77e2a3706d98abad8136d237b8d7 to your computer and use it in GitHub Desktop.
Save WhiteyDude/c88d77e2a3706d98abad8136d237b8d7 to your computer and use it in GitHub Desktop.
const Srf = require('drachtio-srf');
const Mrf = require('drachtio-fsmrf');
const config = require('config');
const srf = new Srf() ;
const mrf = new Mrf(srf) ;
// Connect to drachtio server using SRF - sync
srf.connect(config.get('drachtio'), (err, srf) => {
if (err) return log_to_console(false, `error connecting to drachtio: ${err}`);
});
// Connect to freeswitch using MRF - sync
mrf.connect(config.get('freeswitch'), (err, mediaserver) => {
if (err) return log_to_console(false, `error connecting to mediaserver: ${err}`);
// This is important, this adds the mediaserver object to the SRF object so we can access it directly outside of this function
srf.locals.ms = mediaserver
console.log(`connected to mediaserver listening on ${JSON.stringify(mediaserver.sip)}`);
});
// Log connection and information - async, this is a listener
srf.on('connect', (err, hostport) => {
if (err) return log_to_console(false, `error on drachtio connect: ${err}`);
console.log(`successfully connected to drachtio listening on ${hostport}`);
});
srf.on('destroy', () => {
console.log("SRF destroyed");
});
srf.options((req, res) => {
// Shuts up the noise!
res.send(200);
});
// Listener for an INVITE coming in - async
srf.invite(async(req, res) => {
// req = request
// res = result
// Localised media server reference
const ms = req.app.locals.ms;
try {
// Create a media endpoint - "answer" the call
const {endpoint, dialog} = await ms.connectCaller(req, res);
console.log(`Endpoint UUID: ${endpoint.uuid} Dialog Call-Id: ${dialog.sip['callId']}`);
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`);
});
srf.createUAC(`sip:${config.to_number}@${config.outbound_sbc}`,
{
localSdp: b_endpoint.local.sdp
},
{
cbRequest: (err, req) => invite = req,
})
));
if (err) {
console.log("Hanging up B leg (CANCEL) due to ring timeout")
invite.cancel()
b_endpoint.destroy()
endpoint.destroy()
}
else {
b_endpoint.modify(b_dialog.remote.sdp);
ep.bridge(b_endpoint)
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment