mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-08 06:55:46 +01:00
moved socket connection out of app.js
This commit is contained in:
@@ -1,178 +1,86 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
/*global io, templates, ajaxify, utils, bootbox, RELATIVE_PATH, config, Visibility*/
|
/*global io, templates, ajaxify, utils, bootbox, RELATIVE_PATH, config, Visibility*/
|
||||||
|
|
||||||
var socket,
|
var app = app || {};
|
||||||
app = app || {};
|
|
||||||
|
|
||||||
app.isFocused = true;
|
app.isFocused = true;
|
||||||
app.isConnected = false;
|
|
||||||
app.currentRoom = null;
|
app.currentRoom = null;
|
||||||
app.widgets = {};
|
app.widgets = {};
|
||||||
app.cacheBuster = null;
|
app.cacheBuster = null;
|
||||||
|
|
||||||
(function () {
|
(function () {
|
||||||
var showWelcomeMessage = false;
|
var showWelcomeMessage = !!utils.params().loggedin;
|
||||||
var reconnecting = false;
|
|
||||||
|
|
||||||
function socketIOConnect() {
|
templates.setGlobal('config', config);
|
||||||
var ioParams = {
|
|
||||||
reconnectionAttempts: config.maxReconnectionAttempts,
|
|
||||||
reconnectionDelay: config.reconnectionDelay,
|
|
||||||
transports: config.socketioTransports,
|
|
||||||
path: config.relative_path + '/socket.io'
|
|
||||||
};
|
|
||||||
|
|
||||||
socket = io(config.websocketAddress, ioParams);
|
app.cacheBuster = config['cache-buster'];
|
||||||
reconnecting = false;
|
|
||||||
|
|
||||||
socket.on('event:connect', function () {
|
require(['csrf'], function(csrf) {
|
||||||
app.showLoginMessage();
|
csrf.set(config.csrf_token);
|
||||||
app.replaceSelfLinks();
|
});
|
||||||
$(window).trigger('action:connected');
|
|
||||||
app.isConnected = true;
|
|
||||||
});
|
|
||||||
|
|
||||||
socket.on('connect', onSocketConnect);
|
bootbox.setDefaults({
|
||||||
|
locale: config.userLang
|
||||||
|
});
|
||||||
|
|
||||||
socket.on('event:disconnect', function() {
|
app.load = function() {
|
||||||
$(window).trigger('action:disconnected');
|
$('document').ready(function () {
|
||||||
app.isConnected = false;
|
var url = ajaxify.start(window.location.pathname.slice(1) + window.location.search, true);
|
||||||
socket.connect();
|
ajaxify.end(url, app.template);
|
||||||
});
|
|
||||||
|
|
||||||
socket.on('reconnecting', function (attempt) {
|
handleStatusChange();
|
||||||
reconnecting = true;
|
|
||||||
var reconnectEl = $('#reconnect');
|
|
||||||
|
|
||||||
if (!reconnectEl.hasClass('active')) {
|
if (config.searchEnabled) {
|
||||||
reconnectEl.html('<i class="fa fa-spinner fa-spin"></i>');
|
app.handleSearch();
|
||||||
}
|
}
|
||||||
|
|
||||||
reconnectEl.addClass('active').removeClass("hide").tooltip({
|
handleNewTopic();
|
||||||
placement: 'bottom'
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
socket.on('event:banned', function() {
|
require(['components'], function(components) {
|
||||||
app.alert({
|
components.get('user/logout').on('click', app.logout);
|
||||||
title: '[[global:alert.banned]]',
|
|
||||||
message: '[[global:alert.banned.message]]',
|
|
||||||
type: 'danger',
|
|
||||||
timeout: 1000
|
|
||||||
});
|
});
|
||||||
|
|
||||||
setTimeout(function() {
|
Visibility.change(function(e, state){
|
||||||
window.location.href = config.relative_path + '/';
|
if (state === 'visible') {
|
||||||
}, 1000);
|
app.isFocused = true;
|
||||||
});
|
app.alternatingTitle('');
|
||||||
|
} else if (state === 'hidden') {
|
||||||
socket.on('event:logout', app.logout);
|
app.isFocused = false;
|
||||||
|
}
|
||||||
socket.on('event:alert', function(data) {
|
|
||||||
app.alert(data);
|
|
||||||
});
|
|
||||||
|
|
||||||
socket.on('reconnect_failed', function() {
|
|
||||||
// Wait ten times the reconnection delay and then start over
|
|
||||||
setTimeout(socket.connect.bind(socket), parseInt(config.reconnectionDelay, 10) * 10);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
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 = window.location.pathname.slice(RELATIVE_PATH.length).split('/').slice(1);
|
|
||||||
var room;
|
|
||||||
|
|
||||||
switch(url_parts[0]) {
|
|
||||||
case 'user':
|
|
||||||
room = 'user/' + (ajaxify.data ? ajaxify.data.theirid : 0);
|
|
||||||
break;
|
|
||||||
case 'topic':
|
|
||||||
room = 'topic_' + url_parts[1];
|
|
||||||
break;
|
|
||||||
case 'category':
|
|
||||||
room = 'category_' + url_parts[1];
|
|
||||||
break;
|
|
||||||
case 'recent':
|
|
||||||
room = 'recent_topics';
|
|
||||||
break;
|
|
||||||
case 'unread':
|
|
||||||
room = 'unread_topics';
|
|
||||||
break;
|
|
||||||
case 'popular':
|
|
||||||
room = 'popular_topics';
|
|
||||||
break;
|
|
||||||
case 'admin':
|
|
||||||
room = 'admin';
|
|
||||||
break;
|
|
||||||
case 'categories':
|
|
||||||
room = 'categories';
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
app.currentRoom = '';
|
|
||||||
app.enterRoom(room);
|
|
||||||
|
|
||||||
socket.emit('meta.reconnected');
|
|
||||||
|
|
||||||
app.isConnected = true;
|
|
||||||
$(window).trigger('action:reconnected');
|
|
||||||
|
|
||||||
setTimeout(function() {
|
|
||||||
reconnectEl.removeClass('active').addClass('hide');
|
|
||||||
}, 3000);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function overrideBootbox() {
|
|
||||||
var dialog = bootbox.dialog,
|
|
||||||
prompt = bootbox.prompt,
|
|
||||||
confirm = bootbox.confirm;
|
|
||||||
|
|
||||||
function translate(modal) {
|
|
||||||
var footer = modal.find('.modal-footer');
|
|
||||||
translator.translate(footer.html(), function(html) {
|
|
||||||
footer.html(html);
|
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
bootbox.dialog = function() {
|
overrides.overrideBootbox();
|
||||||
var modal = $(dialog.apply(this, arguments)[0]);
|
overrides.overrideTimeago();
|
||||||
translate(modal);
|
createHeaderTooltips();
|
||||||
return modal;
|
app.showEmailConfirmWarning();
|
||||||
};
|
|
||||||
|
|
||||||
bootbox.prompt = function() {
|
socket.removeAllListeners('event:nodebb.ready');
|
||||||
var modal = $(prompt.apply(this, arguments)[0]);
|
socket.on('event:nodebb.ready', function(data) {
|
||||||
translate(modal);
|
if (!app.cacheBusters || app.cacheBusters['cache-buster'] !== data['cache-buster']) {
|
||||||
return modal;
|
app.cacheBusters = data;
|
||||||
};
|
|
||||||
|
|
||||||
bootbox.confirm = function() {
|
app.alert({
|
||||||
var modal = $(confirm.apply(this, arguments)[0]);
|
alert_id: 'forum_updated',
|
||||||
translate(modal);
|
title: '[[global:updated.title]]',
|
||||||
return modal;
|
message: '[[global:updated.message]]',
|
||||||
};
|
clickfn: function() {
|
||||||
}
|
window.location.reload();
|
||||||
|
},
|
||||||
|
type: 'warning'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
function overrideTimeago() {
|
require(['taskbar', 'helpers'], function(taskbar, helpers) {
|
||||||
var timeagoFn = $.fn.timeago;
|
taskbar.init();
|
||||||
$.fn.timeago = function() {
|
|
||||||
var els = timeagoFn.apply(this, arguments);
|
|
||||||
|
|
||||||
if (els) {
|
// templates.js helpers
|
||||||
els.each(function() {
|
helpers.register();
|
||||||
$(this).attr('title', (new Date($(this).attr('title'))).toString());
|
|
||||||
});
|
$(window).trigger('action:app.load');
|
||||||
}
|
});
|
||||||
};
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
app.logout = function() {
|
app.logout = function() {
|
||||||
require(['csrf'], function(csrf) {
|
require(['csrf'], function(csrf) {
|
||||||
@@ -245,14 +153,7 @@ app.cacheBuster = null;
|
|||||||
var path = window.location.pathname;
|
var path = window.location.pathname;
|
||||||
$('#main-nav li').removeClass('active');
|
$('#main-nav li').removeClass('active');
|
||||||
if (path) {
|
if (path) {
|
||||||
$('#main-nav li a').each(function () {
|
$('#main-nav li').removeClass('active').find('a[href="' + path + '"]').parent().addClass('active');
|
||||||
var href = $(this).attr('href');
|
|
||||||
|
|
||||||
if (href && path.startsWith(href)) {
|
|
||||||
$(this.parentNode).addClass('active');
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -535,65 +436,6 @@ app.cacheBuster = null;
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
app.load = function() {
|
|
||||||
$('document').ready(function () {
|
|
||||||
var url = ajaxify.start(window.location.pathname.slice(1) + window.location.search, true);
|
|
||||||
ajaxify.end(url, app.template);
|
|
||||||
|
|
||||||
handleStatusChange();
|
|
||||||
|
|
||||||
if (config.searchEnabled) {
|
|
||||||
app.handleSearch();
|
|
||||||
}
|
|
||||||
|
|
||||||
handleNewTopic();
|
|
||||||
|
|
||||||
require(['components'], function(components) {
|
|
||||||
components.get('user/logout').on('click', app.logout);
|
|
||||||
});
|
|
||||||
|
|
||||||
Visibility.change(function(e, state){
|
|
||||||
if (state === 'visible') {
|
|
||||||
app.isFocused = true;
|
|
||||||
app.alternatingTitle('');
|
|
||||||
} else if (state === 'hidden') {
|
|
||||||
app.isFocused = false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
overrideBootbox();
|
|
||||||
overrideTimeago();
|
|
||||||
createHeaderTooltips();
|
|
||||||
app.showEmailConfirmWarning();
|
|
||||||
|
|
||||||
socket.removeAllListeners('event:nodebb.ready');
|
|
||||||
socket.on('event:nodebb.ready', function(data) {
|
|
||||||
if (!app.cacheBusters || app.cacheBusters['cache-buster'] !== data['cache-buster']) {
|
|
||||||
app.cacheBusters = data;
|
|
||||||
|
|
||||||
app.alert({
|
|
||||||
alert_id: 'forum_updated',
|
|
||||||
title: '[[global:updated.title]]',
|
|
||||||
message: '[[global:updated.message]]',
|
|
||||||
clickfn: function() {
|
|
||||||
window.location.reload();
|
|
||||||
},
|
|
||||||
type: 'warning'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
require(['taskbar', 'helpers'], function(taskbar, helpers) {
|
|
||||||
taskbar.init();
|
|
||||||
|
|
||||||
// templates.js helpers
|
|
||||||
helpers.register();
|
|
||||||
|
|
||||||
$(window).trigger('action:app.load');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
app.loadJQueryUI = function(callback) {
|
app.loadJQueryUI = function(callback) {
|
||||||
if (typeof $().autocomplete === 'function') {
|
if (typeof $().autocomplete === 'function') {
|
||||||
return callback();
|
return callback();
|
||||||
@@ -636,21 +478,5 @@ app.cacheBuster = null;
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
showWelcomeMessage = window.location.href.indexOf('loggedin') !== -1;
|
|
||||||
|
|
||||||
socketIOConnect();
|
|
||||||
|
|
||||||
templates.setGlobal('config', config);
|
|
||||||
|
|
||||||
app.cacheBuster = config['cache-buster'];
|
|
||||||
|
|
||||||
require(['csrf'], function(csrf) {
|
|
||||||
csrf.set(config.csrf_token);
|
|
||||||
});
|
|
||||||
|
|
||||||
bootbox.setDefaults({
|
|
||||||
locale: config.userLang
|
|
||||||
});
|
|
||||||
|
|
||||||
app.alternatingTitle('');
|
|
||||||
}());
|
}());
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ define('forum/login', ['csrf', 'translator'], function(csrf, translator) {
|
|||||||
'x-csrf-token': csrf.get()
|
'x-csrf-token': csrf.get()
|
||||||
},
|
},
|
||||||
success: function(data, status) {
|
success: function(data, status) {
|
||||||
window.location.href = data;
|
window.location.href = data + '?loggedin';
|
||||||
},
|
},
|
||||||
error: function(data, status) {
|
error: function(data, status) {
|
||||||
errorEl.find('p').translateText(data.responseText);
|
errorEl.find('p').translateText(data.responseText);
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
/* global bootbox, translator */
|
||||||
|
|
||||||
|
var overrides = overrides || {};
|
||||||
|
|
||||||
if ('undefined' !== typeof window) {
|
if ('undefined' !== typeof window) {
|
||||||
|
|
||||||
(function ($, undefined) {
|
(function ($, undefined) {
|
||||||
@@ -102,4 +106,48 @@ if ('undefined' !== typeof window) {
|
|||||||
});
|
});
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
overrides.overrideBootbox = function () {
|
||||||
|
var dialog = bootbox.dialog,
|
||||||
|
prompt = bootbox.prompt,
|
||||||
|
confirm = bootbox.confirm;
|
||||||
|
|
||||||
|
function translate(modal) {
|
||||||
|
var footer = modal.find('.modal-footer');
|
||||||
|
translator.translate(footer.html(), function(html) {
|
||||||
|
footer.html(html);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
bootbox.dialog = function() {
|
||||||
|
var modal = $(dialog.apply(this, arguments)[0]);
|
||||||
|
translate(modal);
|
||||||
|
return modal;
|
||||||
|
};
|
||||||
|
|
||||||
|
bootbox.prompt = function() {
|
||||||
|
var modal = $(prompt.apply(this, arguments)[0]);
|
||||||
|
translate(modal);
|
||||||
|
return modal;
|
||||||
|
};
|
||||||
|
|
||||||
|
bootbox.confirm = function() {
|
||||||
|
var modal = $(confirm.apply(this, arguments)[0]);
|
||||||
|
translate(modal);
|
||||||
|
return modal;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
overrides.overrideTimeago = function() {
|
||||||
|
var timeagoFn = $.fn.timeago;
|
||||||
|
$.fn.timeago = function() {
|
||||||
|
var els = timeagoFn.apply(this, arguments);
|
||||||
|
|
||||||
|
if (els) {
|
||||||
|
els.each(function() {
|
||||||
|
$(this).attr('title', (new Date($(this).attr('title'))).toString());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
133
public/src/sockets.js
Normal file
133
public/src/sockets.js
Normal file
@@ -0,0 +1,133 @@
|
|||||||
|
'use strict';
|
||||||
|
/* globals config, io, ajaxify */
|
||||||
|
|
||||||
|
var app = app || {};
|
||||||
|
var socket;
|
||||||
|
app.isConnected = false;
|
||||||
|
|
||||||
|
(function () {
|
||||||
|
var reconnecting = false;
|
||||||
|
|
||||||
|
var ioParams = {
|
||||||
|
reconnectionAttempts: config.maxReconnectionAttempts,
|
||||||
|
reconnectionDelay: config.reconnectionDelay,
|
||||||
|
transports: config.socketioTransports,
|
||||||
|
path: config.relative_path + '/socket.io'
|
||||||
|
};
|
||||||
|
|
||||||
|
socket = io(config.websocketAddress, ioParams);
|
||||||
|
|
||||||
|
socket.on('connect', onSocketConnect);
|
||||||
|
|
||||||
|
socket.on('reconnecting', onReconnecting);
|
||||||
|
|
||||||
|
socket.on('reconnect_failed', function() {
|
||||||
|
// Wait ten times the reconnection delay and then start over
|
||||||
|
setTimeout(socket.connect.bind(socket), parseInt(config.reconnectionDelay, 10) * 10);
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on('event:connect', onEventConnect);
|
||||||
|
|
||||||
|
socket.on('event:disconnect', onEventDisconnect);
|
||||||
|
|
||||||
|
socket.on('event:banned', onEventBanned);
|
||||||
|
|
||||||
|
socket.on('event:logout', app.logout);
|
||||||
|
|
||||||
|
socket.on('event:alert', app.alert);
|
||||||
|
|
||||||
|
function onSocketConnect() {
|
||||||
|
if (reconnecting) {
|
||||||
|
var reconnectEl = $('#reconnect');
|
||||||
|
|
||||||
|
reconnectEl.tooltip('destroy');
|
||||||
|
reconnectEl.html('<i class="fa fa-check"></i>');
|
||||||
|
reconnecting = false;
|
||||||
|
|
||||||
|
reJoinCurrentRoom();
|
||||||
|
|
||||||
|
socket.emit('meta.reconnected');
|
||||||
|
|
||||||
|
app.isConnected = true;
|
||||||
|
$(window).trigger('action:reconnected');
|
||||||
|
|
||||||
|
setTimeout(function() {
|
||||||
|
reconnectEl.removeClass('active').addClass('hide');
|
||||||
|
}, 3000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function reJoinCurrentRoom() {
|
||||||
|
var url_parts = window.location.pathname.slice(config.relative_path.length).split('/').slice(1);
|
||||||
|
var room;
|
||||||
|
|
||||||
|
switch(url_parts[0]) {
|
||||||
|
case 'user':
|
||||||
|
room = 'user/' + (ajaxify.data ? ajaxify.data.theirid : 0);
|
||||||
|
break;
|
||||||
|
case 'topic':
|
||||||
|
room = 'topic_' + url_parts[1];
|
||||||
|
break;
|
||||||
|
case 'category':
|
||||||
|
room = 'category_' + url_parts[1];
|
||||||
|
break;
|
||||||
|
case 'recent':
|
||||||
|
room = 'recent_topics';
|
||||||
|
break;
|
||||||
|
case 'unread':
|
||||||
|
room = 'unread_topics';
|
||||||
|
break;
|
||||||
|
case 'popular':
|
||||||
|
room = 'popular_topics';
|
||||||
|
break;
|
||||||
|
case 'admin':
|
||||||
|
room = 'admin';
|
||||||
|
break;
|
||||||
|
case 'categories':
|
||||||
|
room = 'categories';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
app.currentRoom = '';
|
||||||
|
app.enterRoom(room);
|
||||||
|
}
|
||||||
|
|
||||||
|
function onReconnecting(attempt) {
|
||||||
|
reconnecting = true;
|
||||||
|
var reconnectEl = $('#reconnect');
|
||||||
|
|
||||||
|
if (!reconnectEl.hasClass('active')) {
|
||||||
|
reconnectEl.html('<i class="fa fa-spinner fa-spin"></i>');
|
||||||
|
}
|
||||||
|
|
||||||
|
reconnectEl.addClass('active').removeClass("hide").tooltip({
|
||||||
|
placement: 'bottom'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function onEventConnect() {
|
||||||
|
app.showLoginMessage();
|
||||||
|
app.replaceSelfLinks();
|
||||||
|
$(window).trigger('action:connected');
|
||||||
|
app.isConnected = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function onEventDisconnect() {
|
||||||
|
$(window).trigger('action:disconnected');
|
||||||
|
app.isConnected = false;
|
||||||
|
socket.connect();
|
||||||
|
}
|
||||||
|
|
||||||
|
function onEventBanned() {
|
||||||
|
app.alert({
|
||||||
|
title: '[[global:alert.banned]]',
|
||||||
|
message: '[[global:alert.banned.message]]',
|
||||||
|
type: 'danger',
|
||||||
|
timeout: 1000
|
||||||
|
});
|
||||||
|
|
||||||
|
setTimeout(function() {
|
||||||
|
window.location.href = config.relative_path + '/';
|
||||||
|
}, 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
}());
|
||||||
@@ -38,6 +38,7 @@ module.exports = function(Meta) {
|
|||||||
'public/vendor/autosize.js',
|
'public/vendor/autosize.js',
|
||||||
'./node_modules/templates.js/lib/templates.js',
|
'./node_modules/templates.js/lib/templates.js',
|
||||||
'public/src/utils.js',
|
'public/src/utils.js',
|
||||||
|
'public/src/sockets.js',
|
||||||
'public/src/app.js',
|
'public/src/app.js',
|
||||||
'public/src/ajaxify.js',
|
'public/src/ajaxify.js',
|
||||||
'public/src/overrides.js',
|
'public/src/overrides.js',
|
||||||
|
|||||||
Reference in New Issue
Block a user