From 42016ac9bcc473da6fcb8b829037445cf153759c Mon Sep 17 00:00:00 2001 From: unkelpehr Date: Sun, 21 Sep 2014 20:45:20 +0200 Subject: [PATCH] Added error handling to the connection --- .gitignore | 1 + public/js/cryptalk_modules/cryptalk.js | 58 +++++++++++++++---------- public/js/cryptalk_modules/templates.js | 8 +++- 3 files changed, 44 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 59d842b..0efe000 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,4 @@ node_modules # Users Environment Variables .lock-wscript +docs/Thumbs.db diff --git a/public/js/cryptalk_modules/cryptalk.js b/public/js/cryptalk_modules/cryptalk.js index daec06c..5144b86 100644 --- a/public/js/cryptalk_modules/cryptalk.js +++ b/public/js/cryptalk_modules/cryptalk.js @@ -248,21 +248,21 @@ define({ // Save to history pushHistory(buffer); } - - - }; - // Connect to server + // Post the help/welcome message + post('motd', templates.motd, true); + + // Push 'Connecting...' message + post('info', $.template(templates.messages.connecting, { + host: data.host || 'localhost' + })); + + // The one and only socket socket = $.Websocket.connect(data.host); // Bind socket events socket - .on('connect', function () { - $(document).on('keydown', onKeyDown); - components.input.focus(); - }) - .on('room:generated', function (data) { var sanitized = $.escapeHtml(data); post('server', $.template(templates.server.room_generated, { payload: sanitized })); @@ -319,18 +319,32 @@ define({ } else { post('error', templates.server.bogus); } + }) + + .on('connect', function () { + // Bind the necessary DOM events + $(document).on('keydown', onKeyDown); + + // Put focus on the message input + components.input.focus(); + + // Tell the user that the chat is ready to interact with + post('info', $.template(templates.messages.connected, { + host: data.host || 'localhost' + })); + + // It's possible to provide room and key using the hashtag. + // The room and key is then seperated by semicolon (room:key). + // If there is no semicolon present, the complete hash will be treated as the room name and the key has to be set manually. + if (hash = window.location.hash) { + parts = hash.slice(1).split(':'); + + parts[0] && commands.join(parts[0]); + parts[1] && commands.key(parts[1]); + } + }) + + .on('error', function () { + post('error', templates.messages.socket_error); }); - - // Post the help/welcome message - post('motd', templates.motd, true); - - // It's possible to provide room and key using the hashtag. - // The room and key is then seperated by semicolon (room:key). - // If there is no semicolon present, the complete hash will be treated as the room name and the key has to be set manually. - if (hash = window.location.hash) { - parts = hash.slice(1).split(':'); - - parts[0] && commands.join(parts[0]); - parts[1] && commands.key(parts[1]); - } }); \ No newline at end of file diff --git a/public/js/cryptalk_modules/templates.js b/public/js/cryptalk_modules/templates.js index 719e738..52c9e08 100644 --- a/public/js/cryptalk_modules/templates.js +++ b/public/js/cryptalk_modules/templates.js @@ -79,7 +79,13 @@ define({ left_room: 'Left room {roomName}', already_in_room: 'You are already in a room ({roomName}), stoopid.', - unable_to_decrypt: 'Unabled to decrypt received message, keys does not match.' + unable_to_decrypt: 'Unabled to decrypt received message, keys does not match.', + + socket_error: 'A network error has occurred. A restart may be required to bring back full functionality.
Examine the logs for more details.', + + // Available variable: 'host' + connecting: 'Connecting to host {host}...', + connected: 'A connection to the server has been established. Happy chatting!' }, server: {