Added error handling to the connection

This commit is contained in:
unkelpehr 2014-09-21 20:45:20 +02:00
parent dcbc4c90c6
commit 42016ac9bc
3 changed files with 44 additions and 23 deletions

1
.gitignore vendored
View File

@ -26,3 +26,4 @@ node_modules
# Users Environment Variables
.lock-wscript
docs/Thumbs.db

View File

@ -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,10 +319,19 @@ define({
} else {
post('error', templates.server.bogus);
}
});
})
// Post the help/welcome message
post('motd', templates.motd, true);
.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).
@ -333,4 +342,9 @@ define({
parts[0] && commands.join(parts[0]);
parts[1] && commands.key(parts[1]);
}
})
.on('error', function () {
post('error', templates.messages.socket_error);
});
});

View File

@ -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.<br>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: {