Skip to content

Commit 2340a17

Browse files
authored
Merge pull request #320 from QuickBlox/dev.xmpp-client-connection
Dev.xmpp_client_connection
2 parents 6fd0a5e + 33991e1 commit 2340a17

File tree

4 files changed

+85
-107
lines changed

4 files changed

+85
-107
lines changed

quickblox.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/modules/chat/qbChat.js

Lines changed: 70 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,15 @@ function ChatProxy(service) {
5454
// nativescript-xmpp-client
5555
if (Utils.getEnv().nativescript) {
5656
self.Client = new XMPP.Client({
57-
'websocket': { 'url': config.chatProtocol.websocket },
57+
'websocket': {
58+
'url': config.chatProtocol.websocket
59+
},
5860
'autostart': false,
5961
'reconnect': true
6062
});
6163
// node-xmpp-client
6264
} else if (Utils.getEnv().node) {
6365
self.Client = new XMPP({
64-
6566
'autostart': false,
6667
'reconnect': true
6768
});
@@ -79,6 +80,9 @@ function ChatProxy(service) {
7980
}
8081

8182
this.service = service;
83+
84+
// Check the chat connection (return true/false)
85+
this.isConnected = false;
8286

8387
this._isLogout = false;
8488
this._isDisconnected = false;
@@ -648,7 +652,6 @@ ChatProxy.prototype = {
648652
* @param {String} params.jid - Connect to the chat by user jid (use instead params.userId and params.email)
649653
* @param {String} params.email - Connect to the chat by user's email (use instead params.userId and params.jid)
650654
* @param {String} params.password - The user's password or session token
651-
* @param {Boolean} [params.connectWithoutGettingRoster=false] - true if you don't need to get roster of subscriptions
652655
* @param {chatConnectCallback} callback - The chatConnectCallback callback
653656
* */
654657
connect: function(params, callback) {
@@ -658,7 +661,7 @@ ChatProxy.prototype = {
658661
* @param {Object} error - The error object
659662
* @param {(Object|Boolean)} response - Object of subscribed users (roster) or empty body.
660663
* */
661-
Utils.QBLog('[Chat]', 'connect', params);
664+
Utils.QBLog('[Chat]', 'Connect with parameters ' + JSON.stringify(params));
662665

663666
var self = this,
664667
err, rooms;
@@ -733,25 +736,13 @@ ChatProxy.prototype = {
733736
self._enableCarbons();
734737

735738
if (typeof callback === 'function') {
736-
if (params.connectWithoutGettingRoster) {
737-
// connected and return nothing as result
738-
callback(null, undefined);
739-
// get the roster and save
740-
self.roster.get(function(contacts) {
741-
self.roster.contacts = contacts;
742-
// send first presence if user is online
743-
self.connection.send($pres());
744-
});
745-
} else {
746-
// get the roster and save
747-
self.roster.get(function(contacts) {
748-
self.roster.contacts = contacts;
749-
// send first presence if user is online
750-
self.connection.send($pres());
751-
// connected and return roster as result
752-
callback(null, self.roster.contacts);
753-
});
754-
}
739+
self.roster.get(function(contacts) {
740+
self.roster.contacts = contacts;
741+
// send first presence if user is online
742+
self.connection.send($pres());
743+
744+
callback(null, self.roster.contacts);
745+
});
755746
} else {
756747
// recover the joined rooms
757748
rooms = Object.keys(self.muc.joinedRooms);
@@ -797,80 +788,54 @@ ChatProxy.prototype = {
797788

798789
/** connect for node */
799790
if(!Utils.getEnv().browser) {
800-
self.Client.options.jid = userJid;
801-
self.Client.options.password = params.password;
791+
if (self.isConnected) {
792+
callback(null, self.roster.contacts);
793+
return;
794+
}
802795

803-
/** HANDLERS */
804-
self.Client.on('auth', function () {
805-
Utils.QBLog('[Chat]', 'Status.CONNECTED at ' + chatUtils.getLocalTime());
796+
self.Client.on('connect', function() {
797+
Utils.QBLog('[Chat]', 'CONNECT - ' + chatUtils.getLocalTime());
806798
});
799+
800+
self.Client.on('reconnect', function() {
801+
Utils.QBLog('[Chat]', 'RECONNECT - ' + chatUtils.getLocalTime());
807802

808-
self.Client.on('online', function () {
809-
Utils.QBLog('[Chat]', 'Status.CONNECTED at ' + chatUtils.getLocalTime());
803+
if (typeof self.onReconnectListener === 'function') {
804+
Utils.safeCallbackCall(self.onReconnectListener);
805+
}
810806

811-
if(config.streamManagement.enable){
807+
self.isConnected = false;
808+
});
809+
810+
self.Client.on('online', function() {
811+
Utils.QBLog('[Chat]', 'CONNECTED - ' + chatUtils.getLocalTime());
812+
813+
if (config.streamManagement.enable) {
812814
self.streamManagement.enable(self.Client, XMPP);
813815
self.streamManagement.sentMessageCallback = self._sentMessageCallback;
814816
}
815-
816-
self._isDisconnected = false;
817-
self._isLogout = false;
818-
817+
819818
self.helpers.setUserCurrentJid(self.helpers.userCurrentJid(self.Client));
820819

820+
self.isConnected = true;
821+
821822
if (typeof callback === 'function') {
822-
var presence = chatUtils.createStanza(XMPP.Stanza, null,'presence');
823-
824-
if (params.connectWithoutGettingRoster) {
825-
// connected and return nothing as result
826-
callback(null, undefined);
827-
// get the roster and save
828-
self.roster.get(function(contacts) {
829-
self.roster.contacts = contacts;
830-
// send first presence if user is online
831-
self.Client.send(presence);
832-
});
833-
} else {
834-
// get the roster and save
835-
self.roster.get(function(contacts) {
836-
self.roster.contacts = contacts;
837-
// send first presence if user is online
838-
self.Client.send(presence);
839-
// connected and return roster as result
840-
callback(null, self.roster.contacts);
841-
});
842-
}
843-
}
844-
});
845-
846-
self.Client.on('connect', function () {
847-
Utils.QBLog('[Chat] client is connected');
848-
self._enableCarbons();
849-
});
850-
851-
self.Client.on('reconnect', function () {
852-
Utils.QBLog('[Chat] client is reconnected');
853-
854-
self._isDisconnected = true;
855-
self._isLogout = true;
856-
});
857-
858-
self.Client.on('disconnect', function () {
859-
Utils.QBLog('[Chat] client is disconnected');
860-
823+
var presence = chatUtils.createStanza(XMPP.Stanza, null, 'presence');
861824

862-
// fire 'onDisconnectedListener' only once
863-
if (!self._isDisconnected && typeof self.onDisconnectedListener === 'function'){
864-
Utils.safeCallbackCall(self.onDisconnectedListener);
825+
self.roster.get(function(contacts) {
826+
self.roster.contacts = contacts;
827+
// send first presence if user is online
828+
self.Client.send(presence);
829+
830+
callback(null, self.roster.contacts);
831+
});
865832
}
866833

867-
self._isLogout = true;
868-
self._isDisconnected = true;
834+
self._enableCarbons();
869835
});
870-
871-
self.Client.on('stanza', function (stanza) {
872-
Utils.QBLog('[QBChat] RECV:', stanza.toString());
873-
836+
837+
self.Client.on('stanza', function(stanza) {
838+
Utils.QBLog('[Chat] RECV:', stanza.toString());
874839
/**
875840
* Detect typeof incoming stanza
876841
* and fire the Listener
@@ -879,37 +844,42 @@ ChatProxy.prototype = {
879844
self._onPresence(stanza);
880845
} else if (stanza.is('iq')) {
881846
self._onIQ(stanza);
882-
} else if(stanza.is('message')){
883-
if(stanza.attrs.type === 'headline') {
847+
} else if(stanza.is('message')) {
848+
if (stanza.attrs.type === 'headline') {
884849
self._onSystemMessageListener(stanza);
885-
}else if(stanza.attrs.type === 'error') {
850+
} else if(stanza.attrs.type === 'error') {
886851
self._onMessageErrorListener(stanza);
887852
} else {
888853
self._onMessage(stanza);
889854
}
890855
}
891856
});
857+
858+
self.Client.on('disconnect', function() {
859+
Utils.QBLog('[Chat]', 'DISCONNECT - ' + chatUtils.getLocalTime());
892860

893-
self.Client.on('offline', function () {
894-
Utils.QBLog('[QBChat] client goes offline');
861+
if (typeof self.onDisconnectedListener === 'function') {
862+
Utils.safeCallbackCall(self.onDisconnectedListener);
863+
}
895864

896-
self._isDisconnected = true;
897-
self._isLogout = true;
865+
self.isConnected = false;
866+
self.Client._events = {};
867+
self.Client._eventsCount = 0;
898868
});
899-
869+
900870
self.Client.on('error', function (e) {
901-
Utils.QBLog('[QBChat] client got error', e);
902-
903-
self._isDisconnected = true;
904-
self._isLogout = true;
905-
906-
err = Utils.getError(422, 'Status.ERROR - An error has occurred');
907-
908-
if(typeof callback === 'function') {
871+
Utils.QBLog('[Chat]', 'ERROR - ' + chatUtils.getLocalTime());
872+
err = Utils.getError(422, 'ERROR - An error has occurred');
873+
874+
if (typeof callback === 'function') {
909875
callback(err, null);
910876
}
877+
878+
self.isConnected = false;
911879
});
912880

881+
self.Client.options.jid = userJid;
882+
self.Client.options.password = params.password;
913883
self.Client.connect();
914884
}
915885
},
@@ -1193,7 +1163,7 @@ ChatProxy.prototype = {
11931163
this._isLogout = true;
11941164
this.helpers.setUserCurrentJid('');
11951165

1196-
if(Utils.getEnv().browser) {
1166+
if (Utils.getEnv().browser) {
11971167
this.connection.flush();
11981168
this.connection.disconnect();
11991169
} else {

src/qbConfig.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
var config = {
1515
version: '2.10.0',
16-
buildNumber: '1073',
16+
buildNumber: '1075',
1717
creds: {
1818
appId: '',
1919
authKey: '',

src/qbUtils.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ var Utils = {
4747
return window.navigator.userAgent;
4848
},
4949

50+
_getOSInfoFromNativeScript: function() {
51+
return (global && global.android ? 'Android' : 'iOS') + ' - NativeScript';
52+
},
53+
5054
getOS: function() {
5155
var self = this;
5256
var osName = 'An unknown OS';
@@ -68,18 +72,22 @@ var Utils = {
6872

6973
var platformInfo;
7074

71-
if(self.getEnv().browser) {
75+
if (self.getEnv().browser) {
7276
platformInfo = self._getOSInfoFromBrowser();
73-
} else if(self.getEnv().node) {
77+
} else if (self.getEnv().nativescript) {
78+
platformInfo = self._getOSInfoFromNativeScript();
79+
} else if (self.getEnv().node) {
7480
platformInfo = self._getOSInfoFromNodeJS();
7581
}
7682

77-
OS_LIST.forEach( function(osInfo) {
78-
osInfo.codeNames.forEach( function(codeName) {
83+
OS_LIST.forEach(function(osInfo) {
84+
osInfo.codeNames.forEach(function(codeName) {
7985
var index = platformInfo.indexOf(codeName);
8086

81-
if(index !== -1) {
87+
if (index !== -1) {
8288
osName = osInfo.osName;
89+
} else if (typeof platformInfo === 'string') {
90+
osName = platformInfo;
8391
}
8492
});
8593
});

0 commit comments

Comments
 (0)