Repository navigation
Expand file tree
/
Copy pathws-server.js
More file actions
78 lines (59 loc) · 2.27 KB
/
Copy pathws-server.js
File metadata and controls
78 lines (59 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const app = require("./http-server").app;
const port = require("./http-server").port;
const expressWs = require('express-ws');
const { spawn, exec } = require('node:child_process');
const path = require('node:path');
expressWs(app)
// Variables
var queryCount = 0;
const chunksEnabled = false;
app.ws('/mapws', (ws, req) => {
ws.on('open', () => {
console.log('WebSocket was opened');
});
ws.on('close', () => {
console.log('WebSocket was closed');
});
ws.on('message', (msg) => { // This contains similar POST request code
console.log(msg);
try {
msg = JSON.parse(msg);
// Parse request and update query count
bounds = JSON.stringify(msg['bounds']).replaceAll('\"', "\'"); // Need to replace double quotes with single for command line input
county = msg['county'];
queryCount++;
myQID = queryCount;
if (chunksEnabled) { // TODO: Move chunking into SHPQuery now that there is async output so server doesn't need to spawn a million processes
// var chunkingProcess = exec(`python ./dataQuery.py getChunks ${bounds}`, (error, stdout, stderr) => {
// var chunks = JSON.parse(stdout);
// // TODO cancel process if new query comes in
// // Spawn python subprocess to make queried file
// chunks.forEach((currChunk) => {
// console.log(currChunk);
// currChunk = JSON.stringify(currChunk).replaceAll('\"', "\'")
runScriptSendWS('python', ["-u", "dataQuery.py", myQID, bounds, county, chunksEnabled], ws);
// })
// });
} else {
runScriptSendWS('python', ["-u", "dataQuery.py", myQID, bounds, county, chunksEnabled], ws); // https://stackoverflow.com/a/56138420/20891092 Insane amount of time spent figuring out it needed a -u credit to Nadav Har'El
}
} catch(e) {
console.log(e);
}
});
});
async function runScriptSendWS(command, args, ws) {
var child = spawn(command, args);
child.stdout.setEncoding('utf8');
child.stdout.on("data", (data) => {
console.log('stdout: ' + data);
ws.send(data);
});
child.stderr.on("data", (data) => {
console.log('stderr: ' + data);
});
child.on('error', (err) => {
console.log("Failed to start child.", err);
});
}
app.listen(port);