|
406 | 406 | const hbRequests = (hbRequestsBySubscriptionKey !== null && hbRequestsBySubscriptionKey !== void 0 ? hbRequestsBySubscriptionKey : {})[heartbeatRequestKey]; |
407 | 407 | notifyRequestProcessing('start', [client], new Date().toISOString(), request); |
408 | 408 | if (!request) { |
409 | | - consoleLog(`Previous heartbeat request has been sent less than ${client.heartbeatInterval} seconds ago. Skipping...`); |
| 409 | + consoleLog(`Previous heartbeat request has been sent less than ${client.heartbeatInterval} seconds ago. Skipping...`, client); |
410 | 410 | let response; |
411 | 411 | let body; |
412 | 412 | // Pulling out previous response. |
|
855 | 855 | const { channels, channelGroups, response } = hbRequestsBySubscriptionKey[heartbeatRequestKey]; |
856 | 856 | aggregatedState = (_d = client.heartbeat.presenceState) !== null && _d !== void 0 ? _d : {}; |
857 | 857 | aggregated = |
858 | | - includesStrings(channels, client.heartbeat.channels) && |
859 | | - includesStrings(channelGroups, client.heartbeat.channelGroups); |
| 858 | + includesStrings(channels, channelsForAnnouncement) && |
| 859 | + includesStrings(channelGroups, channelGroupsForAnnouncement); |
860 | 860 | if (response) |
861 | 861 | failedPreviousRequest = response[0].status >= 400; |
862 | 862 | } |
| 863 | + // Find minimum heartbeat interval which maybe required to use. |
| 864 | + let minimumHeartbeatInterval = client.heartbeatInterval; |
| 865 | + for (const client of clients) { |
| 866 | + if (client.heartbeatInterval) |
| 867 | + minimumHeartbeatInterval = Math.min(minimumHeartbeatInterval, client.heartbeatInterval); |
| 868 | + } |
863 | 869 | if (aggregated) { |
864 | | - const expectedTimestamp = hbRequestsBySubscriptionKey[heartbeatRequestKey].timestamp + client.heartbeatInterval * 1000; |
| 870 | + const expectedTimestamp = hbRequestsBySubscriptionKey[heartbeatRequestKey].timestamp + minimumHeartbeatInterval * 1000; |
865 | 871 | const currentTimestamp = Date.now(); |
866 | 872 | // Check whether it is too soon to send request or not (5 is leeway which let send request a bit earlier). |
867 | 873 | // Request should be sent if previous attempt failed. |
868 | 874 | if (!failedPreviousRequest && currentTimestamp < expectedTimestamp && expectedTimestamp - currentTimestamp > 5000) |
869 | 875 | return undefined; |
870 | | - delete hbRequestsBySubscriptionKey[heartbeatRequestKey].response; |
871 | | - // Aggregate channels for similar clients which is pending for heartbeat. |
872 | | - for (const client of clients) { |
873 | | - const { heartbeat } = client; |
874 | | - if (heartbeat === undefined || client.clientIdentifier === event.clientIdentifier) |
875 | | - continue; |
876 | | - // Append presence state from the client (will override previously set value if already set). |
877 | | - if (heartbeat.presenceState) |
878 | | - aggregatedState = Object.assign(Object.assign({}, aggregatedState), heartbeat.presenceState); |
879 | | - channelGroupsForAnnouncement.push(...heartbeat.channelGroups.filter((channel) => !channelGroupsForAnnouncement.includes(channel))); |
880 | | - channelsForAnnouncement.push(...heartbeat.channels.filter((channel) => !channelsForAnnouncement.includes(channel))); |
881 | | - } |
| 876 | + } |
| 877 | + delete hbRequestsBySubscriptionKey[heartbeatRequestKey].response; |
| 878 | + // Aggregate channels for similar clients which is pending for heartbeat. |
| 879 | + for (const client of clients) { |
| 880 | + const { heartbeat } = client; |
| 881 | + if (heartbeat === undefined || client.clientIdentifier === event.clientIdentifier) |
| 882 | + continue; |
| 883 | + // Append presence state from the client (will override previously set value if already set). |
| 884 | + if (heartbeat.presenceState) |
| 885 | + aggregatedState = Object.assign(Object.assign({}, aggregatedState), heartbeat.presenceState); |
| 886 | + channelGroupsForAnnouncement.push(...heartbeat.channelGroups.filter((channel) => !channelGroupsForAnnouncement.includes(channel))); |
| 887 | + channelsForAnnouncement.push(...heartbeat.channels.filter((channel) => !channelsForAnnouncement.includes(channel))); |
882 | 888 | } |
883 | 889 | hbRequestsBySubscriptionKey[heartbeatRequestKey].channels = channelsForAnnouncement; |
884 | 890 | hbRequestsBySubscriptionKey[heartbeatRequestKey].channelGroups = channelGroupsForAnnouncement; |
|
1231 | 1237 | subscriptionKey: event.subscriptionKey, |
1232 | 1238 | userId: event.userId, |
1233 | 1239 | heartbeatInterval: event.heartbeatInterval, |
| 1240 | + newlyRegistered: true, |
1234 | 1241 | logVerbosity: event.logVerbosity, |
1235 | 1242 | offlineClientsCheckInterval: event.workerOfflineClientsCheckInterval, |
1236 | 1243 | unsubscribeOfflineClients: event.workerUnsubscribeOfflineClients, |
|
1330 | 1337 | subscription.channelGroupQuery = channelGroupQuery; |
1331 | 1338 | subscription.channelGroups = channelGroupsFromRequest(event.request); |
1332 | 1339 | } |
1333 | | - const { authKey, userId } = client; |
| 1340 | + let { authKey } = client; |
| 1341 | + const { userId } = client; |
1334 | 1342 | subscription.request = event.request; |
1335 | 1343 | subscription.filterExpression = ((_j = query['filter-expr']) !== null && _j !== void 0 ? _j : ''); |
1336 | 1344 | subscription.timetoken = ((_k = query.tt) !== null && _k !== void 0 ? _k : '0'); |
|
1341 | 1349 | client.userId = query.uuid; |
1342 | 1350 | client.pnsdk = query.pnsdk; |
1343 | 1351 | client.accessToken = event.token; |
| 1352 | + if (client.newlyRegistered && !authKey && client.authKey) |
| 1353 | + authKey = client.authKey; |
| 1354 | + client.newlyRegistered = false; |
1344 | 1355 | handleClientIdentityChangeIfRequired(client, userId, authKey); |
1345 | 1356 | }; |
1346 | 1357 | /** |
|
0 commit comments