選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

xmpp.js 9.5KB

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