@@ -95,6 +95,9 @@ function ChatProxy(service) {
9595 this . _isLogout = false ;
9696
9797 this . _checkConnectionTimer = undefined ;
98+ // this._checkConnectionPingTimer = undefined; //artan: 13/09/2022 at 16:30
99+ this . _checkExpiredSessionTimer = undefined ;
100+ this . _sessionHasExpired = false ;
98101 this . _pings = { } ;
99102 //
100103 this . helpers = new Helpers ( ) ;
@@ -149,6 +152,7 @@ function ChatProxy(service) {
149152 * - onLastUserActivityListener (userId, seconds)
150153 * - onDisconnectedListener
151154 * - onReconnectListener
155+ * - onSessionExpiredListener
152156 */
153157
154158 /**
@@ -776,6 +780,7 @@ ChatProxy.prototype = {
776780 }
777781
778782 if ( ! self . isConnected && typeof self . onReconnectFailedListener === 'function' ) {
783+ // TODO: investigate problem reconnect field
779784 Utils . safeCallbackCall ( self . onReconnectFailedListener , err ) ;
780785 }
781786
@@ -796,7 +801,50 @@ ChatProxy.prototype = {
796801 self . connection . XAddTrackedHandler ( self . _onIQ , null , 'iq' ) ;
797802 self . connection . XAddTrackedHandler ( self . _onSystemMessageListener , null , 'message' , 'headline' ) ;
798803 self . connection . XAddTrackedHandler ( self . _onMessageErrorListener , null , 'message' , 'error' ) ;
799-
804+
805+ // var noTimerId = typeof self._checkConnectionPingTimer === 'undefined'; //artan: 13/09/2022 at 16:30
806+ // noTimerId = config.pingLocalhostTimeInterval === 0 ? false : noTimerId;
807+ // if (noTimerId) {
808+ // Utils.QBLog('[QBChat]', 'Init ping to localhost at ', Utils.getCurrentTime());
809+ // self._checkConnectionPingTimer = setInterval(function() {
810+ // self._ping(userJid,
811+ // function(error) {
812+ // if (error) {
813+ // Utils.QBLog('[QBChat]',
814+ // 'Local Ping: ',
815+ // 'failed, at ', Utils.getCurrentTime(),
816+ // ' error: ', error);
817+ // } else {
818+ // Utils.QBLog('[QBChat]',
819+ // 'Local Ping: ',
820+ // 'ok, at ', Utils.getCurrentTime());
821+ // }
822+ // });
823+ // }, config.pingLocalhostTimeInterval * 1000);
824+ // }
825+
826+ if ( typeof self . onSessionExpiredListener === 'function' ) {
827+ var noExpiredSessionTimerId = typeof self . _checkExpiredSessionTimer === 'undefined' ;
828+ if ( noExpiredSessionTimerId ) {
829+ Utils . QBLog ( '[QBChat]' , 'Init timer for check session expired at ' , ( ( new Date ( ) ) . toTimeString ( ) . split ( ' ' ) [ 0 ] ) ) ;
830+ self . _checkExpiredSessionTimer = setInterval ( function ( ) {
831+ var timeNow = new Date ( ) ;
832+ if ( typeof config . qbTokenExpirationDate !== 'undefined' ) {
833+ var timeLag = Math . round ( ( timeNow . getTime ( ) - config . qbTokenExpirationDate . getTime ( ) ) / ( 1000 * 60 ) ) ;
834+ //artan 25-08-2022
835+ // TODO: need to delete in task [CROS-815]
836+ console . log ( 'timeLag: ' , timeLag ) ;
837+ if ( timeLag >= 0 ) {
838+ self . _sessionHasExpired = true ;
839+ Utils . safeCallbackCall ( self . onSessionExpiredListener , null ) ;
840+ }
841+ }
842+ // TODO: in task [CROS-815], may by need to change 5 * 1000 to config.liveSessionInterval * 1000
843+ } , 5 * 1000 ) ;
844+
845+ }
846+ }
847+
800848 self . _postConnectActions ( function ( roster ) {
801849 callback ( null , roster ) ;
802850 } , isInitialConnect ) ;
@@ -917,10 +965,13 @@ ChatProxy.prototype = {
917965 _postConnectActions : function ( callback , isInitialConnect ) {
918966 Utils . QBLog ( '[QBChat]' , 'Status.CONNECTED at ' + chatUtils . getLocalTime ( ) ) ;
919967
920- var self = this ,
921- xmppClient = Utils . getEnv ( ) . browser ? self . connection : self . Client ,
922- presence = Utils . getEnv ( ) . browser ? $pres ( ) : chatUtils . createStanza ( XMPP . Stanza , null , 'presence' ) ;
923-
968+ var self = this ;
969+ var isBrowser = Utils . getEnv ( ) . browser ;
970+ var xmppClient = isBrowser ? self . connection : self . Client ;
971+ var presence = isBrowser ?
972+ $pres ( ) :
973+ chatUtils . createStanza ( XMPP . Stanza , null , 'presence' ) ;
974+
924975 if ( config . streamManagement . enable && config . chatProtocol . active === 2 ) {
925976 self . streamManagement . enable ( self . connection , null ) ;
926977 self . streamManagement . sentMessageCallback = self . _sentMessageCallback ;
@@ -930,6 +981,7 @@ ChatProxy.prototype = {
930981
931982 self . isConnected = true ;
932983 self . _isConnecting = false ;
984+ self . _sessionHasExpired = false ;
933985
934986 self . _enableCarbons ( ) ;
935987
@@ -958,23 +1010,30 @@ ChatProxy.prototype = {
9581010 } ,
9591011
9601012 _establishConnection : function ( params ) {
1013+ // TODO: must to call if only qbTokenExpirationDate is not expried
9611014 var self = this ;
962-
1015+ Utils . QBLog ( '[QBChat]' , '_establishConnection called' ) ;
9631016 if ( self . _isLogout || self . _checkConnectionTimer ) {
1017+ Utils . QBLog ( '[QBChat]' , '_establishConnection return' ) ;
9641018 return ;
9651019 }
9661020
9671021 var _connect = function ( ) {
968- if ( ! self . isConnected && ! self . _isConnecting ) {
1022+ Utils . QBLog ( '[QBChat]' , 'call _connect() in _establishConnection ' ) ;
1023+ if ( ! self . isConnected && ! self . _isConnecting && ! self . _sessionHasExpired ) {
1024+ Utils . QBLog ( '[QBChat]' , 'call connect() again in _establishConnection ' ) ;
9691025 self . connect ( params ) ;
9701026 } else {
1027+ Utils . QBLog ( '[QBChat]' , 'stop timer in _establishConnection ' ) ;
9711028 clearInterval ( self . _checkConnectionTimer ) ;
9721029 self . _checkConnectionTimer = undefined ;
9731030 }
9741031 } ;
9751032
9761033 _connect ( ) ;
9771034
1035+
1036+ // TODO: investigate problem with interval connection
9781037 self . _checkConnectionTimer = setInterval ( function ( ) {
9791038 _connect ( ) ;
9801039 } , config . chatReconnectionTimeInterval * 1000 ) ;
@@ -988,6 +1047,7 @@ ChatProxy.prototype = {
9881047 * @returns {String } messageId - The current message id (was generated by SDK)
9891048 * */
9901049 send : function ( jid_or_user_id , message ) {
1050+ Utils . QBLog ( '[QBChat]' , 'Call send ' + JSON . stringify ( message ) ) ;
9911051 var self = this ,
9921052 builder = Utils . getEnv ( ) . browser ? $msg : XMPP . Stanza ;
9931053
@@ -1050,6 +1110,7 @@ ChatProxy.prototype = {
10501110 * @returns {String } messageId - The current message id (was generated by SDK)
10511111 * */
10521112 sendSystemMessage : function ( jid_or_user_id , message ) {
1113+ Utils . QBLog ( '[QBChat]' , 'Call sendSystemMessage ' + JSON . stringify ( message ) ) ;
10531114 var self = this ,
10541115 builder = Utils . getEnv ( ) . browser ? $msg : XMPP . Stanza ,
10551116 paramsCreateMsg = {
@@ -1098,6 +1159,7 @@ ChatProxy.prototype = {
10981159 * @param {String | Number } jid_or_user_id - Use opponent id or jid for 1 to 1 chat, and room jid for group chat.
10991160 * */
11001161 sendIsTypingStatus : function ( jid_or_user_id ) {
1162+ Utils . QBLog ( '[QBChat]' , 'Call sendIsTypingStatus ' ) ;
11011163 var self = this ,
11021164 stanzaParams = {
11031165 from : self . helpers . getUserCurrentJid ( ) ,
@@ -1127,6 +1189,7 @@ ChatProxy.prototype = {
11271189 * @param {String | Number } jid_or_user_id - Use opponent id or jid for 1 to 1 chat, and room jid for group chat.
11281190 * */
11291191 sendIsStopTypingStatus : function ( jid_or_user_id ) {
1192+ Utils . QBLog ( '[QBChat]' , 'Call sendIsStopTypingStatus ' ) ;
11301193 var self = this ,
11311194 stanzaParams = {
11321195 from : self . helpers . getUserCurrentJid ( ) ,
@@ -1159,6 +1222,7 @@ ChatProxy.prototype = {
11591222 * @param {Number } params.dialogId - The dialog id
11601223 * */
11611224 sendDeliveredStatus : function ( params ) {
1225+ Utils . QBLog ( '[QBChat]' , 'Call sendDeliveredStatus ' ) ;
11621226 var self = this ,
11631227 stanzaParams = {
11641228 type : 'chat' ,
@@ -1195,6 +1259,7 @@ ChatProxy.prototype = {
11951259 * @param {Number } params.dialogId - The dialog id
11961260 * */
11971261 sendReadStatus : function ( params ) {
1262+ Utils . QBLog ( '[QBChat]' , 'Call sendReadStatus ' + JSON . stringify ( params ) ) ;
11981263 var self = this ,
11991264 stanzaParams = {
12001265 type : 'chat' ,
@@ -1228,6 +1293,7 @@ ChatProxy.prototype = {
12281293 * @param {(Number|String) } jid_or_user_id - The user id or jid, that the last activity we want to know
12291294 * */
12301295 getLastUserActivity : function ( jid_or_user_id ) {
1296+ Utils . QBLog ( '[QBChat]' , 'Call getLastUserActivity ' ) ;
12311297 var iqParams ,
12321298 builder ,
12331299 iq ;
@@ -1254,7 +1320,47 @@ ChatProxy.prototype = {
12541320 }
12551321 } ,
12561322
1323+ _ping : function ( jid_or_user_id , callback ) {
1324+ var self = this ;
1325+ var id = this . helpers . getUniqueId ( 'ping' ) ;
1326+ var builder = Utils . getEnv ( ) . browser ? $iq : XMPP . Stanza ;
1327+ var to ;
1328+ var _callback ;
1329+ var stanza ;
1330+
1331+ //to = config.endpoints.chat;
1332+ to = 'http://localhost' ;
1333+ _callback = callback ;
1334+
1335+
1336+ var iqParams = {
1337+ from : this . helpers . getUserCurrentJid ( ) ,
1338+ id : id ,
1339+ to : to ,
1340+ type : 'get'
1341+ } ;
1342+ stanza = chatUtils . createStanza ( builder , iqParams , 'iq' ) ;
1343+ stanza . c ( 'ping' , { xmlns : "urn:xmpp:ping" } ) ;
1344+
1345+ var noAnswer = function ( ) {
1346+ _callback ( 'No answer' ) ;
1347+ self . _pings [ id ] = undefined ;
1348+ delete self . _pings [ id ] ;
1349+ } ;
1350+ if ( Utils . getEnv ( ) . browser ) {
1351+ this . connection . send ( stanza ) ;
1352+ } else {
1353+ this . Client . send ( stanza ) ;
1354+ }
1355+ this . _pings [ id ] = {
1356+ callback : _callback ,
1357+ interval : setTimeout ( noAnswer , config . pingTimeout * 1000 )
1358+ } ;
1359+ return id ;
1360+ } ,
1361+
12571362 ping : function ( jid_or_user_id , callback ) {
1363+ Utils . QBLog ( '[QBChat]' , 'Call ping ' ) ;
12581364 var self = this ;
12591365 var id = this . helpers . getUniqueId ( 'ping' ) ;
12601366 var builder = Utils . getEnv ( ) . browser ? $iq : XMPP . Stanza ;
@@ -1306,8 +1412,11 @@ ChatProxy.prototype = {
13061412 * @memberof QB.chat
13071413 * */
13081414 disconnect : function ( ) {
1415+ Utils . QBLog ( '[QBChat]' , 'Call disconnect ' ) ;
13091416 clearInterval ( this . _checkConnectionTimer ) ;
1417+ clearInterval ( this . _checkExpiredSessionTimer ) ;
13101418 this . _checkConnectionTimer = undefined ;
1419+ this . _checkExpiredSessionTimer = undefined ;
13111420 this . muc . joinedRooms = { } ;
13121421 this . _isLogout = true ;
13131422 this . helpers . setUserCurrentJid ( '' ) ;
0 commit comments