Merge pull request #31 from ajtack/connection-failure-visibility

Avoid a token error, make the problem visible in the UI
This commit is contained in:
Wellington Mendoza
2017-11-30 09:50:33 -05:00
committed by GitHub
3 changed files with 14 additions and 7 deletions

View File

@@ -3,13 +3,13 @@ TWILIO_ACCOUNT_SID=
TWILIO_API_KEY= TWILIO_API_KEY=
TWILIO_API_SECRET= TWILIO_API_SECRET=
# Required for Chat # Required for Chat
TWILIO_CHAT_SERVICE_SID= # TWILIO_CHAT_SERVICE_SID=
# Required for Notify # Required for Notify
TWILIO_NOTIFICATION_SERVICE_SID= # TWILIO_NOTIFICATION_SERVICE_SID=
# Optional for Sync # Optional for Sync
# By default, this app will use the default instance for your account (named "default"). # By default, this app will use the default instance for your account (named "default").
# TWILIO_SYNC_SERVICE_SID= # TWILIO_SYNC_SERVICE_SID=

8
app.py
View File

@@ -50,11 +50,11 @@ def chat():
def config(): def config():
return jsonify( return jsonify(
TWILIO_ACCOUNT_SID=os.environ['TWILIO_ACCOUNT_SID'], TWILIO_ACCOUNT_SID=os.environ['TWILIO_ACCOUNT_SID'],
TWILIO_NOTIFICATION_SERVICE_SID=os.environ['TWILIO_NOTIFICATION_SERVICE_SID'], TWILIO_NOTIFICATION_SERVICE_SID=os.environ.get('TWILIO_NOTIFICATION_SERVICE_SID', None),
TWILIO_API_KEY=os.environ['TWILIO_API_KEY'], TWILIO_API_KEY=os.environ['TWILIO_API_KEY'],
TWILIO_API_SECRET=bool(os.environ['TWILIO_API_SECRET']), TWILIO_API_SECRET=bool(os.environ['TWILIO_API_SECRET']),
TWILIO_CHAT_SERVICE_SID=os.environ['TWILIO_CHAT_SERVICE_SID'], TWILIO_CHAT_SERVICE_SID=os.environ.get('TWILIO_CHAT_SERVICE_SID', None),
TWILIO_SYNC_SERVICE_SID=os.environ['TWILIO_SYNC_SERVICE_SID'], TWILIO_SYNC_SERVICE_SID=os.environ.get('TWILIO_SYNC_SERVICE_SID', None),
) )
@app.route('/token', methods=['GET']) @app.route('/token', methods=['GET'])
@@ -80,7 +80,7 @@ def generateToken(identity):
api_key = os.environ['TWILIO_API_KEY'] api_key = os.environ['TWILIO_API_KEY']
api_secret = os.environ['TWILIO_API_SECRET'] api_secret = os.environ['TWILIO_API_SECRET']
sync_service_sid = os.environ.get('TWILIO_SYNC_SERVICE_SID', 'default') sync_service_sid = os.environ.get('TWILIO_SYNC_SERVICE_SID', 'default')
chat_service_sid = os.environ['TWILIO_CHAT_SERVICE_SID'] chat_service_sid = os.environ.get('TWILIO_CHAT_SERVICE_SID', None)
# Create access token with credentials # Create access token with credentials
token = AccessToken(account_sid, api_key, api_secret, identity=identity) token = AccessToken(account_sid, api_key, api_secret, identity=identity)

View File

@@ -15,6 +15,13 @@ $(function () {
$.getJSON('/token', function (tokenResponse) { $.getJSON('/token', function (tokenResponse) {
//Initialize the Sync client //Initialize the Sync client
syncClient = new Twilio.Sync.Client(tokenResponse.token, { logLevel: 'info' }); syncClient = new Twilio.Sync.Client(tokenResponse.token, { logLevel: 'info' });
syncClient.on('connectionStateChanged', function(state) {
if (state != 'connected') {
$message.html('Sync is not live (websocket connection <span style="color: red">' + state + '</span>)…');
} else {
$message.html('Sync is live!');
}
});
//Let's pop a message on the screen to show that Sync is ready //Let's pop a message on the screen to show that Sync is ready
$message.html('Sync initialized!'); $message.html('Sync initialized!');