summaryrefslogtreecommitdiff
path: root/Year_3/Web/notifications/index.js
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2022-07-27 16:26:52 +0200
committerSanto Cariotti <santo@dcariotti.me>2022-07-27 16:26:52 +0200
commitf3007671f230087d017036992844a04fdc812c22 (patch)
tree39a1e1a17ea8744bc40f34a8091aafeae06b4f99 /Year_3/Web/notifications/index.js
parentcf7e0630c77b8f0e9660e3e633e10ea3db116387 (diff)
add app
Diffstat (limited to 'Year_3/Web/notifications/index.js')
-rw-r--r--Year_3/Web/notifications/index.js46
1 files changed, 46 insertions, 0 deletions
diff --git a/Year_3/Web/notifications/index.js b/Year_3/Web/notifications/index.js
new file mode 100644
index 0000000..be867c0
--- /dev/null
+++ b/Year_3/Web/notifications/index.js
@@ -0,0 +1,46 @@
+const http = require("http");
+const express = require("express");
+const { Server } = require("socket.io");
+const path = require("path");
+
+const app = express();
+const server = http.createServer(app);
+const io = new Server(server);
+
+app.set("view engine", "pug");
+app.set("views", path.join(__dirname, "views"));
+app.use(express.static("assets"));
+app.use((req, res, next) => {
+ console.log(`${req.method} ${req.path}`);
+ next();
+});
+
+var click = 0;
+
+io.on("connection", (socket) => {
+ console.log(socket.id);
+
+ io.to(socket.id).emit("message", "hello man, ur now connected");
+ socket.emit("message", "there s another user here!");
+
+ socket.on("disconnect", () => {
+ console.log("bye, sir");
+ });
+
+ socket.on("new click", () => {
+ io.to(socket.id).emit("clicked", ++click);
+ });
+
+ setInterval(() => {
+ io.to(socket.id).emit("new notification");
+ }, 1000);
+});
+
+app.get("/", (req, res) => {
+ ctx = {
+ id: 1,
+ };
+ res.render("index", ctx);
+});
+
+server.listen(3000);