Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

xmpp.js 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* global $, APP, config, Strophe*/
  2. var logger = require("jitsi-meet-logger").getLogger(__filename);
  3. var EventEmitter = require("events");
  4. var Pako = require("pako");
  5. var StreamEventTypes = require("../../service/RTC/StreamEventTypes");
  6. var RTCEvents = require("../../service/RTC/RTCEvents");
  7. var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  8. var JitsiConnectionErrors = require("../../JitsiConnectionErrors");
  9. var JitsiConnectionEvents = require("../../JitsiConnectionEvents");
  10. var RTC = require("../RTC/RTC");
  11. var authenticatedUser = false;
  12. function createConnection(bosh) {
  13. bosh = bosh || '/http-bind';
  14. return new Strophe.Connection(bosh);
  15. };
  16. //!!!!!!!!!! FIXME: ...
  17. function initStrophePlugins(XMPP)
  18. {
  19. require("./strophe.emuc")(XMPP);
  20. require("./strophe.jingle")(XMPP, XMPP.eventEmitter);
  21. // require("./strophe.moderate")(XMPP, eventEmitter);
  22. require("./strophe.util")();
  23. require("./strophe.ping")(XMPP, XMPP.eventEmitter);
  24. require("./strophe.rayo")();
  25. require("./strophe.logger")();
  26. }
  27. //!!!!!!!!!! FIXME: ...
  28. ///**
  29. // * If given <tt>localStream</tt> is video one this method will advertise it's
  30. // * video type in MUC presence.
  31. // * @param localStream new or modified <tt>LocalStream</tt>.
  32. // */
  33. //function broadcastLocalVideoType(localStream) {
  34. // if (localStream.videoType)
  35. // XMPP.addToPresence('videoType', localStream.videoType);
  36. //}
  37. //
  38. //function registerListeners() {
  39. // RTC.addStreamListener(
  40. // broadcastLocalVideoType,
  41. // StreamEventTypes.EVENT_TYPE_LOCAL_CHANGED
  42. // );
  43. // RTC.addListener(RTCEvents.AVAILABLE_DEVICES_CHANGED, function (devices) {
  44. // XMPP.addToPresence("devices", devices);
  45. // });
  46. //}
  47. function XMPP(options) {
  48. this.eventEmitter = new EventEmitter();
  49. this.connection = null;
  50. this.disconnectInProgress = false;
  51. this.forceMuted = false;
  52. this.options = options;
  53. initStrophePlugins(this);
  54. // registerListeners();
  55. this.connection = createConnection(options.bosh);
  56. }
  57. XMPP.prototype.getConnection = function(){ return connection; };
  58. XMPP.prototype._connect = function (jid, password) {
  59. var self = this;
  60. // connection.connect() starts the connection process.
  61. //
  62. // As the connection process proceeds, the user supplied callback will
  63. // be triggered multiple times with status updates. The callback should
  64. // take two arguments - the status code and the error condition.
  65. //
  66. // The status code will be one of the values in the Strophe.Status
  67. // constants. The error condition will be one of the conditions defined
  68. // in RFC 3920 or the condition ‘strophe-parsererror’.
  69. //
  70. // The Parameters wait, hold and route are optional and only relevant
  71. // for BOSH connections. Please see XEP 124 for a more detailed
  72. // explanation of the optional parameters.
  73. //
  74. // Connection status constants for use by the connection handler
  75. // callback.
  76. //
  77. // Status.ERROR - An error has occurred (websockets specific)
  78. // Status.CONNECTING - The connection is currently being made
  79. // Status.CONNFAIL - The connection attempt failed
  80. // Status.AUTHENTICATING - The connection is authenticating
  81. // Status.AUTHFAIL - The authentication attempt failed
  82. // Status.CONNECTED - The connection has succeeded
  83. // Status.DISCONNECTED - The connection has been terminated
  84. // Status.DISCONNECTING - The connection is currently being terminated
  85. // Status.ATTACHED - The connection has been attached
  86. var anonymousConnectionFailed = false;
  87. var connectionFailed = false;
  88. var lastErrorMsg;
  89. this.connection.connect(jid, password, function (status, msg) {
  90. logger.log('Strophe status changed to',
  91. Strophe.getStatusString(status), msg);
  92. if (status === Strophe.Status.CONNECTED) {
  93. if (self.options.useStunTurn) {
  94. self.connection.jingle.getStunAndTurnCredentials();
  95. }
  96. logger.info("My Jabber ID: " + self.connection.jid);
  97. self.connection.ping.startInterval(config.hosts.domain);
  98. if (password)
  99. authenticatedUser = true;
  100. if (self.connection && self.connection.connected &&
  101. Strophe.getResourceFromJid(self.connection.jid)) {
  102. // .connected is true while connecting?
  103. // self.connection.send($pres());
  104. self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_ESTABLISHED,
  105. Strophe.getResourceFromJid(self.connection.jid));
  106. }
  107. } else if (status === Strophe.Status.CONNFAIL) {
  108. if (msg === 'x-strophe-bad-non-anon-jid') {
  109. anonymousConnectionFailed = true;
  110. }
  111. else
  112. {
  113. connectionFailed = true;
  114. }
  115. lastErrorMsg = msg;
  116. } else if (status === Strophe.Status.DISCONNECTED) {
  117. // Stop ping interval
  118. self.connection.ping.stopInterval();
  119. self.disconnectInProgress = false;
  120. if (anonymousConnectionFailed) {
  121. // prompt user for username and password
  122. self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_FAILED,
  123. JitsiConnectionErrors.PASSWORD_REQUIRED);
  124. } else if(connectionFailed) {
  125. self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_FAILED,
  126. JitsiConnectionErrors.OTHER_ERROR,
  127. msg ? msg : lastErrorMsg);
  128. } else {
  129. self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_DISCONNECTED,
  130. msg ? msg : lastErrorMsg);
  131. }
  132. } else if (status === Strophe.Status.AUTHFAIL) {
  133. // wrong password or username, prompt user
  134. self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_FAILED,
  135. JitsiConnectionErrors.PASSWORD_REQUIRED);
  136. }
  137. });
  138. }
  139. XMPP.prototype.connect = function (jid, password) {
  140. if(!jid) {
  141. var configDomain = this.options.hosts.anonymousdomain || this.options.hosts.domain;
  142. // Force authenticated domain if room is appended with '?login=true'
  143. if (this.options.hosts.anonymousdomain &&
  144. window.location.search.indexOf("login=true") !== -1) {
  145. configDomain = this.options.hosts.domain;
  146. }
  147. jid = configDomain || window.location.hostname;
  148. }
  149. return this._connect(jid, password);
  150. };
  151. XMPP.prototype.createRoom = function (roomName, options, useNicks, nick) {
  152. var roomjid = roomName + '@' + this.options.hosts.muc;
  153. if (useNicks) {
  154. if (nick) {
  155. roomjid += '/' + nick;
  156. } else {
  157. roomjid += '/' + Strophe.getNodeFromJid(this.connection.jid);
  158. }
  159. } else {
  160. var tmpJid = Strophe.getNodeFromJid(this.connection.jid);
  161. if(!authenticatedUser)
  162. tmpJid = tmpJid.substr(0, 8);
  163. roomjid += '/' + tmpJid;
  164. }
  165. return this.connection.emuc.createRoom(roomjid, null, options);
  166. }
  167. XMPP.prototype.addListener = function(type, listener) {
  168. this.eventEmitter.on(type, listener);
  169. };
  170. XMPP.prototype.removeListener = function (type, listener) {
  171. this.eventEmitter.removeListener(type, listener);
  172. };
  173. //FIXME: this should work with the room
  174. XMPP.prototype.leaveRoom = function (jid) {
  175. var handler = this.connection.jingle.jid2session[jid];
  176. if (handler && handler.peerconnection) {
  177. // FIXME: probably removing streams is not required and close() should
  178. // be enough
  179. if (RTC.localAudio) {
  180. handler.peerconnection.removeStream(
  181. RTC.localAudio.getOriginalStream(), true);
  182. }
  183. if (RTC.localVideo) {
  184. handler.peerconnection.removeStream(
  185. RTC.localVideo.getOriginalStream(), true);
  186. }
  187. handler.peerconnection.close();
  188. }
  189. this.eventEmitter.emit(XMPPEvents.DISPOSE_CONFERENCE);
  190. this.connection.emuc.doLeave(jid);
  191. };
  192. /**
  193. * Sends 'data' as a log message to the focus. Returns true iff a message
  194. * was sent.
  195. * @param data
  196. * @returns {boolean} true iff a message was sent.
  197. */
  198. XMPP.prototype.sendLogs = function (data) {
  199. if(!this.connection.emuc.focusMucJid)
  200. return false;
  201. var deflate = true;
  202. var content = JSON.stringify(data);
  203. if (deflate) {
  204. content = String.fromCharCode.apply(null, Pako.deflateRaw(content));
  205. }
  206. content = Base64.encode(content);
  207. // XEP-0337-ish
  208. var message = $msg({to: this.connection.emuc.focusMucJid, type: 'normal'});
  209. message.c('log', { xmlns: 'urn:xmpp:eventlog',
  210. id: 'PeerConnectionStats'});
  211. message.c('message').t(content).up();
  212. if (deflate) {
  213. message.c('tag', {name: "deflated", value: "true"}).up();
  214. }
  215. message.up();
  216. this.connection.send(message);
  217. return true;
  218. };
  219. // Gets the logs from strophe.jingle.
  220. XMPP.prototype.getJingleLog = function () {
  221. return this.connection.jingle ? this.connection.jingle.getLog() : {};
  222. };
  223. // Gets the logs from strophe.
  224. XMPP.prototype.getXmppLog = function () {
  225. return this.connection.logger ? this.connection.logger.log : null;
  226. };
  227. XMPP.prototype.dial = function (to, from, roomName,roomPass) {
  228. this.connection.rayo.dial(to, from, roomName,roomPass);
  229. };
  230. XMPP.prototype.setMute = function (jid, mute) {
  231. this.connection.moderate.setMute(jid, mute);
  232. };
  233. XMPP.prototype.eject = function (jid) {
  234. this.connection.moderate.eject(jid);
  235. };
  236. XMPP.prototype.getSessions = function () {
  237. return this.connection.jingle.sessions;
  238. };
  239. XMPP.prototype.disconnect = function () {
  240. if (this.disconnectInProgress || !this.connection || !this.connection.connected)
  241. {
  242. this.eventEmitter.emit(JitsiConnectionEvents.WRONG_STATE);
  243. return;
  244. }
  245. this.disconnectInProgress = true;
  246. this.connection.disconnect();
  247. };
  248. module.exports = XMPP;