-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathserver.js
More file actions
32 lines (25 loc) · 836 Bytes
/
server.js
File metadata and controls
32 lines (25 loc) · 836 Bytes
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
const fs = require("fs");
const process = require("process");
const express = require("express");
const ws = require("express-ws");
const pty = require("node-pty");
const html = fs.readFileSync("index.html");
const app = express();
const expressWs = ws(app);
app.get("/", (req, res) => {
res.setHeader("Content-Type", "text/html");
res.send(html);
});
app.ws("/ws", (ws) => {
const term = pty.spawn("python3", [], { name: "xterm-color" });
setTimeout(() => term.kill(), 3600 * 1000); // session timeout
term.on("data", (data) => {
try {
ws.send(data);
} catch (err) {}
});
ws.on("message", (data) => term.write(data));
});
// Prevent malformed packets from crashing server.
expressWs.getWss().on("connection", (ws) => ws.on("error", console.error));
app.listen(parseInt(process.env.PORT), "0.0.0.0");