2020-06-23 17:29:38 +00:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
*/
|
2020-06-23 17:29:44 +00:00
|
|
|
const WebsocketLoadBalancer = require('./WebsocketLoadBalancer')
|
|
|
|
const DrainManager = require('./DrainManager')
|
2021-12-14 13:00:35 +00:00
|
|
|
const logger = require('@overleaf/logger')
|
2015-03-03 17:15:19 +00:00
|
|
|
|
2020-07-07 10:06:02 +00:00
|
|
|
module.exports = {
|
|
|
|
sendMessage(req, res) {
|
2021-09-14 08:36:24 +00:00
|
|
|
logger.debug({ message: req.params.message }, 'sending message')
|
2020-06-23 17:29:44 +00:00
|
|
|
if (Array.isArray(req.body)) {
|
2020-07-07 10:06:02 +00:00
|
|
|
for (const payload of req.body) {
|
2020-06-23 17:29:44 +00:00
|
|
|
WebsocketLoadBalancer.emitToRoom(
|
|
|
|
req.params.project_id,
|
|
|
|
req.params.message,
|
|
|
|
payload
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
WebsocketLoadBalancer.emitToRoom(
|
|
|
|
req.params.project_id,
|
|
|
|
req.params.message,
|
|
|
|
req.body
|
|
|
|
)
|
|
|
|
}
|
2020-07-22 08:45:14 +00:00
|
|
|
res.sendStatus(204)
|
2020-07-07 10:06:02 +00:00
|
|
|
},
|
2020-02-24 12:28:22 +00:00
|
|
|
|
2020-07-07 10:06:02 +00:00
|
|
|
startDrain(req, res) {
|
2020-06-23 17:29:44 +00:00
|
|
|
const io = req.app.get('io')
|
|
|
|
let rate = req.query.rate || '4'
|
|
|
|
rate = parseFloat(rate) || 0
|
2021-09-14 08:36:24 +00:00
|
|
|
logger.info({ rate }, 'setting client drain rate')
|
2020-06-23 17:29:44 +00:00
|
|
|
DrainManager.startDrain(io, rate)
|
2020-07-22 08:45:14 +00:00
|
|
|
res.sendStatus(204)
|
2020-06-23 17:29:44 +00:00
|
|
|
},
|
2020-02-24 12:28:22 +00:00
|
|
|
|
2020-06-23 17:29:44 +00:00
|
|
|
disconnectClient(req, res, next) {
|
|
|
|
const io = req.app.get('io')
|
|
|
|
const { client_id } = req.params
|
|
|
|
const client = io.sockets.sockets[client_id]
|
|
|
|
|
|
|
|
if (!client) {
|
2021-09-14 08:36:24 +00:00
|
|
|
logger.debug({ client_id }, 'api: client already disconnected')
|
2020-06-23 17:29:44 +00:00
|
|
|
res.sendStatus(404)
|
|
|
|
return
|
|
|
|
}
|
2021-09-14 08:36:24 +00:00
|
|
|
logger.info({ client_id }, 'api: requesting client disconnect')
|
2020-06-23 17:29:44 +00:00
|
|
|
client.on('disconnect', () => res.sendStatus(204))
|
2020-07-07 10:06:02 +00:00
|
|
|
client.disconnect()
|
2021-07-13 11:04:45 +00:00
|
|
|
},
|
2020-06-23 17:29:44 +00:00
|
|
|
}
|