2020-09-01 20:07:56 +08:00
|
|
|
import Logger from './logger';
|
2020-09-10 19:49:22 +08:00
|
|
|
import userLeaving from '/imports/api/users/server/methods/userLeaving';
|
2020-09-01 20:07:56 +08:00
|
|
|
import { extractCredentials } from '/imports/api/common/server/helpers';
|
2021-01-27 03:22:32 +08:00
|
|
|
import Users from '/imports/api/users';
|
2021-03-17 00:15:41 +08:00
|
|
|
import { check } from 'meteor/check';
|
2021-01-27 03:22:32 +08:00
|
|
|
|
2021-01-30 01:27:13 +08:00
|
|
|
const { enabled, syncInterval } = Meteor.settings.public.syncUsersWithConnectionManager;
|
2020-09-01 20:07:56 +08:00
|
|
|
|
|
|
|
class ClientConnections {
|
|
|
|
constructor() {
|
2020-10-22 01:27:40 +08:00
|
|
|
Logger.debug('Initializing client connections structure', { logCode: 'client_connections_init' });
|
2020-09-01 20:07:56 +08:00
|
|
|
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);
|
|
|
|
}
|
2020-09-01 20:07:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2020-09-01 20:07:56 +08:00
|
|
|
|
|
|
|
const { meetingId, requesterUserId: userId } = extractCredentials(sessionId);
|
|
|
|
|
2021-03-17 00:15:41 +08:00
|
|
|
check(meetingId, String);
|
|
|
|
check(userId, String);
|
|
|
|
|
2021-01-27 03:22:32 +08:00
|
|
|
if (!meetingId) {
|
2021-05-07 19:38:19 +08:00
|
|
|
Logger.error(`Error on add new client connection. sessionId=${sessionId} connection=${connection.id}`,
|
2021-01-27 03:22:32 +08:00
|
|
|
{ logCode: 'client_connections_add_error_meeting_id_null', extraInfo: { meetingId, userId } }
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-01 20:07:56 +08:00
|
|
|
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}`);
|
|
|
|
|
2020-09-01 20:07:56 +08:00
|
|
|
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);
|
|
|
|
|
2021-03-17 00:15:41 +08:00
|
|
|
check(meetingId, String);
|
|
|
|
check(userId, String);
|
|
|
|
|
2020-09-01 20:07:56 +08:00
|
|
|
return this.connections.get(meetingId)?.get(userId);
|
|
|
|
}
|
|
|
|
|
|
|
|
print() {
|
2020-10-22 01:27:40 +08:00
|
|
|
const mapConnectionsObj = {};
|
2020-09-01 20:07:56 +08:00
|
|
|
this.connections.forEach((value, key) => {
|
2020-10-22 01:27:40 +08:00
|
|
|
mapConnectionsObj[key] = {};
|
2020-09-01 20:07:56 +08:00
|
|
|
|
|
|
|
value.forEach((v, k) => {
|
2020-10-22 01:27:40 +08:00
|
|
|
mapConnectionsObj[key][k] = v;
|
2020-09-01 20:07:56 +08:00
|
|
|
});
|
|
|
|
|
2020-10-22 01:27:40 +08:00
|
|
|
});
|
|
|
|
Logger.info('Active connections', mapConnectionsObj);
|
2020-09-01 20:07:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
removeClientConnection(sessionId, connectionId = null) {
|
|
|
|
Logger.info(`Removing connectionId for user. sessionId=${sessionId} connectionId=${connectionId}`);
|
|
|
|
const { meetingId, requesterUserId: userId } = extractCredentials(sessionId);
|
|
|
|
|
2021-03-17 00:15:41 +08:00
|
|
|
check(meetingId, String);
|
|
|
|
check(userId, String);
|
|
|
|
|
2021-01-27 03:22:32 +08:00
|
|
|
const meetingConnections = this.connections.get(meetingId);
|
2020-09-01 20:07:56 +08:00
|
|
|
|
|
|
|
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() {
|
2021-01-27 03:22:32 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-09-01 20:07:56 +08:00
|
|
|
}
|
|
|
|
|
2021-07-01 04:08:00 +08:00
|
|
|
if (!process.env.BBB_HTML5_ROLE || process.env.BBB_HTML5_ROLE === 'frontend') {
|
|
|
|
Logger.info("ClientConnectionsSingleton was created")
|
2020-09-01 20:07:56 +08:00
|
|
|
|
2021-07-01 04:08:00 +08:00
|
|
|
const ClientConnectionsSingleton = new ClientConnections();
|
|
|
|
|
|
|
|
export default ClientConnectionsSingleton;
|
|
|
|
}
|