Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. /**
  2. * FreeSWITCH API.
  3. * @file: freeswitch-listener/apis/freeswitch.js
  4. */
  5.  
  6. const ESL = require('modesl');
  7.  
  8. const FreeswitchConfig = require('../config/config.js').getConfig('freeswitch');
  9.  
  10. const Event = {
  11. Connection: {
  12. READY: 'esl::ready',
  13. CLOSED: 'esl::end',
  14. ERROR: 'error',
  15. },
  16. RECEIVED: 'esl::event::*::*',
  17. };
  18. const ALL_EVENTS = 'all';
  19. const DTMF_EVENTS = 'DTMF';
  20. let connection = null;
  21.  
  22. const connect = () => new Promise((resolve, reject) => {
  23. if (connection !== null && connection.connected()) {
  24. resolve(connection);
  25. } else {
  26. // Opening new FreeSWITCH event socket connection...
  27. connection = new ESL.Connection(FreeswitchConfig.ip, FreeswitchConfig.port, FreeswitchConfig.password);
  28. connection.on(Event.Connection.ERROR, () => {
  29. // Error connecting to FreeSWITCH!
  30. reject('Connection error');
  31. });
  32. connection.on(Event.Connection.CLOSED, () => {
  33. // Connection to FreeSWITCH closed!
  34. reject('Connection closed');
  35. });
  36. connection.on(Event.Connection.READY, () => {
  37. // Connection to FreeSWITCH established!
  38. resolve(connection);
  39. });
  40. }
  41. });
  42.  
  43. exports.ALL_EVENTS = ALL_EVENTS;
  44. exports.DTMF_EVENTS = DTMF_EVENTS;
  45. exports.Event = Event;
  46. exports.connect = connect;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement