bigbluebutton-Github/bigbluebutton-html5/imports/startup/server/ClientConnections.js

173 lines
5.4 KiB
JavaScript
Raw Normal View History

import Logger from './logger';
2020-09-10 19:49:22 +08:00
import userLeaving from '/imports/api/users/server/methods/userLeaving';
import { extractCredentials } from '/imports/api/common/server/helpers';
import Users from '/imports/api/users';
import { check } from 'meteor/check';
2021-01-30 01:27:13 +08:00
const { enabled, syncInterval } = Meteor.settings.public.syncUsersWithConnectionManager;
class ClientConnections {
constructor() {
2020-10-22 01:27:40 +08:00
Logger.debug('Initializing client connections structure', { logCode: 'client_connections_init' });
this.connections = new Map();
setInterval(() => {
this.print();
2020-09-10 19:49:22 +08:00
}, 30000);
2020-10-22 01:27:40 +08:00
2021-01-30 01:27:13 +08:00
if (enabled) {
const syncConnections = Meteor.bindEnvironment(() => {
this.syncConnectionsWithServer();
});
2020-10-22 01:27:40 +08:00
2021-01-30 01:27:13 +08:00
setInterval(() => {
syncConnections();
}, syncInterval);
}
}
add(sessionId, connection) {
2020-10-22 01:27:40 +08:00
Logger.info('Client connections add called', { logCode: 'client_connections_add', extraInfo: { sessionId, connection } });
if (!sessionId || !connection) {
Logger.error(`Error on add new client connection. sessionId=${sessionId} connection=${connection.id}`,
{ logCode: 'client_connections_add_error', extraInfo: { sessionId, connection } }
);
return;
}
const { meetingId, requesterUserId: userId } = extractCredentials(sessionId);
check(meetingId, String);
check(userId, String);
if (!meetingId) {
2021-05-07 19:38:19 +08:00
Logger.error(`Error on add new client connection. sessionId=${sessionId} connection=${connection.id}`,
{ logCode: 'client_connections_add_error_meeting_id_null', extraInfo: { meetingId, userId } }
);
return false;
}
if (!this.exists(meetingId)) {
Logger.info(`Meeting not found in connections: meetingId=${meetingId}`);
this.createMeetingConnections(meetingId);
}
const sessionConnections = this.connections.get(meetingId);
if (sessionConnections.has(userId) && sessionConnections.get(userId).includes(connection.id)) {
Logger.info(`Connection already exists for user. userId=${userId} connectionId=${connection.id}`);
return false;
}
2020-09-10 19:49:22 +08:00
connection.onClose(Meteor.bindEnvironment(() => {
userLeaving(meetingId, userId, connection.id);
}));
2020-10-22 01:27:40 +08:00
Logger.info(`Adding new connection for sessionId=${sessionId} connection=${connection.id}`);
if (!sessionConnections.has(userId)) {
Logger.info(`Creating connections poll for ${userId}`);
sessionConnections.set(userId, []);
return sessionConnections.get(userId).push(connection.id);
} else {
return sessionConnections.get(userId).push(connection.id);
}
}
createMeetingConnections(meetingId) {
Logger.info(`Creating meeting in connections. meetingId=${meetingId}`);
if (!this.exists(meetingId))
return this.connections.set(meetingId, new Map());
}
exists(meetingId) {
return this.connections.has(meetingId);
}
getConnectionsForClient(sessionId) {
const { meetingId, requesterUserId: userId } = extractCredentials(sessionId);
check(meetingId, String);
check(userId, String);
return this.connections.get(meetingId)?.get(userId);
}
print() {
2020-10-22 01:27:40 +08:00
const mapConnectionsObj = {};
this.connections.forEach((value, key) => {
2020-10-22 01:27:40 +08:00
mapConnectionsObj[key] = {};
value.forEach((v, k) => {
2020-10-22 01:27:40 +08:00
mapConnectionsObj[key][k] = v;
});
2020-10-22 01:27:40 +08:00
});
Logger.info('Active connections', mapConnectionsObj);
}
removeClientConnection(sessionId, connectionId = null) {
Logger.info(`Removing connectionId for user. sessionId=${sessionId} connectionId=${connectionId}`);
const { meetingId, requesterUserId: userId } = extractCredentials(sessionId);
check(meetingId, String);
check(userId, String);
const meetingConnections = this.connections.get(meetingId);
if (meetingConnections?.has(userId)) {
const filteredConnections = meetingConnections.get(userId).filter(c => c !== connectionId);
return connectionId && filteredConnections.length ? meetingConnections.set(userId, filteredConnections) : meetingConnections.delete(userId);
}
return false;
}
removeMeeting(meetingId) {
Logger.debug(`Removing connections for meeting=${meetingId}`);
return this.connections.delete(meetingId);
}
2020-10-22 01:27:40 +08:00
syncConnectionsWithServer() {
Logger.info('Syncing ClientConnections with server');
const activeConnections = Array.from(Meteor.server.sessions.keys());
Logger.debug(`Found ${activeConnections.length} active connections in server`);
const onlineUsersId = onlineUsers.map(({ userId }) => userId);
const usersQuery = { userId: { $nin: onlineUsersId } };
const userWithoutConnectionIds = Users.find(usersQuery, { fields: { meetingId: 1, userId: 1 } }).fetch();
const removedUsersWithoutConnection = Users.remove(usersQuery);
if (removedUsersWithoutConnection) {
Logger.info(`Removed ${removedUsersWithoutConnection} users that are not connected`);
Logger.info(`Clearing connections`);
try {
userWithoutConnectionIds
.forEach(({ meetingId, userId }) => {
this.removeClientConnection(`${meetingId}--${userId}`);
});
} catch (err) {
Logger.error('Error on sync ClientConnections', err);
}
}
2020-10-22 01:27:40 +08:00
}
}
if (!process.env.BBB_HTML5_ROLE || process.env.BBB_HTML5_ROLE === 'frontend') {
Logger.info("ClientConnectionsSingleton was created")
const ClientConnectionsSingleton = new ClientConnections();
export default ClientConnectionsSingleton;
}