-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
33 lines (25 loc) · 870 Bytes
/
server.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
const express = require("express");
const path = require("path");
const app = express();
const server = require("http").createServer(app);
const io = require("socket.io")(server); // socket.io
app.use(express.static(path.join(__dirname, "public")));
app.set("views", path.join(__dirname, "public"));
app.engine("html", require("ejs").renderFile);
app.set("view engine", "html");
app.use("/", (req, res) => {
res.render("index.html");
});
let messages = [];
// Opens socket connection
io.on("connection", (socket) => {
console.log(`Socket conectado: ${socket.id}`);
// Sends the previous messages
socket.emit("previousMessages", messages);
// Receives a new message, store it and broadcast it for everyone
socket.on("sendMessage", (data) => {
messages.push(data);
socket.broadcast.emit("receivedMessage", data);
});
});
server.listen(3000);