@@ -95,6 +95,9 @@ function ChatProxy(service) {
95
95
this . _isLogout = false ;
96
96
97
97
this . _checkConnectionTimer = undefined ;
98
+ // this._checkConnectionPingTimer = undefined; //artan: 13/09/2022 at 16:30
99
+ this . _checkExpiredSessionTimer = undefined ;
100
+ this . _sessionHasExpired = false ;
98
101
this . _pings = { } ;
99
102
//
100
103
this . helpers = new Helpers ( ) ;
@@ -149,6 +152,7 @@ function ChatProxy(service) {
149
152
* - onLastUserActivityListener (userId, seconds)
150
153
* - onDisconnectedListener
151
154
* - onReconnectListener
155
+ * - onSessionExpiredListener
152
156
*/
153
157
154
158
/**
@@ -776,6 +780,7 @@ ChatProxy.prototype = {
776
780
}
777
781
778
782
if ( ! self . isConnected && typeof self . onReconnectFailedListener === 'function' ) {
783
+ // TODO: investigate problem reconnect field
779
784
Utils . safeCallbackCall ( self . onReconnectFailedListener , err ) ;
780
785
}
781
786
@@ -796,7 +801,50 @@ ChatProxy.prototype = {
796
801
self . connection . XAddTrackedHandler ( self . _onIQ , null , 'iq' ) ;
797
802
self . connection . XAddTrackedHandler ( self . _onSystemMessageListener , null , 'message' , 'headline' ) ;
798
803
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
+
800
848
self . _postConnectActions ( function ( roster ) {
801
849
callback ( null , roster ) ;
802
850
} , isInitialConnect ) ;
@@ -917,10 +965,13 @@ ChatProxy.prototype = {
917
965
_postConnectActions : function ( callback , isInitialConnect ) {
918
966
Utils . QBLog ( '[QBChat]' , 'Status.CONNECTED at ' + chatUtils . getLocalTime ( ) ) ;
919
967
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
+
924
975
if ( config . streamManagement . enable && config . chatProtocol . active === 2 ) {
925
976
self . streamManagement . enable ( self . connection , null ) ;
926
977
self . streamManagement . sentMessageCallback = self . _sentMessageCallback ;
@@ -930,6 +981,7 @@ ChatProxy.prototype = {
930
981
931
982
self . isConnected = true ;
932
983
self . _isConnecting = false ;
984
+ self . _sessionHasExpired = false ;
933
985
934
986
self . _enableCarbons ( ) ;
935
987
@@ -958,23 +1010,30 @@ ChatProxy.prototype = {
958
1010
} ,
959
1011
960
1012
_establishConnection : function ( params ) {
1013
+ // TODO: must to call if only qbTokenExpirationDate is not expried
961
1014
var self = this ;
962
-
1015
+ Utils . QBLog ( '[QBChat]' , '_establishConnection called' ) ;
963
1016
if ( self . _isLogout || self . _checkConnectionTimer ) {
1017
+ Utils . QBLog ( '[QBChat]' , '_establishConnection return' ) ;
964
1018
return ;
965
1019
}
966
1020
967
1021
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 ' ) ;
969
1025
self . connect ( params ) ;
970
1026
} else {
1027
+ Utils . QBLog ( '[QBChat]' , 'stop timer in _establishConnection ' ) ;
971
1028
clearInterval ( self . _checkConnectionTimer ) ;
972
1029
self . _checkConnectionTimer = undefined ;
973
1030
}
974
1031
} ;
975
1032
976
1033
_connect ( ) ;
977
1034
1035
+
1036
+ // TODO: investigate problem with interval connection
978
1037
self . _checkConnectionTimer = setInterval ( function ( ) {
979
1038
_connect ( ) ;
980
1039
} , config . chatReconnectionTimeInterval * 1000 ) ;
@@ -988,6 +1047,7 @@ ChatProxy.prototype = {
988
1047
* @returns {String } messageId - The current message id (was generated by SDK)
989
1048
* */
990
1049
send : function ( jid_or_user_id , message ) {
1050
+ Utils . QBLog ( '[QBChat]' , 'Call send ' + JSON . stringify ( message ) ) ;
991
1051
var self = this ,
992
1052
builder = Utils . getEnv ( ) . browser ? $msg : XMPP . Stanza ;
993
1053
@@ -1050,6 +1110,7 @@ ChatProxy.prototype = {
1050
1110
* @returns {String } messageId - The current message id (was generated by SDK)
1051
1111
* */
1052
1112
sendSystemMessage : function ( jid_or_user_id , message ) {
1113
+ Utils . QBLog ( '[QBChat]' , 'Call sendSystemMessage ' + JSON . stringify ( message ) ) ;
1053
1114
var self = this ,
1054
1115
builder = Utils . getEnv ( ) . browser ? $msg : XMPP . Stanza ,
1055
1116
paramsCreateMsg = {
@@ -1098,6 +1159,7 @@ ChatProxy.prototype = {
1098
1159
* @param {String | Number } jid_or_user_id - Use opponent id or jid for 1 to 1 chat, and room jid for group chat.
1099
1160
* */
1100
1161
sendIsTypingStatus : function ( jid_or_user_id ) {
1162
+ Utils . QBLog ( '[QBChat]' , 'Call sendIsTypingStatus ' ) ;
1101
1163
var self = this ,
1102
1164
stanzaParams = {
1103
1165
from : self . helpers . getUserCurrentJid ( ) ,
@@ -1127,6 +1189,7 @@ ChatProxy.prototype = {
1127
1189
* @param {String | Number } jid_or_user_id - Use opponent id or jid for 1 to 1 chat, and room jid for group chat.
1128
1190
* */
1129
1191
sendIsStopTypingStatus : function ( jid_or_user_id ) {
1192
+ Utils . QBLog ( '[QBChat]' , 'Call sendIsStopTypingStatus ' ) ;
1130
1193
var self = this ,
1131
1194
stanzaParams = {
1132
1195
from : self . helpers . getUserCurrentJid ( ) ,
@@ -1159,6 +1222,7 @@ ChatProxy.prototype = {
1159
1222
* @param {Number } params.dialogId - The dialog id
1160
1223
* */
1161
1224
sendDeliveredStatus : function ( params ) {
1225
+ Utils . QBLog ( '[QBChat]' , 'Call sendDeliveredStatus ' ) ;
1162
1226
var self = this ,
1163
1227
stanzaParams = {
1164
1228
type : 'chat' ,
@@ -1195,6 +1259,7 @@ ChatProxy.prototype = {
1195
1259
* @param {Number } params.dialogId - The dialog id
1196
1260
* */
1197
1261
sendReadStatus : function ( params ) {
1262
+ Utils . QBLog ( '[QBChat]' , 'Call sendReadStatus ' + JSON . stringify ( params ) ) ;
1198
1263
var self = this ,
1199
1264
stanzaParams = {
1200
1265
type : 'chat' ,
@@ -1228,6 +1293,7 @@ ChatProxy.prototype = {
1228
1293
* @param {(Number|String) } jid_or_user_id - The user id or jid, that the last activity we want to know
1229
1294
* */
1230
1295
getLastUserActivity : function ( jid_or_user_id ) {
1296
+ Utils . QBLog ( '[QBChat]' , 'Call getLastUserActivity ' ) ;
1231
1297
var iqParams ,
1232
1298
builder ,
1233
1299
iq ;
@@ -1254,7 +1320,47 @@ ChatProxy.prototype = {
1254
1320
}
1255
1321
} ,
1256
1322
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
+
1257
1362
ping : function ( jid_or_user_id , callback ) {
1363
+ Utils . QBLog ( '[QBChat]' , 'Call ping ' ) ;
1258
1364
var self = this ;
1259
1365
var id = this . helpers . getUniqueId ( 'ping' ) ;
1260
1366
var builder = Utils . getEnv ( ) . browser ? $iq : XMPP . Stanza ;
@@ -1306,8 +1412,11 @@ ChatProxy.prototype = {
1306
1412
* @memberof QB.chat
1307
1413
* */
1308
1414
disconnect : function ( ) {
1415
+ Utils . QBLog ( '[QBChat]' , 'Call disconnect ' ) ;
1309
1416
clearInterval ( this . _checkConnectionTimer ) ;
1417
+ clearInterval ( this . _checkExpiredSessionTimer ) ;
1310
1418
this . _checkConnectionTimer = undefined ;
1419
+ this . _checkExpiredSessionTimer = undefined ;
1311
1420
this . muc . joinedRooms = { } ;
1312
1421
this . _isLogout = true ;
1313
1422
this . helpers . setUserCurrentJid ( '' ) ;
0 commit comments