From f3007671f230087d017036992844a04fdc812c22 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Wed, 27 Jul 2022 16:26:52 +0200 Subject: add app --- Year_3/Web/notifications/index.js | 46 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Year_3/Web/notifications/index.js (limited to 'Year_3/Web/notifications/index.js') 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); -- cgit v1.2.3-18-g5258