forked from jitsi/lib-jitsi-meet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JitsiConnection.js
170 lines (154 loc) · 5.84 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
var JitsiConference = require("./JitsiConference");
var XMPP = require("./modules/xmpp/xmpp");
var JitsiConnectionEvents = require("./JitsiConnectionEvents");
var JitsiConnectionErrors = require("./JitsiConnectionErrors");
var Statistics = require("./modules/statistics/statistics");
/**
* Creates 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
*/
function JitsiConnection(appID, token, options) {
this.appID = appID;
this.token = token;
this.options = options;
this.xmpp = new XMPP(options, token);
this.conferences = {};
this.retryOnFail = 0;
this.addEventListener(JitsiConnectionEvents.CONNECTION_ESTABLISHED,
function () {
this.retryOnFail = 3;
}.bind(this));
this.addEventListener(JitsiConnectionEvents.CONNECTION_FAILED,
function (errType, msg) {
Statistics.analytics.sendEvent('connection.failed.' + errType);
if(errType === JitsiConnectionErrors.OTHER_ERROR &&
(msg === "item-not-found" || msg === "host-unknown")) {
var reason = "connectionError." + msg.replace(/-/g,"_");
// FIXME: don't report the error if we are going to reload
this._reload(reason);
}
}.bind(this));
this.addEventListener(JitsiConnectionEvents.CONNECTION_DISCONNECTED,
function (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
if(msg)
Statistics.analytics.sendEvent(
'connection.disconnected.' + msg);
});
}
/**
* Connect the client with the server.
* @param options {object} connecting options
* (for example authentications parameters).
*/
JitsiConnection.prototype.connect = function (options) {
if(!options)
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);
}
/**
* Reloads the JitsiConnection instance and all related conferences
* @param reason {String} the reason for the reload.
*/
JitsiConnection.prototype._reload = function (reason) {
if(this.retryOnFail === 0)
return false;
Statistics.sendReloadEvent(reason);
this.retryOnFail--;
var states = {};
for(var name in this.conferences) {
states[name] = this.conferences[name].room.exportState();
this.conferences[name].leave(true);
}
this.connectionEstablishedHandler =
this._reloadConferences.bind(this, states);
this.addEventListener(JitsiConnectionEvents.CONNECTION_ESTABLISHED,
this.connectionEstablishedHandler);
this.xmpp.reload();
return true;
}
/**
* Reloads all conferences related to this JitsiConnection instance
* @param states {object} the exported states per conference
*/
JitsiConnection.prototype._reloadConferences = function (states) {
this.removeEventListener(JitsiConnectionEvents.CONNECTION_ESTABLISHED,
this.connectionEstablishedHandler);
this.connectionEstablishedHandler = null;
states = states || {};
for(var name in this.conferences) {
this.conferences[name]._init({roomState: states[name]});
this.conferences[name].join();
}
}
/**
* Disconnect the client from the server.
*/
JitsiConnection.prototype.disconnect = function () {
// 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.
var x = this.xmpp;
x.disconnect.apply(x, arguments);
}
/**
* 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) {
var conference
= new JitsiConference({name: name, config: options, connection: this});
this.conferences[name] = conference;
return conference;
}
/**
* 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;
};
module.exports = JitsiConnection;