-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
52 lines (39 loc) · 1.34 KB
/
index.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const express = require("express");
const socket = require("socket.io");
// App setup
const PORT = process.env.PORT || 3000;
const app = express();
const server = app.listen(PORT, function () {
console.log(`http://localhost:${PORT}`);
});
app.use(express.urlencoded());
// Static files
app.use(express.static("Public"))
// const fs = require("fs");
// const Home = fs.readFileSync('Public/index.html');
// Socket setup
// app.get("/", (req, res) => {
// res.status(200).end(Home);
const io = socket(server);
const activeUsers = new Set();
io.on("connection", function (socket) {
console.log("Made socket connection");
socket.on("new user", function (data) {
socket.userId = data;
activeUsers.add(data);
io.emit("new user", [...activeUsers]);
});
socket.on("disconnect", () => {
activeUsers.delete(socket.userId);
io.emit("user disconnected", socket.userId);
});
socket.on("chat message", function (data) {
io.emit("chat message", data);
});
socket.on("typing", function (data) {
socket.broadcast.emit("typing", data);
});
socket.on("sent", function (data) {
socket.broadcast.emit("sent", data);
});
});