-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·68 lines (48 loc) · 1.7 KB
/
server.js
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
// Create server
let port = process.env.PORT || 8000;
let express = require('express');
let app = express();
let server = require('http').createServer(app).listen(port, function () {
console.log('Server listening at port: ', port);
});
// Tell server where to look for files
app.use(express.static('public'));
// Create socket connection
let io = require('socket.io').listen(server);
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const fs = require('fs');
// Listen for individual clients to connect
io.sockets.on('connection',
// Callback function on connection
// Comes back with a socket object
function (socket) {
console.log("We have a new client: " + socket.id);
// Listen for username
// Stick the username on the socket object
socket.on('recipeName', (recipeName)=>{
runPyScripts(recipeName.filename, socket);
})
// Listen for this client to disconnect
socket.on('disconnect', function () {
console.log("Client has disconnected " + socket.id);
});
}
);
async function runPyScripts(filename, socket){
let command = "python3 recipe.py -i " + filename;
const response1 = await exec(command);
console.log(response1.stdout);
const keywords = await fs.readFileSync('public/keywords.txt', 'utf-8');
console.log(keywords);
const send_keywords = await socket.emit('keywords', keywords);
command = 'python3 useownimg.py -i ' + filename + ' -o output.jpg'
console.log(command)
const response2 = await exec(command);
console.log(response2.stdout);
let message = {
inputImages: filename.split(',')
}
const msg_to_client = await socket.emit('message', message);
return 'All Done'
}