1. context.onEvent(event) //=> event 설정
events
'variables' - on start call
'close' - on end session
'hangup' - on hangup channel
2. context.answer() //=> sip answer 수행
3. context.end() //=> FastAGI 종료, 다시 dialplan 으로..
아직 javascript에 익숙하지 않다면, 위 소스에서 event가 어떤 의미 인지 모를 수 있다. 간단히 한글로 설명 하자면,
만약 당신이 내 이름을 부른다면, 난 춤을 출 거에요.
내 이름을 부른다면, 에서의 이름은 => event명
춤을 춘다. => event발생시 내가 하려고 하는 일을 미리 설정한다.
이제 이벤트 들을 확인하기 위해 Node.js소스로 코딩을 해보자.
const AGIServer = require('ding-dong');
const handler = (context) => {
context
.onEvent('close')
.then((data) => {
console.log('close event=>', data);
})
.catch((error) => {
context.end();
});
context
.onEvent('hangup')
.then(() => {
console.log('hangup event 발생');
})
.catch((error) => {
context.end();
});
context
.onEvent('variables')
.then((vars) => {
return context.answer();
})
.then((result) => {
return context.end(); // context.end() 결과는 없다. undefined
})
.catch((error) => {
context.end();
});
};
const agi = new AGIServer(handler, { debug: true });
agi.start(5000);
[dialplan]
exten => s,1,NoOp(dialplan start====>) ; log
same => n,AGI(agi://127.0.0.1:5000) ; call FastAGI module
same => n,wait(10) ; wait 10 sec
same => n,Hangup() ; call hangup
위 AGI를 설명하자면 그냥 answer하고 해당 AGI를 end한다.
사실, 보통은 ding-dong모듈에서 variables event만 사용하지만, 문서에 close, hangup event가 보여 함께 확인해 보았다.
위 소스를 sngrep의 도움으로 흐름도를 뽑아 제어를 확인해 보면 아래와 같다.
자, 아무일도 하지 않으니 심심하다. 소스를 수정해서 소리가 들리게 만들어 보자. !!
const AGIServer = require('ding-dong');
const handler = (context) => {
context
.onEvent('variables')
.then((vars) => {
return context.channelStatus(vars.agi_channel); // get channel status
})
.then((results) => {
return context.answer(); // answer to caller
})
.then((results) => {
return context.streamFile('tt-monkeys', '1');
})
.then((results) => {
return context.end(); // return to the dialplan
})
.catch((error) => {
context.end();
});
};
const agi = new AGIServer(handler, { debug: true });
agi.start(5000);
이제 전화를 걸면, 시끄러운 원숭이소리가 들린다.
streamFile에서 절대경로는 가능할까 ? 해보자 !!!
return context.streamFile('/var/lib/asterisk/sounds/en/tt-monkeys', '1');
시끄러운 원숭이 소리가 들린다. 성공적이다. wav확장자가 없음을 유의하자.
streamFile 이라는 명령어가 왠지 stream으로 play 해줄 것 같은 기분이 든다. try 해보자 !!
웹상에서 wav파일을 다운로드 해주면 좋겠다. 구글링으로 Asterisk에서 play가능한 음원을 찾아보자. 16bit/mono/8000Hz PCM파일을 찾았다. 위대한 구글! 다른 규격의 음원도 있으니 스크랩 해두자!! (sample PCM)
그리고, streamFile부분을 아래처럼 변경한다.
return context.streamFile('http://mauvecloud.net/sounds/pcm1608m.wav', '1');
변경하고, 다시 node 실행. 전화를 걸면 기분 나쁜 웃음소리가 들릴 것이다.
어우,, 저도 글을 쓰려고 노력중이라 더더욱 글의 수고를 이해합니다.
ReactNative 앱에서 보이스통화를 하려고 하고, 보이스통화는 voip밖에 안들어봤던 저로써
어디서부터 찾아봐야하나 깝깝햇는데..감사합니다.