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