@@ -54,14 +54,15 @@ function ChatProxy(service) {
54
54
// nativescript-xmpp-client
55
55
if ( Utils . getEnv ( ) . nativescript ) {
56
56
self . Client = new XMPP . Client ( {
57
- 'websocket' : { 'url' : config . chatProtocol . websocket } ,
57
+ 'websocket' : {
58
+ 'url' : config . chatProtocol . websocket
59
+ } ,
58
60
'autostart' : false ,
59
61
'reconnect' : true
60
62
} ) ;
61
63
// node-xmpp-client
62
64
} else if ( Utils . getEnv ( ) . node ) {
63
65
self . Client = new XMPP ( {
64
-
65
66
'autostart' : false ,
66
67
'reconnect' : true
67
68
} ) ;
@@ -79,6 +80,9 @@ function ChatProxy(service) {
79
80
}
80
81
81
82
this . service = service ;
83
+
84
+ // Check the chat connection (return true/false)
85
+ this . isConnected = false ;
82
86
83
87
this . _isLogout = false ;
84
88
this . _isDisconnected = false ;
@@ -648,7 +652,6 @@ ChatProxy.prototype = {
648
652
* @param {String } params.jid - Connect to the chat by user jid (use instead params.userId and params.email)
649
653
* @param {String } params.email - Connect to the chat by user's email (use instead params.userId and params.jid)
650
654
* @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
652
655
* @param {chatConnectCallback } callback - The chatConnectCallback callback
653
656
* */
654
657
connect : function ( params , callback ) {
@@ -658,7 +661,7 @@ ChatProxy.prototype = {
658
661
* @param {Object } error - The error object
659
662
* @param {(Object|Boolean) } response - Object of subscribed users (roster) or empty body.
660
663
* */
661
- Utils . QBLog ( '[Chat]' , 'connect' , params ) ;
664
+ Utils . QBLog ( '[Chat]' , 'Connect with parameters ' + JSON . stringify ( params ) ) ;
662
665
663
666
var self = this ,
664
667
err , rooms ;
@@ -733,25 +736,13 @@ ChatProxy.prototype = {
733
736
self . _enableCarbons ( ) ;
734
737
735
738
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
+ } ) ;
755
746
} else {
756
747
// recover the joined rooms
757
748
rooms = Object . keys ( self . muc . joinedRooms ) ;
@@ -797,80 +788,54 @@ ChatProxy.prototype = {
797
788
798
789
/** connect for node */
799
790
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
+ }
802
795
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 ( ) ) ;
806
798
} ) ;
799
+
800
+ self . Client . on ( 'reconnect' , function ( ) {
801
+ Utils . QBLog ( '[Chat]' , 'RECONNECT - ' + chatUtils . getLocalTime ( ) ) ;
807
802
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
+ }
810
806
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 ) {
812
814
self . streamManagement . enable ( self . Client , XMPP ) ;
813
815
self . streamManagement . sentMessageCallback = self . _sentMessageCallback ;
814
816
}
815
-
816
- self . _isDisconnected = false ;
817
- self . _isLogout = false ;
818
-
817
+
819
818
self . helpers . setUserCurrentJid ( self . helpers . userCurrentJid ( self . Client ) ) ;
820
819
820
+ self . isConnected = true ;
821
+
821
822
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' ) ;
861
824
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
+ } ) ;
865
832
}
866
833
867
- self . _isLogout = true ;
868
- self . _isDisconnected = true ;
834
+ self . _enableCarbons ( ) ;
869
835
} ) ;
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 ( ) ) ;
874
839
/**
875
840
* Detect typeof incoming stanza
876
841
* and fire the Listener
@@ -879,37 +844,42 @@ ChatProxy.prototype = {
879
844
self . _onPresence ( stanza ) ;
880
845
} else if ( stanza . is ( 'iq' ) ) {
881
846
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' ) {
884
849
self . _onSystemMessageListener ( stanza ) ;
885
- } else if ( stanza . attrs . type === 'error' ) {
850
+ } else if ( stanza . attrs . type === 'error' ) {
886
851
self . _onMessageErrorListener ( stanza ) ;
887
852
} else {
888
853
self . _onMessage ( stanza ) ;
889
854
}
890
855
}
891
856
} ) ;
857
+
858
+ self . Client . on ( 'disconnect' , function ( ) {
859
+ Utils . QBLog ( '[Chat]' , 'DISCONNECT - ' + chatUtils . getLocalTime ( ) ) ;
892
860
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
+ }
895
864
896
- self . _isDisconnected = true ;
897
- self . _isLogout = true ;
865
+ self . isConnected = false ;
866
+ self . Client . _events = { } ;
867
+ self . Client . _eventsCount = 0 ;
898
868
} ) ;
899
-
869
+
900
870
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' ) {
909
875
callback ( err , null ) ;
910
876
}
877
+
878
+ self . isConnected = false ;
911
879
} ) ;
912
880
881
+ self . Client . options . jid = userJid ;
882
+ self . Client . options . password = params . password ;
913
883
self . Client . connect ( ) ;
914
884
}
915
885
} ,
@@ -1193,7 +1163,7 @@ ChatProxy.prototype = {
1193
1163
this . _isLogout = true ;
1194
1164
this . helpers . setUserCurrentJid ( '' ) ;
1195
1165
1196
- if ( Utils . getEnv ( ) . browser ) {
1166
+ if ( Utils . getEnv ( ) . browser ) {
1197
1167
this . connection . flush ( ) ;
1198
1168
this . connection . disconnect ( ) ;
1199
1169
} else {
0 commit comments