diff --git a/app.js b/app.js index 02d4d3f7e4..e1391e432b 100644 --- a/app.js +++ b/app.js @@ -82,9 +82,12 @@ var templates = require('./public/src/templates.js'), webserver = require('./src/webserver.js'), + SocketIO = require('socket.io').listen(global.server, { log: false, transports: ['websocket', 'xhr-polling', 'jsonp-polling', 'flashsocket']}), websockets = require('./src/websockets.js'), plugins = require('./src/plugins'); // Don't remove this - plugins initializes itself + websockets.init(SocketIO); + global.templates = {}; templates.init([ 'header', 'footer', 'logout', 'outgoing', 'admin/header', 'admin/footer', 'admin/index', diff --git a/public/src/app.js b/public/src/app.js index f9a793c055..b42fc1e211 100644 --- a/public/src/app.js +++ b/public/src/app.js @@ -17,7 +17,9 @@ var socket, config = data; if(socket) { socket.disconnect(); - socket.socket.connect(); + setTimeout(function() { + socket.socket.connect(); + }, 200); } else { socket = io.connect(config.socket.address); @@ -28,8 +30,8 @@ var socket, app.username = data.username; app.showLoginMessage(); socket.emit('api:updateHeader', { - fields: ['username', 'picture', 'userslug'] - }); + fields: ['username', 'picture', 'userslug'] + }); }); socket.on('event:alert', function (data) { @@ -49,10 +51,15 @@ var socket, }, 1000); reconnecting = false; reconnectTries = 0; - socket.emit('api:updateHeader', { - fields: ['username', 'picture', 'userslug'] - }); } + + socket.emit('api:updateHeader', { + fields: ['username', 'picture', 'userslug'] + }); + }); + + socket.on('event:disconnect', function() { + socket.socket.connect(); }); socket.on('reconnecting', function (data) { diff --git a/public/src/forum/footer.js b/public/src/forum/footer.js index 3f91308384..d3cc3d094f 100644 --- a/public/src/forum/footer.js +++ b/public/src/forum/footer.js @@ -3,8 +3,7 @@ stats_topics = document.getElementById('stats_topics'), stats_posts = document.getElementById('stats_posts'), stats_online = document.getElementById('stats_online'), - user_label = document.getElementById('user_label'), - right_menu = document.getElementById('right-menu'); + user_label = document.getElementById('user_label'); socket.emit('user.count', {}); socket.on('user.count', function(data) { @@ -25,6 +24,7 @@ socket.emit('api:updateHeader', { fields: ['username', 'picture', 'userslug'] }); + socket.on('api:updateHeader', function(data) { jQuery('#search-button').on('click', function() { jQuery('#search-fields').removeClass('hide').show(); @@ -42,14 +42,19 @@ }); }); - var rightMenu = $('#right-menu'), + var loggedInMenu = $('#logged-in-menu'), isLoggedIn = data.uid > 0; if (isLoggedIn) { jQuery('.nodebb-loggedin').show(); jQuery('.nodebb-loggedout').hide(); - var userLabel = rightMenu.find('#user_label'); + $('#logged-out-menu').addClass('hide'); + $('#logged-in-menu').removeClass('hide'); + + $('#search-button').show(); + + var userLabel = loggedInMenu.find('#user_label'); if (userLabel.length) { if (data['userslug']) userLabel.attr('href', '/user/' + data['userslug']); @@ -57,18 +62,8 @@ userLabel.find('img').attr('src', data['picture']); if (data['username']) userLabel.find('span').html(data['username']); - } else { - rightMenu.empty(); - var userli = $('<li> \ - <a id="user_label" href="/user/' + data['userslug'] + '"> \ - <img src="' + data['picture'] + '"/> \ - <span>' + data['username'] + '</span> \ - </a> \ - </li>'); - rightMenu.append(userli); - - var logoutli = $('<li><a href="#">Log out</a></li>'); - logoutli.on('click', function() { + + $('#logout-link').on('click', function() { var csrf_token = $('#csrf_token').val(); $.post(RELATIVE_PATH + '/logout', { @@ -77,23 +72,16 @@ window.location = RELATIVE_PATH + '/'; }); }); - rightMenu.append(logoutli); } } else { $('#search-button').hide(); + jQuery('.nodebb-loggedin').hide(); jQuery('.nodebb-loggedout').show(); - rightMenu.html(''); - - var registerEl = document.createElement('li'), - loginEl = document.createElement('li'); - - registerEl.innerHTML = '<a href="/register">Register</a>'; - loginEl.innerHTML = '<a href="/login">Login</a>'; + $('#logged-out-menu').removeClass('hide'); + $('#logged-in-menu').addClass('hide'); - right_menu.appendChild(registerEl); - right_menu.appendChild(loginEl); } $('#main-nav a,#right-menu a').off('click').on('click', function() { diff --git a/public/templates/header.tpl b/public/templates/header.tpl index 2cc068220d..55d2254c98 100644 --- a/public/templates/header.tpl +++ b/public/templates/header.tpl @@ -65,8 +65,8 @@ <button id="search-button" type="button" class="btn btn-link"><i class="icon-search"></i></button> </form> - <ul id="right-menu" class="nav navbar-nav navbar-right"> - <li class="notifications dropdown text-center hidden-xs"> + <ul id="logged-in-menu" class="nav navbar-nav navbar-right hide"> + <li id="notifications-list" class="notifications dropdown text-center hidden-xs"> <a class="dropdown-toggle" data-toggle="dropdown" href="#" id="notif_dropdown"><i class="icon-circle-blank"></i></a> <ul id="notif-list" class="dropdown-menu" aria-labelledby="notif_dropdown"> <li> @@ -74,6 +74,26 @@ </li> </ul> </li> + + <li> + <a id="user_label" href=""> + <img src=""/> + <span></span> + </a> + </li> + + <li id="logout-link"> + <a href="#">Log out</a> + </li> + </ul> + + <ul id="logged-out-menu" class="nav navbar-nav navbar-right"> + <li id="register-link"> + <a href="/register">Register</a> + </li> + <li id="login-link"> + <a href="/login">Login</a> + </li> </ul> <div class="pagination-block"> diff --git a/src/routes/authentication.js b/src/routes/authentication.js index faa995b62d..8169758688 100644 --- a/src/routes/authentication.js +++ b/src/routes/authentication.js @@ -94,6 +94,9 @@ if (req.user && req.user.uid > 0) { winston.info('[Auth] Session ' + req.sessionID + ' logout (uid: ' + req.user.uid + ')'); + var ws = require('./../websockets'); + ws.logoutUser(req.user.uid); + req.logout(); } diff --git a/src/websockets.js b/src/websockets.js index eec83d0693..982bfaa8cb 100644 --- a/src/websockets.js +++ b/src/websockets.js @@ -1,8 +1,5 @@ -var SocketIO = require('socket.io').listen(global.server, { - log: false, - transports: ['websocket', 'xhr-polling', 'jsonp-polling', 'flashsocket'] -}), - cookie = require('cookie'), + +var cookie = require('cookie'), express = require('express'), user = require('./user.js'), Groups = require('./groups'), @@ -32,11 +29,26 @@ var SocketIO = require('socket.io').listen(global.server, { plugins = require('./plugins'), winston = require('winston'); -(function(io) { +//(function(io) { + var users = {}, userSockets = {}, rooms = {}; +module.exports.logoutUser = function(uid) { + if(userSockets[uid] && userSockets[uid].length) { + for(var i=0; i< userSockets[uid].length; ++i) { + userSockets[uid][i].emit('event:disconnect'); + userSockets[uid][i].disconnect(); + + if(!userSockets[uid]) + return; + } + } +} + +module.exports.init = function(io) { + global.io = io; io.sockets.on('connection', function(socket) { @@ -886,4 +898,4 @@ var SocketIO = require('socket.io').listen(global.server, { }); }); -}(SocketIO)); +}