Skip to content
Permalink
Browse files

Disconnection handling

  • Loading branch information...
Depado committed May 17, 2014
1 parent dcf7916 commit 7b3efd2be7df4319626da0c8172a638e26b2bc9d
Showing with 37 additions and 9 deletions.
  1. +6 −2 app.js
  2. +31 −7 static/custom.js
8 app.js
@@ -40,6 +40,10 @@ io.sockets.on('connection', function (socket) {
io.sockets.emit('dash');
io.sockets.emit('server-message', socket.username + ' launches a wild pony !');
break;
case '/dashrain':
io.sockets.emit('dashrain');
io.sockets.emit('server-message', socket.username + ' launches a pony rain !');
break;
default:
var data = validator.escape(data);
for(var key in staticData.emotes) {
@@ -58,9 +62,9 @@ io.sockets.on('connection', function (socket) {
// Adding the user and support nickname changing
socket.on('adduser', function(username){
if(username in usernames) {
io.sockets.emit('error', 'This nickname is already taken.');
socket.emit('error', 'This nickname is already taken.');
} else if(username == 'null' || username == '') {
io.sockets.emit('error', 'This is not a valid nickname.');
socket.emit('error', 'This is not a valid nickname.');
} else {
var new_username = validator.escape(username);
if(socket.username in usernames) {
@@ -2,6 +2,11 @@ function scrollDown () {
var objDiv = document.getElementById('toscroll');
objDiv.scrollTop = objDiv.scrollHeight;
}

function titleNotify(isActive) {
if(!isActive && document.title != "[New] Unicorn Chat") document.title = "[New] Unicorn Chat";
}

var isActive;
window.onfocus = function () { isActive = true; document.title = "Unicorn Chat"; };
window.onblur = function () { isActive = false; };
@@ -21,7 +26,7 @@ socket.on('updatechat', function (username, data) {

socket.on('server-message', function(data) {
$('#conversation').append('<p>'+ data + '</p>');
if(!isActive && document.title != "[New] Unicorn Chat") document.title = "[New] Unicorn Chat";
titleNotify(isActive);
scrollDown();
});

@@ -47,6 +52,14 @@ socket.on('updateusers', function(data) {
$('#users').append('<div>Anonymous users : ' + data['anon'] + '</div>');
});

socket.on('disconnect', function() {
$('#data').hide();
$('#activate-browser-notifications').hide();
$('#nickname-send').html('Connect');
$('#conversation').append('<p>You\'re disconnected.</p>');
titleNotify(isActive);
});

$(function(){
$('#data').keypress(function(e) {
if(e.which == 13) {
@@ -76,19 +89,30 @@ $(function(){
var windowWidth = $(document).width();
var windowHeight = $(document).height();
var current_dash = 0;
socket.on('dash', function() {

function launchPony () {
current_dash += 1;
var this_dash = current_dash;
var image = "/custom/images/rdash.gif";
var position = Math.floor((Math.random() * 200) + 1);
if(position > 90) {
image = "/custom/images/rdash_fly.gif";
}
var position = Math.floor((Math.random() * 90) + 1);
$('body').append(
'<img id="dash_'+ this_dash + '" src="' + image + '" style="position: absolute; bottom:' + position + 'px;">'
'<img id="dash_'+ this_dash + '" src="' + image + '" style="position: absolute; bottom:' + position + 'px; left:-100px;z-index:-' + position + ';">'
);
$('#dash_' + this_dash).animate({left: windowWidth}, 5000, function() {
$('#dash_' + this_dash).remove();
});
}

socket.on('dash', function() {
launchPony();
});

socket.on('dashrain', function() {
for (var i = 20; i >= 0; i--) {
var wait = Math.random() * 10000;
setTimeout(function() {
launchPony();
}, wait);
};
});
});

0 comments on commit 7b3efd2

Please sign in to comment.
You can’t perform that action at this time.