diff options
author | Santo Cariotti <santo@dcariotti.me> | 2022-07-27 16:26:52 +0200 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2022-07-27 16:26:52 +0200 |
commit | f3007671f230087d017036992844a04fdc812c22 (patch) | |
tree | 39a1e1a17ea8744bc40f34a8091aafeae06b4f99 /Year_3/Web/notifications/index.js | |
parent | cf7e0630c77b8f0e9660e3e633e10ea3db116387 (diff) |
add app
Diffstat (limited to 'Year_3/Web/notifications/index.js')
-rw-r--r-- | Year_3/Web/notifications/index.js | 46 |
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); |