Skip to content

Commit 8153d2d

Browse files
author
Austin Johnson
committed
Fix inconsistency around handling of credentials for each client
1 parent f6da65d commit 8153d2d

File tree

1 file changed

+103
-98
lines changed

1 file changed

+103
-98
lines changed

lex-web-ui/src/store/actions.js

Lines changed: 103 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -430,10 +430,10 @@ export default {
430430
**********************************************************************/
431431

432432
pollyGetBlob(context, text, format = 'text') {
433-
return context.dispatch('refreshAuthTokens')
433+
return context.dispatch('checkCredentialsForRefresh')
434434
.then(() => context.dispatch('getCredentials', context.state.config))
435-
.then((creds) => {
436-
pollyClient.config.credentials = creds;
435+
.then(() => {
436+
pollyClient.config.credentials = awsCredentials;
437437
const synthReq = pollyClient.synthesizeSpeech({
438438
Text: text,
439439
VoiceId: context.state.polly.voiceId,
@@ -933,70 +933,72 @@ export default {
933933
InstanceId: context.state.config.connect.instanceId,
934934
};
935935

936-
context.dispatch('refreshAuthTokens')
936+
context.dispatch('checkCredentialsForRefresh')
937937
.then(() => context.dispatch('getCredentials', context.state.config))
938-
.then((credentials) => {
938+
.then(() => {
939939
const bodyText = JSON.stringify(initiateChatRequest);
940940
const serviceInfo = {
941941
region: context.state.config.region,
942942
service: 'execute-api'
943943
};
944-
945-
const accessInfo = {
946-
access_key: credentials.accessKeyId,
947-
secret_key: credentials.secretAccessKey,
948-
session_token: credentials.sessionToken,
949-
}
950-
951-
var request = {
952-
url: context.state.config.connect.apiGatewayEndpoint,
953-
method: 'POST',
954-
mode: 'cors',
955-
data: bodyText
956-
}
944+
945+
Promise.resolve(awsCredentials).then((credentials) => {
946+
const accessInfo = {
947+
access_key: credentials.accessKeyId,
948+
secret_key: credentials.secretAccessKey,
949+
session_token: credentials.sessionToken,
950+
}
957951

958-
const signedRequest = Signer.sign(request, accessInfo, serviceInfo);
959-
960-
return fetch(signedRequest.url, signedRequest)
961-
.then(response => response.json())
962-
.then(json => json.data)
963-
.then((result) => {
964-
console.info('Live Chat Config Success:', result);
965-
context.commit('setLiveChatStatus', liveChatStatus.CONNECTING);
966-
function waitMessage(context, type, message) {
967-
context.commit('pushLiveChatMessage', {
968-
type,
969-
text: message,
970-
});
971-
};
972-
if (context.state.config.connect.waitingForAgentMessageIntervalSeconds > 0) {
973-
const intervalID = setInterval(waitMessage,
974-
1000 * context.state.config.connect.waitingForAgentMessageIntervalSeconds,
975-
context,
976-
'bot',
977-
context.state.config.connect.waitingForAgentMessage);
978-
console.info(`interval now set: ${intervalID}`);
979-
context.commit('setLiveChatIntervalId', intervalID);
952+
var request = {
953+
url: context.state.config.connect.apiGatewayEndpoint,
954+
method: 'POST',
955+
mode: 'cors',
956+
data: bodyText
980957
}
981-
liveChatSession = createLiveChatSession(result);
982-
console.info('Live Chat Session Created:', liveChatSession);
983-
initLiveChatHandlers(context, liveChatSession);
984-
console.info('Live Chat Handlers initialised:');
985-
return connectLiveChatSession(liveChatSession);
986-
})
987-
.then((response) => {
988-
console.info('live Chat session connection response', response);
989-
console.info('Live Chat Session CONNECTED:', liveChatSession);
990-
context.commit('setLiveChatStatus', liveChatStatus.ESTABLISHED);
991-
// context.commit('setLiveChatbotSession', liveChatSession);
992-
return Promise.resolve();
993-
})
994-
.catch((error) => {
995-
console.error("Error esablishing live chat");
996-
context.commit('setLiveChatStatus', liveChatStatus.ENDED);
997-
return Promise.resolve();
958+
959+
const signedRequest = Signer.sign(request, accessInfo, serviceInfo);
960+
961+
return fetch(signedRequest.url, signedRequest)
962+
.then(response => response.json())
963+
.then(json => json.data)
964+
.then((result) => {
965+
console.info('Live Chat Config Success:', result);
966+
context.commit('setLiveChatStatus', liveChatStatus.CONNECTING);
967+
function waitMessage(context, type, message) {
968+
context.commit('pushLiveChatMessage', {
969+
type,
970+
text: message,
971+
});
972+
};
973+
if (context.state.config.connect.waitingForAgentMessageIntervalSeconds > 0) {
974+
const intervalID = setInterval(waitMessage,
975+
1000 * context.state.config.connect.waitingForAgentMessageIntervalSeconds,
976+
context,
977+
'bot',
978+
context.state.config.connect.waitingForAgentMessage);
979+
console.info(`interval now set: ${intervalID}`);
980+
context.commit('setLiveChatIntervalId', intervalID);
981+
}
982+
liveChatSession = createLiveChatSession(result);
983+
console.info('Live Chat Session Created:', liveChatSession);
984+
initLiveChatHandlers(context, liveChatSession);
985+
console.info('Live Chat Handlers initialised:');
986+
return connectLiveChatSession(liveChatSession);
987+
})
988+
.then((response) => {
989+
console.info('live Chat session connection response', response);
990+
console.info('Live Chat Session CONNECTED:', liveChatSession);
991+
context.commit('setLiveChatStatus', liveChatStatus.ESTABLISHED);
992+
// context.commit('setLiveChatbotSession', liveChatSession);
993+
return Promise.resolve();
994+
})
995+
.catch((error) => {
996+
console.error("Error esablishing live chat");
997+
context.commit('setLiveChatStatus', liveChatStatus.ENDED);
998+
return Promise.resolve();
999+
});
9981000
});
999-
});
1001+
});
10001002
}
10011003
// If TalkDesk endpoint is available use
10021004
else if (context.state.config.connect.talkDeskWebsocketEndpoint) {
@@ -1182,6 +1184,7 @@ export default {
11821184
sessionToken: creds.SessionToken,
11831185
expiration: creds.Expiration,
11841186
};
1187+
awsCredentials = credentials;
11851188
if (lexClient) {
11861189
lexClient.refreshClient(region, credentials);
11871190
}
@@ -1204,7 +1207,7 @@ export default {
12041207
},
12051208
checkCredentialsForRefresh() {
12061209
if (awsCredentials) {
1207-
awsCredentials.then((res) => {
1210+
Promise.resolve(awsCredentials).then((res) => {
12081211
if (res.expiration) {
12091212
const expiration = new Date(res.expiration).getTime();
12101213
const now = Date.now();
@@ -1379,56 +1382,58 @@ export default {
13791382
*
13801383
**********************************************************************/
13811384
InitWebSocketConnect(context){
1382-
context.dispatch('getCredentials', context.state.config).then((credentials) => {
1385+
context.dispatch('getCredentials', context.state.config).then(() => {
13831386
const sessionId = lexClient.userId;
13841387
const serviceInfo = {
13851388
region: context.state.config.region,
13861389
service: 'execute-api'
13871390
};
13881391

1389-
const accessInfo = {
1390-
access_key: credentials.accessKeyId,
1391-
secret_key: credentials.secretAccessKey,
1392-
session_token: credentials.sessionToken,
1393-
}
1392+
Promise.resolve(awsCredentials).then((credentials) => {
1393+
const accessInfo = {
1394+
access_key: credentials.accessKeyId,
1395+
secret_key: credentials.secretAccessKey,
1396+
session_token: credentials.sessionToken,
1397+
}
13941398

1395-
const signedUrl = Signer.signUrl(context.state.config.lex.streamingWebSocketEndpoint+'?sessionId='+sessionId, accessInfo, serviceInfo);
1396-
wsClient = new WebSocket(signedUrl);
1397-
1398-
// Add heartbeat logic
1399-
const HEARTBEAT_INTERVAL = 540000; // 9 minutes
1400-
const MAX_DURATION = 7200000; // 2 hours
1401-
const startTime = Date.now();
1402-
let heartbeatTimer = null;
1403-
1404-
function startHeartbeat() {
1405-
if (wsClient.readyState === WebSocket.OPEN) {
1406-
const elapsedTime = Date.now() - startTime;
1407-
if (elapsedTime < MAX_DURATION) {
1408-
const pingMessage = JSON.stringify({ action: 'ping' });
1409-
wsClient.send(pingMessage);
1410-
console.log('Sending Ping:', new Date().toISOString());
1411-
heartbeatTimer = setTimeout(startHeartbeat, HEARTBEAT_INTERVAL);
1412-
} else {
1413-
console.log('Stopped sending pings after reaching 2-hour limit.');
1414-
clearTimeout(heartbeatTimer);
1399+
const signedUrl = Signer.signUrl(context.state.config.lex.streamingWebSocketEndpoint+'?sessionId='+sessionId, accessInfo, serviceInfo);
1400+
wsClient = new WebSocket(signedUrl);
1401+
1402+
// Add heartbeat logic
1403+
const HEARTBEAT_INTERVAL = 540000; // 9 minutes
1404+
const MAX_DURATION = 7200000; // 2 hours
1405+
const startTime = Date.now();
1406+
let heartbeatTimer = null;
1407+
1408+
function startHeartbeat() {
1409+
if (wsClient.readyState === WebSocket.OPEN) {
1410+
const elapsedTime = Date.now() - startTime;
1411+
if (elapsedTime < MAX_DURATION) {
1412+
const pingMessage = JSON.stringify({ action: 'ping' });
1413+
wsClient.send(pingMessage);
1414+
console.log('Sending Ping:', new Date().toISOString());
1415+
heartbeatTimer = setTimeout(startHeartbeat, HEARTBEAT_INTERVAL);
1416+
} else {
1417+
console.log('Stopped sending pings after reaching 2-hour limit.');
1418+
clearTimeout(heartbeatTimer);
1419+
}
14151420
}
14161421
}
1417-
}
1418-
wsClient.onopen = () => {
1419-
console.log('WebSocket Connected');
1420-
startHeartbeat();
1421-
};
1422+
wsClient.onopen = () => {
1423+
console.log('WebSocket Connected');
1424+
startHeartbeat();
1425+
};
14221426

1423-
wsClient.onclose = () => {
1424-
console.log('WebSocket Closed');
1425-
clearTimeout(heartbeatTimer);
1426-
};
1427+
wsClient.onclose = () => {
1428+
console.log('WebSocket Closed');
1429+
clearTimeout(heartbeatTimer);
1430+
};
14271431

1428-
wsClient.onerror = (error) => {
1429-
console.log('WebSocket Error', error.message);
1430-
clearTimeout(heartbeatTimer);
1431-
};
1432+
wsClient.onerror = (error) => {
1433+
console.log('WebSocket Error', error.message);
1434+
clearTimeout(heartbeatTimer);
1435+
};
1436+
});
14321437
});
14331438
},
14341439
typingWsMessages(context){

0 commit comments

Comments
 (0)