-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
103 lines (89 loc) · 3.16 KB
/
app.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import "dotenv/config";
import { fileURLToPath } from "node:url";
import http from "node:http";
import path from "node:path";
import express from "express";
import { Server } from "socket.io";
import favicon from "serve-favicon";
import clock1 from "./scripts/default/index.js";
import clock2 from "./scripts/gbn/index.js";
import clock3 from "./scripts/gbm/index.js";
import clock4 from "./scripts/cbm-sameUnits/index.js";
import clock5 from "./scripts/cbm-differentUnits/index.js";
import clock6 from "./scripts/ask/index.js";
const app = express();
const httpServer = http.createServer(app);
const io = new Server(httpServer);
app.use(favicon(path.join(path.dirname(fileURLToPath(import.meta.url)), "public", "favicon.ico")));
app.set("view engine", "pug");
app.use("/", express.static(path.join(path.dirname(fileURLToPath(import.meta.url)), "/public")));
app.get("/", (req, res) => res.render("landing", { title: "cbmjs demo" }));
app.get("/1", (req, res) => res.render("clock_1", { title: "Default JavaScript" }));
app.get("/2", (req, res) => res.render("clock_2", { title: "Get by name" }));
app.get("/3", (req, res) => res.render("clock_3", { title: "Get by meaning" }));
app.get("/4", (req, res) => res.render("clock_4", { title: "Call by meaning with existing units" }));
app.get("/5", (req, res) => res.render("clock_5", { title: "Call by meaning with different units" }));
app.get("/6", (req, res) => res.render("clock_6", { title: "Just Ask" }));
app.get("*", (req, res) => res.status(404).send("Hmm... How did you end up here?"));
let io1;
io.of("/1").on("connection", (socket) => {
socket.on("clock1", () => {
io1 = setInterval(() => {
const times = clock1();
io.of("/1").emit("data1", times);
}, 1000);
});
socket.on("disconnect", () => clearInterval(io1));
});
let io2;
io.of("/2").on("connection", (socket) => {
socket.on("clock2", () => {
io2 = setInterval(async () => {
const times = await clock2();
io.of("/2").emit("data2", times);
}, 1000);
});
socket.on("disconnect", () => clearInterval(io2));
});
let io3;
io.of("/3").on("connection", (socket) => {
socket.on("clock3", () => {
io3 = setInterval(async () => {
const times = await clock3();
io.of("/3").emit("data3", times);
}, 1000);
});
socket.on("disconnect", () => clearInterval(io3));
});
let io4;
io.of("/4").on("connection", (socket) => {
socket.on("clock4", () => {
io4 = setInterval(async () => {
const times = await clock4();
io.of("/4").emit("data4", times);
}, 1000);
});
socket.on("disconnect", () => clearInterval(io4));
});
let io5;
io.of("/5").on("connection", (socket) => {
socket.on("clock5", () => {
io5 = setInterval(async () => {
const times = await clock5();
io.of("/5").emit("data5", times);
}, 1000);
});
socket.on("disconnect", () => clearInterval(io5));
});
let io6;
io.of("/6").on("connection", (socket) => {
socket.on("clock6", () => {
io6 = setInterval(async () => {
const times = await clock6();
io.of("/6").emit("data6", times);
}, 1000);
});
socket.on("disconnect", () => clearInterval(io6));
});
const port = process.env.PORT || 3000;
httpServer.listen(port, () => console.log("Server started at http://localhost:%s. Have fun. 😀", port));