diff options
author | Santo Cariotti <santo@dcariotti.me> | 2022-07-26 15:41:36 +0200 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2022-07-26 15:41:36 +0200 |
commit | cf7e0630c77b8f0e9660e3e633e10ea3db116387 (patch) | |
tree | 17959c07604896b2db792598c4a82ec9d34c4281 /Year_3/Web/chat-socket-io/index.js | |
parent | d29b49fc89b608794a2ab9df84e745b864b93b2f (diff) |
Usernames and exclude sender
Diffstat (limited to 'Year_3/Web/chat-socket-io/index.js')
-rw-r--r-- | Year_3/Web/chat-socket-io/index.js | 23 |
1 files changed, 19 insertions, 4 deletions
diff --git a/Year_3/Web/chat-socket-io/index.js b/Year_3/Web/chat-socket-io/index.js index 6224c50..b59cb72 100644 --- a/Year_3/Web/chat-socket-io/index.js +++ b/Year_3/Web/chat-socket-io/index.js @@ -13,19 +13,34 @@ app.get("/", (req, res) => { res.render("index"); }); -app.get("/chat.js", (req, res) => { - res.sendFile(__dirname + "/chat.js"); +app.use(express.static("assets")); + +app.get("*", (req, res) => { + res.render("404"); }); +const users = {}; + io.on("connection", (socket) => { - console.log("A user is connected"); + console.log("A user is connected: " + socket.id); + io.to(socket.id).emit("set user", socket.id); + users[socket.id] = socket.id; socket.on("disconnect", () => { console.log("A user is disconnected"); }); socket.on("chat message", (message) => { - io.emit("chat message", message); + socket.broadcast.emit("chat message", message); + }); + + socket.on("handle error", (msg) => { + io.emit("handle error", msg); + }); + + socket.on("set user", (username) => { + users[socket.id] = username; + io.to(socket.id).emit("set user", username); }); }); |