Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. const SIP = require('sip.js');
  2. const net = require('net');
  3.  
  4. const TransportStatus = {
  5. STATUS_CONNECTING: 'STATUS_CONNECTING',
  6. STATUS_CONNECTED: 'STATUS_CONNECTED',
  7. STATUS_CLOSING: 'STATUS_CLOSING',
  8. STATUS_CLOSED: 'STATUS_CLOSED'
  9. };
  10.  
  11. class SipTcpTransport extends SIP.Transport {
  12. constructor(logger, options) {
  13. super(logger, options);
  14. this.type = SIP.TypeStrings.Transport;
  15. this.status = TransportStatus.STATUS_CONNECTING;
  16. this.configuration = options;
  17. }
  18.  
  19. isConnected() {
  20. return this.status === TransportStatus.STATUS_CONNECTED;
  21. }
  22.  
  23. connectPromise(options) {
  24. let socket = new net.Socket();
  25. this._tcpSocket = socket;
  26. let { ip, port } = this.configuration;
  27.  
  28. let promise = new Promise((resolve, reject) => {
  29. socket.connect(port, ip, {}, () => {
  30. this.status = TransportStatus.STATUS_CONNECTED;
  31. this.emit('connected');
  32.  
  33. resolve({ overrideEvent: true });
  34. });
  35. });
  36.  
  37. socket.setEncoding('utf8');
  38. this.boundOnMessage = this.onMessage.bind(this);
  39. this.boundOnError = this.onError.bind(this);
  40. this.boundOnClose = this.onClose.bind(this);
  41.  
  42. socket.on('data', this.boundOnMessage);
  43. socket.on('error', this.boundOnError);
  44. socket.on('close', this.boundOnClose);
  45.  
  46. return promise;
  47. }
  48.  
  49. sendPromise(message, options) {
  50. if (!this._tcpSocket) {
  51. return Promise.reject();
  52. }
  53.  
  54. this._tcpSocket.write(message);
  55. return Promise.resolve({ msg: message });
  56. }
  57.  
  58. disconnectPromise(options) {
  59. if (!this._tcpSocket) {
  60. return Promise.reject();
  61. }
  62.  
  63. this._tcpSocket.destroy();
  64. return Promise.resolve();
  65. }
  66.  
  67. onMessage(data) {
  68. let finishedData;
  69.  
  70. if (/^(\r\n)+$/.test(data)) {
  71. // this.clearKeepAliveTimeout();
  72.  
  73. // if (this.configuration.traceSip === true) {
  74. // this.logger.log(
  75. // 'received WebSocket message with CRLF Keep Alive response'
  76. // );
  77. // }
  78. return;
  79. } else if (!data) {
  80. this.logger.warn('received empty message, message discarded');
  81. return;
  82. } else if (typeof data !== 'string') {
  83. // WebSocket binary message.
  84. // try {
  85. // // the UInt8Data was here prior to types, and doesn't check
  86. // finishedData = String.fromCharCode.apply(null, (new Uint8Array(data) as unknown as Array<number>));
  87. // } catch (err) {
  88. // this.logger.warn("received WebSocket binary message failed to be converted into string, message discarded");
  89. // return;
  90. // }
  91. // if (this.configuration.traceSip === true) {
  92. // this.logger.log("received WebSocket binary message:\n\n" + data + "\n");
  93. // }
  94. } else {
  95. // WebSocket text message.
  96. // if (this.configuration.traceSip === true) {
  97. // this.logger.log("received WebSocket text message:\n\n" + data + "\n");
  98. // }
  99. finishedData = data;
  100. }
  101.  
  102. this.emit('message', finishedData);
  103. }
  104.  
  105. onError(e) {
  106. this.logger.warn('Transport error: ' + e);
  107. this.emit('transportError');
  108. }
  109.  
  110. onClose(e) {
  111. this.logger.log(
  112. 'TCPSocket disconnected (code: ' +
  113. e.code +
  114. (e.reason ? '| reason: ' + e.reason : '') +
  115. ')'
  116. );
  117.  
  118. this.status = TransportStatus.STATUS_CLOSED;
  119. this.emit('disconnected', { code: e.code, reason: e.reason });
  120. }
  121. }
  122.  
  123. module.exports = SipTcpTransport;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement