-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
65 lines (57 loc) · 1.71 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
const express = require('express')
const exphbs = require('express-handlebars')
const app = express()
const server = require('http').createServer(app)
const io = require('socket.io')(server)
const favicon = require('serve-favicon')
const path = require('path')
const _favicon = favicon(path.join(__dirname, 'public', 'favicon.ico'))
const PythonShell = require('python-shell')
const pathRead = './MFRC522/Read.py'
const pathWrite = './MFRC522/Write.py'
//const readShell = new PythonShell('./MFRC522-python/Read.py')
//app.use(express.static(__dirname + '/config'));
//INITIAL index.html RENDER
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
})
//SOCKET - connection ESTABLISHED
io.on('connection', socket => {
console.log('user connected')
socket.on('startReading', () => {
let readShell = new PythonShell(pathRead);
let messages = [];
console.log('BE - start readshell')
readShell.on('message', message => {
messages.push(message.trim())
if(messages.length > 1){
io.emit('read', messages)
}
})
})
socket.on('startWriting', message => {
console.log('BE - input message: ', message)
//let writeShell = new PythonShell(pathWrite);
let options = {
mode: 'text',
args: [message]
}
console.log('BE - writeshell');
PythonShell.run(pathWrite, options, (err, res) => {
if (err) throw err;
console.log('results:', res)
})
//writeShell.send(message);
//writeShell.on('message', messageShell => {
// console.log('BE-writeshell-message:', messageShell)
//})
})
})
//PYTHON SHELL - READ
//readShell.on('message', message => {
// io.emit('read', message);
//})
//SERVER LISTEN ON PORT 3000
server.listen(3000, () => {
console.log('BE - server listetning on port 3000')
})