您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

strophe.rayo.ts 3.5KB

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