Skip to content

Commit

Permalink
[NEW] Disconnect users from websocket when away from the login screen…
Browse files Browse the repository at this point in the history
… for 10min (#11086)
  • Loading branch information
rodrigok authored and sampaiodiego committed Jun 11, 2018
1 parent 401e2e2 commit e2d8494
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
52 changes: 52 additions & 0 deletions packages/rocketchat-lib/client/lib/LoginPresence.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
export const LoginPresence = {
awayTime: 600000, //10 minutes
started: false,

startTimer() {
LoginPresence.stopTimer();
if (!LoginPresence.awayTime) {
return;
}
this.timer = setTimeout(LoginPresence.disconnect, LoginPresence.awayTime);
},
stopTimer() {
clearTimeout(this.timer);
},
disconnect() {
const status = Meteor.status();
if (status && status.status !== 'offline') {
if (!Meteor.userId() && RocketChat.settings.get('Accounts_AllowAnonymousRead') !== true) {
Meteor.disconnect();
}
}
LoginPresence.stopTimer();
},
connect() {
const status = Meteor.status();
if (status && status.status === 'offline') {
Meteor.reconnect();
}
},
start() {
if (LoginPresence.started) {
return;
}

window.addEventListener('focus', () => {
LoginPresence.stopTimer();
LoginPresence.connect();
});

window.addEventListener('blur', () => {
LoginPresence.startTimer();
});

if (!window.document.hasFocus()) {
LoginPresence.startTimer();
}

LoginPresence.started = true;
}
};

LoginPresence.start();
4 changes: 3 additions & 1 deletion packages/rocketchat-lib/client/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { RocketChatAnnouncement } from './RocketChatAnnouncement';
import { RoomSettingsEnum, RoomTypeConfig, RoomTypeRouteConfig, UiTextContext } from '../../lib/RoomTypeConfig';
import { hide, leave, erase } from './ChannelActions';
import { call } from './callMethod';
import { LoginPresence } from './LoginPresence';

export {
call,
Expand All @@ -22,5 +23,6 @@ export {
RoomTypeConfig,
RoomTypeRouteConfig,
UiTextContext,
RocketChatAnnouncement
RocketChatAnnouncement,
LoginPresence
};

0 comments on commit e2d8494

Please sign in to comment.