2020-06-23 13:29:38 -04:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
*/
|
2020-06-23 13:29:44 -04:00
|
|
|
const WebsocketLoadBalancer = require('./WebsocketLoadBalancer')
|
|
|
|
const DrainManager = require('./DrainManager')
|
|
|
|
const logger = require('logger-sharelatex')
|
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')
|
|
|
|
const { client_id } = req.params
|
|
|
|
const client = io.sockets.sockets[client_id]
|
|
|
|
|
|
|
|
if (!client) {
|
2021-09-14 04:36:24 -04:00
|
|
|
logger.debug({ client_id }, 'api: client already disconnected')
|
2020-06-23 13:29:44 -04:00
|
|
|
res.sendStatus(404)
|
|
|
|
return
|
|
|
|
}
|
2021-09-14 04:36:24 -04:00
|
|
|
logger.info({ client_id }, '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
|
|
|
}
|