-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
30 lines (28 loc) · 735 Bytes
/
index.js
File metadata and controls
30 lines (28 loc) · 735 Bytes
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
const express = require("express");
const app = express();
const http = require("http").Server(app);
const io = require("socket.io");
// socket.io config
const socket = io(http, {
cors: {
origin: "*",
},
});
app.use(express.static(__dirname + "/public"));
// when a client connects
socket.on("connection", function (socket) {
console.log("a user connected");
// when a user send a message
socket.on("msg", (data) => {
console.log("mesaje del cliente", data);
socket.broadcast.emit("msg", data);
});
// when the user disconnects
socket.on("disconnect", function () {
console.log("user disconnected");
});
});
// start the server
http.listen(3000, () => {
console.log("listening on port 3000");
});