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

strophe.emuc.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* a simple MUC connection plugin
  2. * can only handle a single MUC room
  3. */
  4. /* global $, Strophe */
  5. import {getLogger} from "jitsi-meet-logger";
  6. const logger = getLogger(__filename);
  7. import ChatRoom from "./ChatRoom";
  8. import {ConnectionPluginListenable} from "./ConnectionPlugin";
  9. import XMPPEvents from "../../service/xmpp/XMPPEvents";
  10. class MucConnectionPlugin extends ConnectionPluginListenable {
  11. constructor(xmpp) {
  12. super();
  13. this.xmpp = xmpp;
  14. this.rooms = {};
  15. }
  16. init (connection) {
  17. super.init(connection);
  18. // add handlers (just once)
  19. this.connection.addHandler(this.onPresence.bind(this), null,
  20. 'presence', null, null, null, null);
  21. this.connection.addHandler(this.onPresenceUnavailable.bind(this),
  22. null, 'presence', 'unavailable', null);
  23. this.connection.addHandler(this.onPresenceError.bind(this), null,
  24. 'presence', 'error', null);
  25. this.connection.addHandler(this.onMessage.bind(this), null,
  26. 'message', null, null);
  27. this.connection.addHandler(this.onMute.bind(this),
  28. 'http://jitsi.org/jitmeet/audio', 'iq', 'set',null,null);
  29. }
  30. createRoom (jid, password, options) {
  31. const roomJid = Strophe.getBareJidFromJid(jid);
  32. if (this.rooms[roomJid]) {
  33. const errmsg = "You are already in the room!";
  34. logger.error(errmsg);
  35. throw new Error(errmsg);
  36. }
  37. this.rooms[roomJid] = new ChatRoom(this.connection, jid,
  38. password, this.xmpp, options);
  39. this.eventEmitter.emit(
  40. XMPPEvents.EMUC_ROOM_ADDED, this.rooms[roomJid]);
  41. return this.rooms[roomJid];
  42. }
  43. doLeave (jid) {
  44. this.eventEmitter.emit(
  45. XMPPEvents.EMUC_ROOM_REMOVED, this.rooms[jid]);
  46. delete this.rooms[jid];
  47. }
  48. onPresence (pres) {
  49. const from = pres.getAttribute('from');
  50. // What is this for? A workaround for something?
  51. if (pres.getAttribute('type')) {
  52. return true;
  53. }
  54. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  55. if(!room)
  56. return;
  57. // Parse status.
  58. if ($(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]' +
  59. '>status[code="201"]').length) {
  60. room.createNonAnonymousRoom();
  61. }
  62. room.onPresence(pres);
  63. return true;
  64. }
  65. onPresenceUnavailable (pres) {
  66. const from = pres.getAttribute('from');
  67. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  68. if(!room)
  69. return;
  70. room.onPresenceUnavailable(pres, from);
  71. return true;
  72. }
  73. onPresenceError (pres) {
  74. const from = pres.getAttribute('from');
  75. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  76. if(!room)
  77. return;
  78. room.onPresenceError(pres, from);
  79. return true;
  80. }
  81. onMessage (msg) {
  82. // FIXME: this is a hack. but jingle on muc makes nickchanges hard
  83. const from = msg.getAttribute('from');
  84. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  85. if(!room)
  86. return;
  87. room.onMessage(msg, from);
  88. return true;
  89. }
  90. setJingleSession (from, session) {
  91. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  92. if(!room)
  93. return;
  94. room.setJingleSession(session);
  95. }
  96. onMute(iq) {
  97. const from = iq.getAttribute('from');
  98. const room = this.rooms[Strophe.getBareJidFromJid(from)];
  99. if(!room)
  100. return;
  101. room.onMute(iq);
  102. return true;
  103. }
  104. }
  105. export default function(XMPP) {
  106. Strophe.addConnectionPlugin('emuc', new MucConnectionPlugin(XMPP));
  107. }