You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

strophe.rayo.ts 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { getLogger } from '@jitsi/logger';
  2. import $ from 'jquery';
  3. import { $iq } from 'strophe.js';
  4. import type { Connection } from 'strophe.js';
  5. import ConnectionPlugin from './ConnectionPlugin';
  6. const logger = getLogger('modules/xmpp/strophe.rayo');
  7. const RAYO_XMLNS = 'urn:xmpp:rayo:1';
  8. /**
  9. *
  10. */
  11. export default class RayoConnectionPlugin extends ConnectionPlugin {
  12. private callResource: string | null = null;
  13. /**
  14. *
  15. * @param connection
  16. */
  17. init(connection: Connection): void {
  18. super.init(connection);
  19. connection.addHandler(
  20. this.onRayo.bind(this),
  21. RAYO_XMLNS,
  22. 'iq',
  23. 'set',
  24. null,
  25. null
  26. );
  27. }
  28. /**
  29. *
  30. * @param iq
  31. */
  32. onRayo(iq: any): any {
  33. logger.info('Rayo IQ', iq);
  34. }
  35. /* eslint-disable max-params */
  36. /**
  37. *
  38. * @param to
  39. * @param from
  40. * @param roomName
  41. * @param roomPass
  42. * @param focusMucJid
  43. */
  44. dial(
  45. to: string,
  46. from: string,
  47. roomName: string,
  48. roomPass: string,
  49. focusMucJid: string
  50. ): Promise<void> {
  51. return new Promise((resolve, reject) => {
  52. if (!focusMucJid) {
  53. reject(new Error('Internal error!'));
  54. return;
  55. }
  56. const req = $iq({
  57. type: 'set',
  58. to: focusMucJid,
  59. });
  60. req.c('dial', {
  61. xmlns: RAYO_XMLNS,
  62. to,
  63. from,
  64. });
  65. req.c('header', {
  66. name: 'JvbRoomName',
  67. value: roomName,
  68. }).up();
  69. if (roomPass?.length) {
  70. req.c('header', {
  71. name: 'JvbRoomPassword',
  72. value: roomPass,
  73. }).up();
  74. }
  75. (this.connection as Connection).sendIQ(
  76. req,
  77. result => {
  78. logger.info('Dial result ', result);
  79. // eslint-disable-next-line newline-per-chained-call
  80. const resource = $(result).find('ref').attr('uri');
  81. this.callResource = resource.substr('xmpp:'.length);
  82. logger.info(`Received call resource: ${this.callResource}`);
  83. resolve();
  84. },
  85. error => {
  86. logger.info('Dial error ', error);
  87. reject(error);
  88. }
  89. );
  90. });
  91. }
  92. /* eslint-enable max-params */
  93. /**
  94. *
  95. */
  96. hangup(): Promise<void> {
  97. return new Promise((resolve, reject) => {
  98. if (!this.callResource) {
  99. reject(new Error('No call in progress'));
  100. logger.warn('No call in progress');
  101. return;
  102. }
  103. const req = $iq({
  104. type: 'set',
  105. to: this.callResource,
  106. });
  107. req.c('hangup', {
  108. xmlns: RAYO_XMLNS,
  109. });
  110. (this.connection as Connection).sendIQ(
  111. req,
  112. result => {
  113. logger.info('Hangup result ', result);
  114. this.callResource = null;
  115. resolve();
  116. },
  117. error => {
  118. logger.info('Hangup error ', error);
  119. this.callResource = null;
  120. reject(new Error('Hangup error '));
  121. }
  122. );
  123. });
  124. }
  125. }