2019-07-02 03:00:27 +08:00
|
|
|
import { Meteor } from 'meteor/meteor';
|
|
|
|
import Logger from '/imports/startup/server/logger';
|
|
|
|
import _ from 'lodash';
|
2020-02-07 04:47:28 +08:00
|
|
|
import { extractCredentials } from '/imports/api/common/server/helpers';
|
2019-07-02 03:00:27 +08:00
|
|
|
|
|
|
|
const COLLECTION_NAME = 'ping-pong';
|
2019-07-09 03:49:09 +08:00
|
|
|
const INTERVAL_IN_SETTINGS = (Meteor.settings.public.pingPong.clearUsersInSeconds) * 1000;
|
|
|
|
const INTERVAL_TIME = INTERVAL_IN_SETTINGS < 10000 ? 10000 : INTERVAL_IN_SETTINGS;
|
|
|
|
const PONG_INTERVAL_IN_SETTINGS = (Meteor.settings.public.pingPong.pongTimeInSeconds) * 1000;
|
2019-07-10 00:41:04 +08:00
|
|
|
const PONG_INTERVAL = PONG_INTERVAL_IN_SETTINGS >= (INTERVAL_TIME / 2)
|
2019-07-09 03:49:09 +08:00
|
|
|
? (INTERVAL_TIME / 2) : PONG_INTERVAL_IN_SETTINGS;
|
2019-07-02 03:00:27 +08:00
|
|
|
|
2020-02-07 04:47:28 +08:00
|
|
|
function pingPong() {
|
|
|
|
if (!this.userId) {
|
|
|
|
return; // TODO-- is there a more appropriate set to return?
|
|
|
|
}
|
|
|
|
const { meetingId, requesterUserId } = extractCredentials(this.userId);
|
2019-07-02 03:00:27 +08:00
|
|
|
const id = _.uniqueId('pong-');
|
|
|
|
Logger.info(`Starting ping-pong publish for userId: ${requesterUserId}`);
|
|
|
|
const pongSender = (interval) => {
|
|
|
|
const payload = {
|
|
|
|
pong: {
|
|
|
|
message: 'pong',
|
|
|
|
time: Date.now(),
|
|
|
|
meetingId,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
let fn = this.added.bind(this);
|
|
|
|
if (interval) fn = this.changed.bind(this);
|
|
|
|
fn(COLLECTION_NAME, id, payload);
|
|
|
|
};
|
|
|
|
pongSender();
|
|
|
|
this.ready();
|
2019-07-09 03:49:09 +08:00
|
|
|
const interval = Meteor.setInterval(() => pongSender(true), PONG_INTERVAL);
|
2019-07-02 03:00:27 +08:00
|
|
|
|
|
|
|
this.onStop(() => {
|
|
|
|
Meteor.clearInterval(interval);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Meteor.publish('ping-pong', pingPong);
|