2020-06-23 13:29:38 -04:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-06-23 13:29:34 -04:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
let HttpApiController;
|
|
|
|
const WebsocketLoadBalancer = require("./WebsocketLoadBalancer");
|
|
|
|
const DrainManager = require("./DrainManager");
|
|
|
|
const logger = require("logger-sharelatex");
|
2015-03-03 12:15:19 -05:00
|
|
|
|
2020-06-23 13:29:34 -04:00
|
|
|
module.exports = (HttpApiController = {
|
|
|
|
sendMessage(req, res, next) {
|
|
|
|
logger.log({message: req.params.message}, "sending message");
|
|
|
|
if (Array.isArray(req.body)) {
|
2020-06-23 13:29:38 -04:00
|
|
|
for (const payload of Array.from(req.body)) {
|
2020-06-23 13:29:34 -04:00
|
|
|
WebsocketLoadBalancer.emitToRoom(req.params.project_id, req.params.message, payload);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
WebsocketLoadBalancer.emitToRoom(req.params.project_id, req.params.message, req.body);
|
|
|
|
}
|
|
|
|
return res.send(204);
|
|
|
|
}, // No content
|
2016-10-24 11:36:09 -04:00
|
|
|
|
2020-06-23 13:29:34 -04:00
|
|
|
startDrain(req, res, next) {
|
|
|
|
const io = req.app.get("io");
|
|
|
|
let rate = req.query.rate || "4";
|
|
|
|
rate = parseFloat(rate) || 0;
|
|
|
|
logger.log({rate}, "setting client drain rate");
|
|
|
|
DrainManager.startDrain(io, rate);
|
|
|
|
return res.send(204);
|
|
|
|
},
|
2020-02-24 07:28:22 -05:00
|
|
|
|
2020-06-23 13:29:34 -04:00
|
|
|
disconnectClient(req, res, next) {
|
|
|
|
const io = req.app.get("io");
|
|
|
|
const {
|
|
|
|
client_id
|
|
|
|
} = req.params;
|
|
|
|
const client = io.sockets.sockets[client_id];
|
2020-02-24 07:28:22 -05:00
|
|
|
|
2020-06-23 13:29:34 -04:00
|
|
|
if (!client) {
|
|
|
|
logger.info({client_id}, "api: client already disconnected");
|
|
|
|
res.sendStatus(404);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
logger.warn({client_id}, "api: requesting client disconnect");
|
|
|
|
client.on("disconnect", () => res.sendStatus(204));
|
|
|
|
return client.disconnect();
|
|
|
|
}
|
|
|
|
});
|