mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-18 21:55:59 +00:00
fixed problem with SSE #323
This commit is contained in:
@@ -25,6 +25,7 @@ const plugins = require('./controllers/plugins');
|
||||
const files = require('./controllers/files');
|
||||
const scheduler = require('./controllers/scheduler');
|
||||
const queryHistory = require('./controllers/queryHistory');
|
||||
const onFinished = require('on-finished');
|
||||
|
||||
const { rundir } = require('./utility/directories');
|
||||
const platformInfo = require('./utility/platformInfo');
|
||||
@@ -63,7 +64,10 @@ function start() {
|
||||
|
||||
// Tell the client to retry every 10 seconds if connectivity is lost
|
||||
res.write('retry: 10000\n\n');
|
||||
socket.setSseResponse(res);
|
||||
socket.addSseResponse(res);
|
||||
onFinished(req, () => {
|
||||
socket.removeSseResponse(res);
|
||||
});
|
||||
});
|
||||
|
||||
app.use(bodyParser.json({ limit: '50mb' }));
|
||||
|
||||
@@ -1,36 +1,33 @@
|
||||
let sseResponse = null;
|
||||
const _ = require('lodash');
|
||||
|
||||
const sseResponses = [];
|
||||
let electronSender = null;
|
||||
let init = [];
|
||||
let pingConfigured = false;
|
||||
|
||||
module.exports = {
|
||||
setSseResponse(value) {
|
||||
sseResponse = value;
|
||||
setInterval(() => this.emit('ping'), 29 * 1000);
|
||||
ensurePing() {
|
||||
if (!pingConfigured) {
|
||||
setInterval(() => this.emit('ping'), 29 * 1000);
|
||||
pingConfigured = true;
|
||||
}
|
||||
},
|
||||
addSseResponse(value) {
|
||||
sseResponses.push(value);
|
||||
this.ensurePing();
|
||||
},
|
||||
removeSseResponse(value) {
|
||||
_.remove(sseResponses, x => x == value);
|
||||
},
|
||||
setElectronSender(value) {
|
||||
electronSender = value;
|
||||
this.ensurePing();
|
||||
},
|
||||
emit(message, data) {
|
||||
if (electronSender) {
|
||||
if (init.length > 0) {
|
||||
for (const item of init) {
|
||||
electronSender.send(item.message, item.data == null ? null : item.data);
|
||||
}
|
||||
init = [];
|
||||
}
|
||||
electronSender.send(message, data == null ? null : data);
|
||||
} else if (sseResponse) {
|
||||
if (init.length > 0) {
|
||||
for (const item of init) {
|
||||
sseResponse.write(
|
||||
`event: ${item.message}\ndata: ${JSON.stringify(item.data == null ? null : item.data)}\n\n`
|
||||
);
|
||||
}
|
||||
init = [];
|
||||
}
|
||||
sseResponse.write(`event: ${message}\ndata: ${JSON.stringify(data == null ? null : data)}\n\n`);
|
||||
} else {
|
||||
init.push([{ message, data }]);
|
||||
}
|
||||
for (const res of sseResponses) {
|
||||
res.write(`event: ${message}\ndata: ${JSON.stringify(data == null ? null : data)}\n\n`);
|
||||
}
|
||||
},
|
||||
emitChanged(key) {
|
||||
|
||||
Reference in New Issue
Block a user