-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
99 lines (83 loc) · 2.7 KB
/
Copy pathapp.js
File metadata and controls
99 lines (83 loc) · 2.7 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const express = require("express");
const { spawn } = require("child_process");
require("dotenv").config();
const app = express();
const server = require("http").createServer(app);
const io = require("socket.io")(server);
const path = require("path");
const { v4: uuidv4 } = require("uuid");
const { ExpressPeerServer } = require("peer");
const peerServer = ExpressPeerServer(server, {
debug: true,
});
//EJS
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");
//body-parser
// app.use(express.urlencoded({ extended: true }));
//adding static files like css
// app.use(express.static(path.join(__dirname, "assets")));
app.use(express.static(path.join(__dirname, "assets1")));
app.use(express.static(path.join(__dirname, "public")));
// app.use(express.static('/public'));
app.use("/peerjs", peerServer);
app.get("/air_canvas", (req, res) => {
// res.render("air_canvas");
var dataToSend;
// spawn new child process to call the python script
python = spawn("python", ["./Script/script.py"]);
// collect data from script
python.stdout.on("data", function (data) {
console.log("Pipe data from python script ...");
dataToSend = data.toString();
});
python.stderr.on("data", function (data) {
console.log("Pipe data from python script ...");
dataToSend = data.toString();
});
// in close event we are sure that stream from child process is closed
python.on("close", (code) => {
console.log(`child process close all stdio with code ${code}`);
// send data to browser
res.redirect("/");
});
});
app.get("/", function (req, res) {
res.render("index1");
});
app.get("/room", (req, res) => {
res.redirect(`/${uuidv4()}`);
});
app.get("/:room", (req, res) => {
console.log(req.params.room);
if (req.params.room !== "index1" && req.params.room !== "notes1" && req.params.room !== "analyzer1" && req.params.room !== "script") {
res.render("room", { roomId: req.params.room });
} else {
if (req.params.room == "notes1") {
res.render("notes1");
} else if (req.params.room == "analyzer1") {
res.render("analyzer1");
} else {
res.render("index1");
}
}
});
app.get("/notes1", function (req, res) {
res.render("notes1");
});
app.get("/analyzer1", function (req, res) {
res.render("analyzer1");
});
io.on("connection", (socket) => {
socket.on("join-room", (roomId, userId) => {
console.log(roomId);
console.log(userId);
socket.join(roomId);
socket.broadcast.to(roomId).emit("user-connected", userId);
socket.on("message", (message) => {
io.to(roomId).emit("createMessage", message);
});
});
});
var PORT = process.env.PORT || 5000;
server.listen(PORT, console.log(`Server started on port ${PORT}`));