-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
38 lines (30 loc) · 993 Bytes
/
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
const { Socket } = require("dgram");
const express = require("express");
const { Server } = require("http");
const path = require("path");
const app = express();
const PORT = process.env.PORT || 4000;
const server = app.listen(PORT, () =>
console.log(`server running on port${PORT}`)
);
const io = require("socket.io")(server);
app.use(express.static(path.join(__dirname, "public")));
let socketsConnected = new Set();
io.on("connection", onConnected);
function onConnected(socket) {
console.log(socket.id);
socketsConnected.add(socket.id);
io.emit("clients-total", socketsConnected.size);
socket.on("disconnect", () => {
console.log("socket dissconnected", socket.id);
socketsConnected.delete(socket.id);
io.emit("clients-total", socketsConnected.size);
});
socket.on("message", (data) => {
console.log(data);
socket.broadcast.emit("chat-messaeg", data);
});
socket.on("feedback", (data) => {
socket.broadcast.emit("feedback", data);
});
}