forked from jitsi/lib-jitsi-meet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
JitsiConnection.js
188 lines (167 loc) · 5.95 KB
/
JitsiConnection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import JitsiConference from './JitsiConference';
import * as JitsiConnectionEvents from './JitsiConnectionEvents';
import Statistics from './modules/statistics/statistics';
import XMPP from './modules/xmpp/xmpp';
import {
CONNECTION_DISCONNECTED as ANALYTICS_CONNECTION_DISCONNECTED,
createConnectionFailedEvent
} from './service/statistics/AnalyticsEvents';
/**
* Creates a new connection object for the Jitsi Meet server side video
* conferencing service. Provides access to the JitsiConference interface.
* @param appID identification for the provider of Jitsi Meet video conferencing
* services.
* @param token the JWT token used to authenticate with the server(optional)
* @param options Object with properties / settings related to connection with
* the server.
* @constructor
*/
export default function JitsiConnection(appID, token, options) {
this.appID = appID;
this.token = token;
this.options = options;
this.xmpp = new XMPP(options, token);
/* eslint-disable max-params */
this.addEventListener(JitsiConnectionEvents.CONNECTION_FAILED,
(errType, msg, credentials, details) => {
Statistics.sendAnalyticsAndLog(
createConnectionFailedEvent(errType, msg, details));
});
/* eslint-enable max-params */
this.addEventListener(JitsiConnectionEvents.CONNECTION_DISCONNECTED,
msg => {
// we can see disconnects from normal tab closing of the browser
// and then there are no msgs, but we want to log only disconnects
// when there is real error
// XXX Do we need the difference in handling between the log and
// analytics event here?
if (msg) {
Statistics.sendAnalytics(
ANALYTICS_CONNECTION_DISCONNECTED,
{ message: msg });
}
Statistics.sendLog(
JSON.stringify(
{
id: ANALYTICS_CONNECTION_DISCONNECTED,
msg
}));
});
}
/**
* Connect the client with the server.
* @param options {object} connecting options
* (for example authentications parameters).
*/
JitsiConnection.prototype.connect = function(options = {}) {
this.xmpp.connect(options.id, options.password);
};
/**
* Attach to existing connection. Can be used for optimizations. For example:
* if the connection is created on the server we can attach to it and start
* using it.
*
* @param options {object} connecting options - rid, sid and jid.
*/
JitsiConnection.prototype.attach = function(options) {
this.xmpp.attach(options);
};
/**
* Disconnect the client from the server.
* @returns {Promise} - Resolves when the disconnect process is finished or rejects with an error.
*/
JitsiConnection.prototype.disconnect = function(...args) {
// XXX Forward any arguments passed to JitsiConnection.disconnect to
// XMPP.disconnect. For example, the caller of JitsiConnection.disconnect
// may optionally pass the event which triggered the disconnect in order to
// provide the implementation with finer-grained context.
return this.xmpp.disconnect(...args);
};
/**
* Returns the jid of the participant associated with the XMPP connection.
*
* @returns {string} The jid of the participant.
*/
JitsiConnection.prototype.getJid = function() {
return this.xmpp.getJid();
};
/**
* This method allows renewal of the tokens if they are expiring.
* @param token the new token.
*/
JitsiConnection.prototype.setToken = function(token) {
this.token = token;
};
/**
* Creates and joins new conference.
* @param name the name of the conference; if null - a generated name will be
* provided from the api
* @param options Object with properties / settings related to the conference
* that will be created.
* @returns {JitsiConference} returns the new conference object.
*/
JitsiConnection.prototype.initJitsiConference = function(name, options) {
return new JitsiConference({
name,
config: options,
connection: this
});
};
/**
* Subscribes the passed listener to the event.
* @param event {JitsiConnectionEvents} the connection event.
* @param listener {Function} the function that will receive the event
*/
JitsiConnection.prototype.addEventListener = function(event, listener) {
this.xmpp.addListener(event, listener);
};
/**
* Unsubscribes the passed handler.
* @param event {JitsiConnectionEvents} the connection event.
* @param listener {Function} the function that will receive the event
*/
JitsiConnection.prototype.removeEventListener = function(event, listener) {
this.xmpp.removeListener(event, listener);
};
/**
* Returns measured connectionTimes.
*/
JitsiConnection.prototype.getConnectionTimes = function() {
return this.xmpp.connectionTimes;
};
/**
* Adds new feature to the list of supported features for the local
* participant.
* @param {String} feature the name of the feature.
* @param {boolean} submit if true - the new list of features will be
* immediately submitted to the others.
*/
JitsiConnection.prototype.addFeature = function(feature, submit = false) {
return this.xmpp.caps.addFeature(feature, submit);
};
/**
* Removes a feature from the list of supported features for the local
* participant
* @param {String} feature the name of the feature.
* @param {boolean} submit if true - the new list of features will be
* immediately submitted to the others.
*/
JitsiConnection.prototype.removeFeature = function(feature, submit = false) {
return this.xmpp.caps.removeFeature(feature, submit);
};
/**
* Get object with internal logs.
*/
JitsiConnection.prototype.getLogs = function() {
const data = this.xmpp.getJingleLog();
const metadata = {};
metadata.time = new Date();
metadata.url = window.location.href;
metadata.ua = navigator.userAgent;
const log = this.xmpp.getXmppLog();
if (log) {
metadata.xmpp = log;
}
data.metadata = metadata;
return data;
};