-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
86 lines (76 loc) · 1.73 KB
/
main.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
var os = require('os');
var fs = require('fs');
var path = require('path');
var pty = require('node-pty');
var socketio = require('socket.io')
var express = require('express');
const bodyParser = require('body-parser');
var app = express();
var server = require('http').createServer(app);
var t; // ひとまず端末は高々1個
app.use(bodyParser.json());
app.use(express.static('public'));
app.post('/setTermSize', function(req, res, next) {
console.log(req.body);
if (t) {
var cols = req.body.cols;
var rows = req.body.rows;
t.resize(cols, rows);
res.json({status: 'ok'});
}
else {
res.json({status: 'err'});
}
});
app.get('/get', function (req, res, next) {
fs.readFile("./sketch/main.sh", function (err, data) {
if (err) {
next(err);
}
else {
//res.send(data);
res.json({script: data.toString()});
}
});
});
app.post('/post', (req, res) => {
console.log(req.body);
fs.writeFile('./sketch/main.sh', req.body.script, function(err, result) {
if (err) {
next(err);
}
else {
//res.send('ok');
res.json({status: 'ok'});
}
});
});
var sock = socketio(server);
var shell = 'bash';
sock.on('connect', function(s) {
console.log('connected');
if (t) {
t.kill();
t = null;
}
t = pty.spawn(shell, [], {
name: 'xterm-color',
cols: 80,
rows: 24,
//cwd: process.env.HOME,
cwd: path.join(__dirname, 'sketch'),
env: process.env
});
t.on('data', function(d) {
s.emit('data', d);
});
s.on('data', function(d) {
t.write(d);
});
s.on('disconnect', function() {
console.log('disconnected');
t.kill();
});
});
server.listen(3001);
console.log("PiDE is listening to PORT:" + server.address().port);