Merge pull request #42 from TwilioDevEd/chat-error-handling-improvements

Error handling improvements for Chat
This commit is contained in:
Jeff Linwood
2019-05-03 16:39:10 -05:00
committed by GitHub

View File

@@ -46,15 +46,23 @@ $(function() {
$.getJSON('/token', { $.getJSON('/token', {
device: 'browser' device: 'browser'
}, function(data) { }, function(data) {
// Initialize the Chat client
Twilio.Chat.Client.create(data.token).then(client => {
console.log('Created chat client');
chatClient = client;
chatClient.getSubscribedChannels().then(createOrJoinGeneralChannel);
// Alert the user they have been assigned a random username // Alert the user they have been assigned a random username
username = data.identity; username = data.identity;
print('You have been assigned a random username of: ' print('You have been assigned a random username of: '
+ '<span class="me">' + username + '</span>', true); + '<span class="me">' + username + '</span>', true);
// Initialize the Chat client }).catch(error => {
Twilio.Chat.Client.create(data.token).then(client => { console.error(error);
chatClient = client; print('There was an error creating the chat client:<br/>' + error, true);
chatClient.getSubscribedChannels().then(createOrJoinGeneralChannel); print('Please check your .env file.', false);
}); });
}); });
@@ -103,7 +111,12 @@ $(function() {
// Send a new message to the general channel // Send a new message to the general channel
var $input = $('#chat-input'); var $input = $('#chat-input');
$input.on('keydown', function(e) { $input.on('keydown', function(e) {
if (e.keyCode == 13) { if (e.keyCode == 13) {
if (generalChannel === undefined) {
print('The Chat Service is not configured. Please check your .env file.', false);
return;
}
generalChannel.sendMessage($input.val()) generalChannel.sendMessage($input.val())
$input.val(''); $input.val('');
} }