2017-02-03 18:50:06 +00:00
|
|
|
/* @flow */
|
|
|
|
|
|
|
|
import type { Dispatch } from 'redux';
|
|
|
|
|
2016-12-05 15:14:50 +00:00
|
|
|
import { conferenceWillLeave } from '../conference';
|
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 {
|
|
|
|
CONNECTION_DISCONNECTED,
|
|
|
|
CONNECTION_ESTABLISHED,
|
|
|
|
CONNECTION_FAILED,
|
|
|
|
SET_DOMAIN
|
|
|
|
} from './actionTypes';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens new connection.
|
|
|
|
*
|
2017-02-03 18:50:06 +00:00
|
|
|
* @returns {Function}
|
2016-10-05 14:36:59 +00:00
|
|
|
*/
|
|
|
|
export function connect() {
|
2017-02-03 18:50:06 +00:00
|
|
|
return (dispatch: Dispatch<*>, getState: Function) => {
|
2016-10-05 14:36:59 +00:00
|
|
|
const state = getState();
|
2017-02-23 23:12:50 +00:00
|
|
|
const { options } = state['features/base/connection'];
|
|
|
|
const { room } = state['features/base/conference'];
|
2016-12-05 15:14:50 +00:00
|
|
|
const connection
|
|
|
|
= new JitsiMeetJS.JitsiConnection(
|
2017-02-23 23:12:50 +00:00
|
|
|
options.appId,
|
|
|
|
options.token,
|
2016-12-05 15:14:50 +00:00
|
|
|
{
|
2017-02-23 23:12:50 +00:00
|
|
|
...options,
|
2017-01-28 23:26:09 +00:00
|
|
|
bosh:
|
2017-02-23 23:12:50 +00:00
|
|
|
options.bosh
|
2017-01-28 23:26:09 +00:00
|
|
|
|
|
|
|
// XXX The Jitsi Meet deployments require the room
|
|
|
|
// argument to be in lower case at the time of this
|
|
|
|
// writing but, unfortunately, they do not ignore
|
|
|
|
// case themselves.
|
|
|
|
+ (room ? `?room=${room.toLowerCase()}` : '')
|
2016-12-05 15:14:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
connection.addEventListener(
|
2016-10-05 14:36:59 +00:00
|
|
|
JitsiConnectionEvents.CONNECTION_DISCONNECTED,
|
2017-01-31 20:58:48 +00:00
|
|
|
_onConnectionDisconnected);
|
2016-12-05 15:14:50 +00:00
|
|
|
connection.addEventListener(
|
2016-10-05 14:36:59 +00:00
|
|
|
JitsiConnectionEvents.CONNECTION_ESTABLISHED,
|
2017-01-31 20:58:48 +00:00
|
|
|
_onConnectionEstablished);
|
2016-12-05 15:14:50 +00:00
|
|
|
connection.addEventListener(
|
2016-10-05 14:36:59 +00:00
|
|
|
JitsiConnectionEvents.CONNECTION_FAILED,
|
2017-01-31 20:58:48 +00:00
|
|
|
_onConnectionFailed);
|
2016-12-05 15:14:50 +00:00
|
|
|
|
|
|
|
connection.connect();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatches CONNECTION_DISCONNECTED action when connection is
|
|
|
|
* disconnected.
|
|
|
|
*
|
|
|
|
* @param {string} message - Disconnect reason.
|
|
|
|
* @returns {void}
|
2017-01-31 20:58:48 +00:00
|
|
|
* @private
|
2016-12-05 15:14:50 +00:00
|
|
|
*/
|
2017-01-31 20:58:48 +00:00
|
|
|
function _onConnectionDisconnected(message: string) {
|
2016-12-05 15:14:50 +00:00
|
|
|
connection.removeEventListener(
|
2016-10-05 14:36:59 +00:00
|
|
|
JitsiConnectionEvents.CONNECTION_DISCONNECTED,
|
2017-01-31 20:58:48 +00:00
|
|
|
_onConnectionDisconnected);
|
2016-12-05 15:14:50 +00:00
|
|
|
|
|
|
|
dispatch(_connectionDisconnected(connection, message));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolves external promise when connection is established.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
2017-01-31 20:58:48 +00:00
|
|
|
* @private
|
2016-12-05 15:14:50 +00:00
|
|
|
*/
|
2017-01-31 20:58:48 +00:00
|
|
|
function _onConnectionEstablished() {
|
2016-12-05 15:14:50 +00:00
|
|
|
unsubscribe();
|
2017-01-31 20:58:48 +00:00
|
|
|
dispatch(connectionEstablished(connection));
|
2016-12-05 15:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rejects external promise when connection fails.
|
|
|
|
*
|
|
|
|
* @param {JitsiConnectionErrors} err - Connection error.
|
|
|
|
* @returns {void}
|
2017-01-31 20:58:48 +00:00
|
|
|
* @private
|
2016-12-05 15:14:50 +00:00
|
|
|
*/
|
2017-01-31 20:58:48 +00:00
|
|
|
function _onConnectionFailed(err) {
|
2016-12-05 15:14:50 +00:00
|
|
|
unsubscribe();
|
|
|
|
console.error('CONNECTION FAILED:', err);
|
2017-01-31 20:58:48 +00:00
|
|
|
dispatch(connectionFailed(connection, err, ''));
|
2016-12-05 15:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsubscribes connection instance from CONNECTION_ESTABLISHED
|
|
|
|
* and CONNECTION_FAILED events.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function unsubscribe() {
|
|
|
|
connection.removeEventListener(
|
2016-10-05 14:36:59 +00:00
|
|
|
JitsiConnectionEvents.CONNECTION_ESTABLISHED,
|
2017-01-31 20:58:48 +00:00
|
|
|
_onConnectionEstablished);
|
2016-12-05 15:14:50 +00:00
|
|
|
connection.removeEventListener(
|
2016-10-05 14:36:59 +00:00
|
|
|
JitsiConnectionEvents.CONNECTION_FAILED,
|
2017-01-31 20:58:48 +00:00
|
|
|
_onConnectionFailed);
|
2016-12-05 15:14:50 +00:00
|
|
|
}
|
2016-10-05 14:36:59 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Closes connection.
|
|
|
|
*
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
export function disconnect() {
|
2017-02-03 18:50:06 +00:00
|
|
|
return (dispatch: Dispatch<*>, getState: Function) => {
|
2016-10-05 14:36:59 +00:00
|
|
|
const state = getState();
|
2016-12-12 00:29:13 +00:00
|
|
|
const conference = state['features/base/conference'].conference;
|
|
|
|
const connection = state['features/base/connection'].connection;
|
2016-10-05 14:36:59 +00:00
|
|
|
|
|
|
|
let promise;
|
|
|
|
|
|
|
|
// Leave the conference.
|
|
|
|
if (conference) {
|
|
|
|
// 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.
|
|
|
|
dispatch(conferenceWillLeave(conference));
|
|
|
|
|
|
|
|
promise = conference.leave();
|
|
|
|
} else {
|
|
|
|
promise = Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disconnect the connection.
|
|
|
|
if (connection) {
|
|
|
|
promise = promise.then(() => connection.disconnect());
|
|
|
|
}
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets connection domain.
|
|
|
|
*
|
|
|
|
* @param {string} domain - Domain name.
|
|
|
|
* @returns {{
|
|
|
|
* type: SET_DOMAIN,
|
|
|
|
* domain: string
|
|
|
|
* }}
|
|
|
|
*/
|
2017-02-03 18:50:06 +00:00
|
|
|
export function setDomain(domain: string) {
|
2016-10-05 14:36:59 +00:00
|
|
|
return {
|
|
|
|
type: SET_DOMAIN,
|
|
|
|
domain
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an action for when the signaling connection has been lost.
|
|
|
|
*
|
2016-12-05 15:14:50 +00:00
|
|
|
* @param {JitsiConnection} connection - The JitsiConnection which disconnected.
|
2016-10-05 14:36:59 +00:00
|
|
|
* @param {string} message - Error message.
|
|
|
|
* @private
|
|
|
|
* @returns {{
|
|
|
|
* type: CONNECTION_DISCONNECTED,
|
|
|
|
* connection: JitsiConnection,
|
|
|
|
* message: string
|
|
|
|
* }}
|
|
|
|
*/
|
2017-02-03 18:50:06 +00:00
|
|
|
function _connectionDisconnected(connection, message: string) {
|
2016-10-05 14:36:59 +00:00
|
|
|
return {
|
|
|
|
type: CONNECTION_DISCONNECTED,
|
|
|
|
connection,
|
|
|
|
message
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an action for when the signaling connection has been established.
|
|
|
|
*
|
2016-12-05 15:14:50 +00:00
|
|
|
* @param {JitsiConnection} connection - The JitsiConnection which was
|
|
|
|
* established.
|
|
|
|
* @returns {{
|
|
|
|
* type: CONNECTION_ESTABLISHED,
|
|
|
|
* connection: JitsiConnection
|
|
|
|
* }}
|
2017-01-31 20:58:48 +00:00
|
|
|
* @public
|
2016-10-05 14:36:59 +00:00
|
|
|
*/
|
2017-01-31 20:58:48 +00:00
|
|
|
export function connectionEstablished(connection: Object) {
|
2016-10-05 14:36:59 +00:00
|
|
|
return {
|
|
|
|
type: CONNECTION_ESTABLISHED,
|
|
|
|
connection
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an action for when the signaling connection could not be created.
|
|
|
|
*
|
2016-12-05 15:14:50 +00:00
|
|
|
* @param {JitsiConnection} connection - The JitsiConnection which failed.
|
2017-01-31 20:58:48 +00:00
|
|
|
* @param {string} error - Error.
|
|
|
|
* @param {string} errorMessage - Error message.
|
2016-12-05 15:14:50 +00:00
|
|
|
* @returns {{
|
|
|
|
* type: CONNECTION_FAILED,
|
|
|
|
* connection: JitsiConnection,
|
2017-01-31 20:58:48 +00:00
|
|
|
* error: string,
|
|
|
|
* errorMessage: string
|
2016-12-05 15:14:50 +00:00
|
|
|
* }}
|
2017-01-31 20:58:48 +00:00
|
|
|
* @public
|
2016-10-05 14:36:59 +00:00
|
|
|
*/
|
2017-01-31 20:58:48 +00:00
|
|
|
export function connectionFailed(
|
|
|
|
connection: Object, error: string, errorMessage: string) {
|
2016-10-05 14:36:59 +00:00
|
|
|
return {
|
|
|
|
type: CONNECTION_FAILED,
|
2016-12-05 15:14:50 +00:00
|
|
|
connection,
|
2017-01-31 20:58:48 +00:00
|
|
|
error,
|
|
|
|
errorMessage
|
2016-10-05 14:36:59 +00:00
|
|
|
};
|
|
|
|
}
|