2022-10-11 10:47:54 +00:00
|
|
|
import { IStore } from '../../app/types';
|
2020-06-04 14:09:13 +00:00
|
|
|
import { conferenceLeft, conferenceWillLeave } from '../conference/actions';
|
|
|
|
import { getCurrentConference } from '../conference/functions';
|
2017-02-28 03:21:50 +00:00
|
|
|
import JitsiMeetJS, { JitsiConnectionEvents } from '../lib-jitsi-meet';
|
2016-12-05 15:14:50 +00:00
|
|
|
|
2016-10-05 14:36:59 +00:00
|
|
|
import {
|
2022-10-18 16:21:48 +00:00
|
|
|
CONNECTION_WILL_CONNECT
|
2016-10-05 14:36:59 +00:00
|
|
|
} from './actionTypes';
|
2022-10-18 16:21:48 +00:00
|
|
|
import {
|
|
|
|
connectionDisconnected,
|
|
|
|
connectionEstablished,
|
|
|
|
connectionFailed,
|
|
|
|
constructOptions
|
|
|
|
} from './actions.any';
|
2019-04-25 08:14:23 +00:00
|
|
|
import { JITSI_CONNECTION_URL_KEY } from './constants';
|
2019-08-21 14:50:00 +00:00
|
|
|
import logger from './logger';
|
2018-05-17 14:53:51 +00:00
|
|
|
|
2022-06-16 12:27:41 +00:00
|
|
|
export * from './actions.any';
|
|
|
|
|
2016-10-05 14:36:59 +00:00
|
|
|
/**
|
|
|
|
* Opens new connection.
|
|
|
|
*
|
2018-11-08 12:25:02 +00:00
|
|
|
* @param {string} [id] - The XMPP user's ID (e.g. {@code user@server.com}).
|
2017-09-18 07:09:43 +00:00
|
|
|
* @param {string} [password] - The XMPP user's password.
|
2017-02-03 18:50:06 +00:00
|
|
|
* @returns {Function}
|
2016-10-05 14:36:59 +00:00
|
|
|
*/
|
2022-09-14 07:54:56 +00:00
|
|
|
export function connect(id?: string, password?: string) {
|
2022-10-11 10:47:54 +00:00
|
|
|
return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
|
2016-10-05 14:36:59 +00:00
|
|
|
const state = getState();
|
2022-06-16 12:27:41 +00:00
|
|
|
const options = constructOptions(state);
|
2019-04-25 08:14:23 +00:00
|
|
|
const { locationURL } = state['features/base/connection'];
|
2020-08-12 20:32:09 +00:00
|
|
|
const { jwt } = state['features/base/jwt'];
|
|
|
|
const connection = new JitsiMeetJS.JitsiConnection(options.appId, jwt, options);
|
2016-12-05 15:14:50 +00:00
|
|
|
|
2019-04-25 08:14:23 +00:00
|
|
|
connection[JITSI_CONNECTION_URL_KEY] = locationURL;
|
|
|
|
|
2017-07-06 11:51:35 +00:00
|
|
|
dispatch(_connectionWillConnect(connection));
|
|
|
|
|
2016-12-05 15:14:50 +00:00
|
|
|
connection.addEventListener(
|
2017-06-15 00:40:51 +00:00
|
|
|
JitsiConnectionEvents.CONNECTION_DISCONNECTED,
|
|
|
|
_onConnectionDisconnected);
|
2016-12-05 15:14:50 +00:00
|
|
|
connection.addEventListener(
|
2017-06-15 00:40:51 +00:00
|
|
|
JitsiConnectionEvents.CONNECTION_ESTABLISHED,
|
|
|
|
_onConnectionEstablished);
|
2016-12-05 15:14:50 +00:00
|
|
|
connection.addEventListener(
|
2017-06-15 00:40:51 +00:00
|
|
|
JitsiConnectionEvents.CONNECTION_FAILED,
|
|
|
|
_onConnectionFailed);
|
2016-12-05 15:14:50 +00:00
|
|
|
|
2019-05-03 18:11:33 +00:00
|
|
|
connection.connect({
|
2017-09-18 07:09:43 +00:00
|
|
|
id,
|
2017-09-08 13:36:42 +00:00
|
|
|
password
|
|
|
|
});
|
2016-12-05 15:14:50 +00:00
|
|
|
|
|
|
|
/**
|
2018-05-21 03:58:34 +00:00
|
|
|
* Dispatches {@code CONNECTION_DISCONNECTED} action when connection is
|
2016-12-05 15:14:50 +00:00
|
|
|
* disconnected.
|
|
|
|
*
|
2017-01-31 20:58:48 +00:00
|
|
|
* @private
|
2017-09-24 21:51:43 +00:00
|
|
|
* @returns {void}
|
2016-12-05 15:14:50 +00:00
|
|
|
*/
|
2020-05-20 08:25:31 +00:00
|
|
|
function _onConnectionDisconnected() {
|
2018-05-23 11:43:40 +00:00
|
|
|
unsubscribe();
|
2020-05-20 08:25:31 +00:00
|
|
|
dispatch(connectionDisconnected(connection));
|
2016-12-05 15:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolves external promise when connection is established.
|
|
|
|
*
|
2017-01-31 20:58:48 +00:00
|
|
|
* @private
|
2017-09-24 21:51:43 +00:00
|
|
|
* @returns {void}
|
2016-12-05 15:14:50 +00:00
|
|
|
*/
|
2017-01-31 20:58:48 +00:00
|
|
|
function _onConnectionEstablished() {
|
2018-05-23 11:43:40 +00:00
|
|
|
connection.removeEventListener(
|
|
|
|
JitsiConnectionEvents.CONNECTION_ESTABLISHED,
|
|
|
|
_onConnectionEstablished);
|
2018-07-02 21:22:51 +00:00
|
|
|
dispatch(connectionEstablished(connection, Date.now()));
|
2016-12-05 15:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rejects external promise when connection fails.
|
|
|
|
*
|
|
|
|
* @param {JitsiConnectionErrors} err - Connection error.
|
2017-09-22 18:18:16 +00:00
|
|
|
* @param {string} [msg] - Error message supplied by lib-jitsi-meet.
|
|
|
|
* @param {Object} [credentials] - The invalid credentials that were
|
|
|
|
* used to authenticate and the authentication failed.
|
|
|
|
* @param {string} [credentials.jid] - The XMPP user's ID.
|
|
|
|
* @param {string} [credentials.password] - The XMPP user's password.
|
2018-07-02 21:22:51 +00:00
|
|
|
* @param {Object} details - Additional information about the error.
|
2017-01-31 20:58:48 +00:00
|
|
|
* @private
|
2017-09-24 21:51:43 +00:00
|
|
|
* @returns {void}
|
2016-12-05 15:14:50 +00:00
|
|
|
*/
|
2018-07-02 21:22:51 +00:00
|
|
|
function _onConnectionFailed( // eslint-disable-line max-params
|
|
|
|
err: string,
|
|
|
|
msg: string,
|
2022-09-14 07:54:56 +00:00
|
|
|
credentials: any,
|
2018-07-02 21:22:51 +00:00
|
|
|
details: Object) {
|
2016-12-05 15:14:50 +00:00
|
|
|
unsubscribe();
|
2018-05-02 18:27:50 +00:00
|
|
|
dispatch(
|
|
|
|
connectionFailed(
|
|
|
|
connection, {
|
|
|
|
credentials,
|
2018-07-02 21:22:51 +00:00
|
|
|
details,
|
2018-05-02 18:27:50 +00:00
|
|
|
name: err,
|
|
|
|
message: msg
|
|
|
|
}
|
|
|
|
));
|
2016-12-05 15:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-23 11:43:40 +00:00
|
|
|
* Unsubscribe the connection instance from
|
|
|
|
* {@code CONNECTION_DISCONNECTED} and {@code CONNECTION_FAILED} events.
|
2016-12-05 15:14:50 +00:00
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function unsubscribe() {
|
|
|
|
connection.removeEventListener(
|
2018-05-23 11:43:40 +00:00
|
|
|
JitsiConnectionEvents.CONNECTION_DISCONNECTED,
|
|
|
|
_onConnectionDisconnected);
|
2016-12-05 15:14:50 +00:00
|
|
|
connection.removeEventListener(
|
2017-06-15 00:40:51 +00:00
|
|
|
JitsiConnectionEvents.CONNECTION_FAILED,
|
|
|
|
_onConnectionFailed);
|
2016-12-05 15:14:50 +00:00
|
|
|
}
|
2016-10-05 14:36:59 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-05-21 03:58:34 +00:00
|
|
|
/**
|
|
|
|
* Create an action for when a connection will connect.
|
|
|
|
*
|
|
|
|
* @param {JitsiConnection} connection - The {@code JitsiConnection} which will
|
|
|
|
* connect.
|
|
|
|
* @private
|
|
|
|
* @returns {{
|
|
|
|
* type: CONNECTION_WILL_CONNECT,
|
|
|
|
* connection: JitsiConnection
|
|
|
|
* }}
|
|
|
|
*/
|
2022-09-14 07:54:56 +00:00
|
|
|
function _connectionWillConnect(connection: Object) {
|
2018-05-21 03:58:34 +00:00
|
|
|
return {
|
|
|
|
type: CONNECTION_WILL_CONNECT,
|
|
|
|
connection
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-10-21 11:09:15 +00:00
|
|
|
|
|
|
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
2017-05-09 05:15:43 +00:00
|
|
|
/**
|
|
|
|
* Closes connection.
|
|
|
|
*
|
2022-10-21 11:09:15 +00:00
|
|
|
* @param {boolean} _ - Used in web.
|
2017-05-09 05:15:43 +00:00
|
|
|
* @returns {Function}
|
|
|
|
*/
|
2022-10-21 11:09:15 +00:00
|
|
|
export function disconnect(_?: boolean) {
|
|
|
|
/* eslint-enable @typescript-eslint/no-unused-vars */
|
2022-10-11 10:47:54 +00:00
|
|
|
return (dispatch: IStore['dispatch'], getState: IStore['getState']): Promise<void> => {
|
2017-05-09 05:15:43 +00:00
|
|
|
const state = getState();
|
2017-07-06 11:51:35 +00:00
|
|
|
|
2017-10-17 22:10:42 +00:00
|
|
|
// The conference we have already joined or are joining.
|
2018-05-24 20:15:32 +00:00
|
|
|
const conference_ = getCurrentConference(state);
|
2017-05-09 05:15:43 +00:00
|
|
|
|
2017-07-06 11:51:35 +00:00
|
|
|
// Promise which completes when the conference has been left and the
|
|
|
|
// connection has been disconnected.
|
2017-05-09 05:15:43 +00:00
|
|
|
let promise;
|
|
|
|
|
|
|
|
// Leave the conference.
|
2017-07-07 17:21:08 +00:00
|
|
|
if (conference_) {
|
2017-05-09 05:15:43 +00:00
|
|
|
// In a fashion similar to JitsiConference's CONFERENCE_LEFT event
|
|
|
|
// (and the respective Redux action) which is fired after the
|
|
|
|
// conference has been left, notify the application about the
|
|
|
|
// intention to leave the conference.
|
2017-07-07 17:21:08 +00:00
|
|
|
dispatch(conferenceWillLeave(conference_));
|
2017-05-09 05:15:43 +00:00
|
|
|
|
2018-03-05 01:25:23 +00:00
|
|
|
promise
|
|
|
|
= conference_.leave()
|
2022-09-14 07:54:56 +00:00
|
|
|
.catch((error: Error) => {
|
2018-05-17 14:53:51 +00:00
|
|
|
logger.warn(
|
2018-05-21 03:58:34 +00:00
|
|
|
'JitsiConference.leave() rejected with:',
|
2018-05-17 14:53:51 +00:00
|
|
|
error);
|
|
|
|
|
2018-03-05 01:25:23 +00:00
|
|
|
// The library lib-jitsi-meet failed to make the
|
|
|
|
// JitsiConference leave. Which may be because
|
|
|
|
// JitsiConference thinks it has already left.
|
|
|
|
// Regardless of the failure reason, continue in
|
|
|
|
// jitsi-meet as if the leave has succeeded.
|
|
|
|
dispatch(conferenceLeft(conference_));
|
|
|
|
});
|
2017-05-09 05:15:43 +00:00
|
|
|
} else {
|
|
|
|
promise = Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disconnect the connection.
|
2017-07-07 17:21:08 +00:00
|
|
|
const { connecting, connection } = state['features/base/connection'];
|
|
|
|
|
2017-10-17 22:10:42 +00:00
|
|
|
// The connection we have already connected or are connecting.
|
2017-07-07 17:21:08 +00:00
|
|
|
const connection_ = connection || connecting;
|
|
|
|
|
|
|
|
if (connection_) {
|
|
|
|
promise = promise.then(() => connection_.disconnect());
|
2019-04-25 08:14:23 +00:00
|
|
|
} else {
|
2019-05-22 10:01:58 +00:00
|
|
|
logger.info('No connection found while disconnecting.');
|
2017-05-09 05:15:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
};
|
|
|
|
}
|