Refactor connection logic

Aiming to be flatter, with less deep nesting. Still more to go here.
v1.18.x
Micheil Smith 11 years ago
parent 0bbd29ac42
commit 11b6794946

@ -13,150 +13,156 @@ var socket,
(function () {
var showWelcomeMessage = false;
var reconnecting = false;
function onSocketConnect(data) {
if (reconnecting) {
var reconnectEl = $('#reconnect');
reconnectEl.tooltip('destroy');
reconnectEl.html('<i class="fa fa-check"></i>');
reconnecting = false;
// Rejoin room that was left when we disconnected
var url_parts = document.location.pathname.slice(RELATIVE_PATH.length).split('/').slice(1);
var room;
switch(url_parts[0]) {
case 'user':
room = 'user/' + ajaxify.variables.get('theirid');
break;
case 'topic':
room = 'topic_' + url_parts[1];
break;
case 'category':
room = 'category_' + url_parts[1];
break;
case 'recent': // intentional fall-through
case 'unread':
room = 'recent_posts';
break;
case 'admin':
room = 'admin';
break;
default:
room = 'global';
break;
}
app.loadConfig = function() {
$.ajax({
url: RELATIVE_PATH + '/api/config',
success: function (data) {
config = data;
app.enterRoom(room, true);
exposeConfigToTemplates();
socket.emit('meta.reconnected');
$(window).trigger('action:reconnected');
if(socket) {
socket.disconnect();
setTimeout(function() {
socket.socket.connect();
}, 200);
} else {
var ioParams = {
'max reconnection attempts': config.maxReconnectionAttempts,
'reconnection delay': config.reconnectionDelay,
resource: RELATIVE_PATH.length ? RELATIVE_PATH.slice(1) + '/socket.io' : 'socket.io'
};
setTimeout(function() {
reconnectEl.removeClass('active').addClass("hide");
}, 3000);
}
if (utils.isAndroidBrowser()) {
ioParams.transports = ['xhr-polling'];
}
socket.emit('meta.updateHeader', {
fields: ['username', 'picture', 'userslug']
}, app.updateHeader);
}
socket = io.connect('', ioParams);
function onConfigLoad(data) {
config = data;
var reconnecting = false,
reconnectEl, reconnectTimer;
exposeConfigToTemplates();
socket.on('event:connect', function (data) {
app.username = data.username;
app.uid = data.uid;
app.isAdmin = data.isAdmin;
if(socket) {
socket.disconnect();
setTimeout(function() {
socket.socket.connect();
}, 200);
} else {
var ioParams = {
'max reconnection attempts': config.maxReconnectionAttempts,
'reconnection delay': config.reconnectionDelay,
resource: RELATIVE_PATH.length ? RELATIVE_PATH.slice(1) + '/socket.io' : 'socket.io'
};
if (utils.isAndroidBrowser()) {
ioParams.transports = ['xhr-polling'];
}
templates.setGlobal('loggedIn', parseInt(data.uid, 10) !== 0);
socket = io.connect('', ioParams);
reconnecting = false;
app.showLoginMessage();
socket.on('event:connect', function (data) {
app.username = data.username;
app.uid = data.uid;
app.isAdmin = data.isAdmin;
socket.emit('meta.updateHeader', {
fields: ['username', 'picture', 'userslug']
}, app.updateHeader);
templates.setGlobal('loggedIn', parseInt(data.uid, 10) !== 0);
$(window).trigger('action:connected');
});
app.showLoginMessage();
socket.on('event:alert', function (data) {
app.alert(data);
});
socket.emit('meta.updateHeader', {
fields: ['username', 'picture', 'userslug']
}, app.updateHeader);
socket.on('connect', function (data) {
if (reconnecting) {
reconnectEl.tooltip('destroy');
reconnectEl.html('<i class="fa fa-check"></i>');
reconnecting = false;
// Rejoin room that was left when we disconnected
var url_parts = document.location.pathname.slice(RELATIVE_PATH.length).split('/').slice(1),
room;
switch(url_parts[0]) {
case 'user':
room = 'user/' + ajaxify.variables.get('theirid');
break;
case 'topic':
room = 'topic_' + url_parts[1];
break;
case 'category':
room = 'category_' + url_parts[1];
break;
case 'recent': // intentional fall-through
case 'unread':
room = 'recent_posts';
break;
case 'admin':
room = 'admin';
break;
default:
room = 'global';
break;
}
app.enterRoom(room, true);
socket.emit('meta.reconnected');
$(window).trigger('action:reconnected');
setTimeout(function() {
reconnectEl.removeClass('active').addClass("hide");
}, 3000);
}
socket.emit('meta.updateHeader', {
fields: ['username', 'picture', 'userslug']
}, app.updateHeader);
});
$(window).trigger('action:connected');
});
socket.on('event:disconnect', function() {
$(window).trigger('action:disconnected');
socket.socket.connect();
});
socket.on('event:alert', function (data) {
app.alert(data);
});
socket.on('reconnecting', function (data, attempt) {
if(attempt === config.maxReconnectionAttempts) {
socket.socket.reconnectionAttempts = 0;
socket.socket.reconnectionDelay = config.reconnectionDelay;
return;
}
socket.on('connect', onSocketConnect);
reconnectEl = reconnectEl || $('#reconnect');
reconnecting = true;
socket.on('event:disconnect', function() {
$(window).trigger('action:disconnected');
socket.socket.connect();
});
if (!reconnectEl.hasClass('active')) {
reconnectEl.html('<i class="fa fa-spinner fa-spin"></i>');
}
socket.on('reconnecting', function (data, attempt) {
if(attempt === config.maxReconnectionAttempts) {
socket.socket.reconnectionAttempts = 0;
socket.socket.reconnectionDelay = config.reconnectionDelay;
return;
}
reconnectEl.addClass('active').removeClass("hide").tooltip({
placement: 'bottom'
});
});
reconnecting = true;
var reconnectEl = $('#reconnect');
socket.on('event:banned', function() {
app.alert({
title: '[[global:alert.banned]]',
message: '[[global:alert.banned.message]]',
type: 'warning',
timeout: 1000
});
if (!reconnectEl.hasClass('active')) {
reconnectEl.html('<i class="fa fa-spinner fa-spin"></i>');
}
setTimeout(app.logout, 1000);
});
reconnectEl.addClass('active').removeClass("hide").tooltip({
placement: 'bottom'
});
});
socket.on('meta.updateHeader', app.updateHeader);
socket.on('event:banned', function() {
app.alert({
title: '[[global:alert.banned]]',
message: '[[global:alert.banned.message]]',
type: 'warning',
timeout: 1000
});
setTimeout(app.logout, 1000);
});
app.enterRoom('global');
socket.on('meta.updateHeader', app.updateHeader);
if (config.environment === 'development' && console && console.log) {
var log = console.log;
console.log = function() {
log.apply(this, arguments);
socket.emit('tools.log', arguments);
};
}
}
},
app.enterRoom('global');
if (config.environment === 'development' && console && console.log) {
var log = console.log;
console.log = function() {
log.apply(this, arguments);
socket.emit('tools.log', arguments);
};
}
}
}
app.loadConfig = function() {
$.ajax({
url: RELATIVE_PATH + '/api/config',
success: onConfigLoad,
async: false
});
};
@ -586,4 +592,4 @@ var socket,
app.loadConfig();
app.alternatingTitle('');
}());
}());

Loading…
Cancel
Save