|
@@ -11,12 +11,12 @@ var RTC = require("../RTC/RTC");
|
11
|
11
|
|
12
|
12
|
var authenticatedUser = false;
|
13
|
13
|
|
14
|
|
-function createConnection(bosh) {
|
|
14
|
+function createConnection(bosh, token) {
|
15
|
15
|
bosh = bosh || '/http-bind';
|
16
|
16
|
|
17
|
17
|
// Append token as URL param
|
18
|
|
- if (this.token) {
|
19
|
|
- bosh += (bosh.indexOf('?') == -1 ? '?' : '&') + 'token=' + this.token;
|
|
18
|
+ if (token) {
|
|
19
|
+ bosh += (bosh.indexOf('?') == -1 ? '?' : '&') + 'token=' + token;
|
20
|
20
|
}
|
21
|
21
|
|
22
|
22
|
return new Strophe.Connection(bosh);
|
|
@@ -33,7 +33,7 @@ function initStrophePlugins(XMPP) {
|
33
|
33
|
require("./strophe.logger")();
|
34
|
34
|
}
|
35
|
35
|
|
36
|
|
-function XMPP(options) {
|
|
36
|
+function XMPP(options, token) {
|
37
|
37
|
this.eventEmitter = new EventEmitter();
|
38
|
38
|
this.connection = null;
|
39
|
39
|
this.disconnectInProgress = false;
|
|
@@ -42,7 +42,7 @@ function XMPP(options) {
|
42
|
42
|
this.options = options;
|
43
|
43
|
initStrophePlugins(this);
|
44
|
44
|
|
45
|
|
- this.connection = createConnection(options.bosh);
|
|
45
|
+ this.connection = createConnection(options.bosh, token);
|
46
|
46
|
|
47
|
47
|
// Setup a disconnect on unload as a way to facilitate API consumers. It
|
48
|
48
|
// sounds like they would want that. A problem for them though may be if
|
|
@@ -54,8 +54,77 @@ function XMPP(options) {
|
54
|
54
|
|
55
|
55
|
XMPP.prototype.getConnection = function () { return this.connection; };
|
56
|
56
|
|
|
57
|
+/**
|
|
58
|
+ * Receive connection status changes and handles them.
|
|
59
|
+ * @password {string} the password passed in connect method
|
|
60
|
+ * @status the connection status
|
|
61
|
+ * @msg message
|
|
62
|
+ */
|
|
63
|
+XMPP.prototype.connectionHandler = function (password, status, msg) {
|
|
64
|
+ logger.log("(TIME) Strophe " + Strophe.getStatusString(status) +
|
|
65
|
+ (msg ? "[" + msg + "]" : "") + "\t:" + window.performance.now());
|
|
66
|
+ if (status === Strophe.Status.CONNECTED ||
|
|
67
|
+ status === Strophe.Status.ATTACHED) {
|
|
68
|
+ if (this.options.useStunTurn) {
|
|
69
|
+ this.connection.jingle.getStunAndTurnCredentials();
|
|
70
|
+ }
|
|
71
|
+
|
|
72
|
+ logger.info("My Jabber ID: " + this.connection.jid);
|
|
73
|
+
|
|
74
|
+ // Schedule ping ?
|
|
75
|
+ var pingJid = this.connection.domain;
|
|
76
|
+ this.connection.ping.hasPingSupport(
|
|
77
|
+ pingJid,
|
|
78
|
+ function (hasPing) {
|
|
79
|
+ if (hasPing)
|
|
80
|
+ this.connection.ping.startInterval(pingJid);
|
|
81
|
+ else
|
|
82
|
+ logger.warn("Ping NOT supported by " + pingJid);
|
|
83
|
+ }.bind(this));
|
|
84
|
+
|
|
85
|
+ if (password)
|
|
86
|
+ authenticatedUser = true;
|
|
87
|
+ if (this.connection && this.connection.connected &&
|
|
88
|
+ Strophe.getResourceFromJid(this.connection.jid)) {
|
|
89
|
+ // .connected is true while connecting?
|
|
90
|
+// this.connection.send($pres());
|
|
91
|
+ this.eventEmitter.emit(
|
|
92
|
+ JitsiConnectionEvents.CONNECTION_ESTABLISHED,
|
|
93
|
+ Strophe.getResourceFromJid(this.connection.jid));
|
|
94
|
+ }
|
|
95
|
+ } else if (status === Strophe.Status.CONNFAIL) {
|
|
96
|
+ if (msg === 'x-strophe-bad-non-anon-jid') {
|
|
97
|
+ this.anonymousConnectionFailed = true;
|
|
98
|
+ } else {
|
|
99
|
+ this.connectionFailed = true;
|
|
100
|
+ }
|
|
101
|
+ this.lastErrorMsg = msg;
|
|
102
|
+ } else if (status === Strophe.Status.DISCONNECTED) {
|
|
103
|
+ // Stop ping interval
|
|
104
|
+ this.connection.ping.stopInterval();
|
|
105
|
+ this.disconnectInProgress = false;
|
|
106
|
+ if (this.anonymousConnectionFailed) {
|
|
107
|
+ // prompt user for username and password
|
|
108
|
+ this.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_FAILED,
|
|
109
|
+ JitsiConnectionErrors.PASSWORD_REQUIRED);
|
|
110
|
+ } else if(this.connectionFailed) {
|
|
111
|
+ this.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_FAILED,
|
|
112
|
+ JitsiConnectionErrors.OTHER_ERROR,
|
|
113
|
+ msg ? msg : this.lastErrorMsg);
|
|
114
|
+ } else {
|
|
115
|
+ this.eventEmitter.emit(
|
|
116
|
+ JitsiConnectionEvents.CONNECTION_DISCONNECTED,
|
|
117
|
+ msg ? msg : this.lastErrorMsg);
|
|
118
|
+ }
|
|
119
|
+ } else if (status === Strophe.Status.AUTHFAIL) {
|
|
120
|
+ // wrong password or username, prompt user
|
|
121
|
+ this.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_FAILED,
|
|
122
|
+ JitsiConnectionErrors.PASSWORD_REQUIRED);
|
|
123
|
+
|
|
124
|
+ }
|
|
125
|
+}
|
|
126
|
+
|
57
|
127
|
XMPP.prototype._connect = function (jid, password) {
|
58
|
|
- var self = this;
|
59
|
128
|
// connection.connect() starts the connection process.
|
60
|
129
|
//
|
61
|
130
|
// As the connection process proceeds, the user supplied callback will
|
|
@@ -83,72 +152,24 @@ XMPP.prototype._connect = function (jid, password) {
|
83
|
152
|
// Status.DISCONNECTING - The connection is currently being terminated
|
84
|
153
|
// Status.ATTACHED - The connection has been attached
|
85
|
154
|
|
86
|
|
- var anonymousConnectionFailed = false;
|
87
|
|
- var connectionFailed = false;
|
88
|
|
- var lastErrorMsg;
|
89
|
|
- this.connection.connect(jid, password, function (status, msg) {
|
90
|
|
- logger.log("(TIME) Strophe " + Strophe.getStatusString(status) +
|
91
|
|
- (msg ? "[" + msg + "]" : "") + "\t:" + window.performance.now());
|
92
|
|
- if (status === Strophe.Status.CONNECTED) {
|
93
|
|
- if (self.options.useStunTurn) {
|
94
|
|
- self.connection.jingle.getStunAndTurnCredentials();
|
95
|
|
- }
|
96
|
|
-
|
97
|
|
- logger.info("My Jabber ID: " + self.connection.jid);
|
98
|
|
-
|
99
|
|
- // Schedule ping ?
|
100
|
|
- var pingJid = self.connection.domain;
|
101
|
|
- self.connection.ping.hasPingSupport(
|
102
|
|
- pingJid,
|
103
|
|
- function (hasPing) {
|
104
|
|
- if (hasPing)
|
105
|
|
- self.connection.ping.startInterval(pingJid);
|
106
|
|
- else
|
107
|
|
- logger.warn("Ping NOT supported by " + pingJid);
|
108
|
|
- }
|
109
|
|
- );
|
110
|
|
-
|
111
|
|
- if (password)
|
112
|
|
- authenticatedUser = true;
|
113
|
|
- if (self.connection && self.connection.connected &&
|
114
|
|
- Strophe.getResourceFromJid(self.connection.jid)) {
|
115
|
|
- // .connected is true while connecting?
|
116
|
|
-// self.connection.send($pres());
|
117
|
|
- self.eventEmitter.emit(
|
118
|
|
- JitsiConnectionEvents.CONNECTION_ESTABLISHED,
|
119
|
|
- Strophe.getResourceFromJid(self.connection.jid));
|
120
|
|
- }
|
121
|
|
- } else if (status === Strophe.Status.CONNFAIL) {
|
122
|
|
- if (msg === 'x-strophe-bad-non-anon-jid') {
|
123
|
|
- anonymousConnectionFailed = true;
|
124
|
|
- } else {
|
125
|
|
- connectionFailed = true;
|
126
|
|
- }
|
127
|
|
- lastErrorMsg = msg;
|
128
|
|
- } else if (status === Strophe.Status.DISCONNECTED) {
|
129
|
|
- // Stop ping interval
|
130
|
|
- self.connection.ping.stopInterval();
|
131
|
|
- self.disconnectInProgress = false;
|
132
|
|
- if (anonymousConnectionFailed) {
|
133
|
|
- // prompt user for username and password
|
134
|
|
- self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_FAILED,
|
135
|
|
- JitsiConnectionErrors.PASSWORD_REQUIRED);
|
136
|
|
- } else if(connectionFailed) {
|
137
|
|
- self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_FAILED,
|
138
|
|
- JitsiConnectionErrors.OTHER_ERROR,
|
139
|
|
- msg ? msg : lastErrorMsg);
|
140
|
|
- } else {
|
141
|
|
- self.eventEmitter.emit(
|
142
|
|
- JitsiConnectionEvents.CONNECTION_DISCONNECTED,
|
143
|
|
- msg ? msg : lastErrorMsg);
|
144
|
|
- }
|
145
|
|
- } else if (status === Strophe.Status.AUTHFAIL) {
|
146
|
|
- // wrong password or username, prompt user
|
147
|
|
- self.eventEmitter.emit(JitsiConnectionEvents.CONNECTION_FAILED,
|
148
|
|
- JitsiConnectionErrors.PASSWORD_REQUIRED);
|
|
155
|
+ this.anonymousConnectionFailed = false;
|
|
156
|
+ this.connectionFailed = false;
|
|
157
|
+ this.lastErrorMsg;
|
|
158
|
+ this.connection.connect(jid, password,
|
|
159
|
+ this.connectionHandler.bind(this, password));
|
|
160
|
+}
|
149
|
161
|
|
150
|
|
- }
|
151
|
|
- });
|
|
162
|
+/**
|
|
163
|
+ * Attach to existing connection. Can be used for optimizations. For example:
|
|
164
|
+ * if the connection is created on the server we can attach to it and start
|
|
165
|
+ * using it.
|
|
166
|
+ *
|
|
167
|
+ * @param options {object} connecting options - rid, sid, jid and password.
|
|
168
|
+ */
|
|
169
|
+ XMPP.prototype.attach = function (options) {
|
|
170
|
+ logger.log("(TIME) Strophe attaching\t:" + window.performance.now(),options, options.jid);
|
|
171
|
+ this.connection.attach(options.jid, options.sid, parseInt(options.rid,10)+1,
|
|
172
|
+ this.connectionHandler.bind(this, options.password));
|
152
|
173
|
}
|
153
|
174
|
|
154
|
175
|
XMPP.prototype.connect = function (jid, password) {
|