-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
50 lines (39 loc) · 1.11 KB
/
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const express = require("express")
const http = require("http")
const cors = require("cors")
const { Server } = require("socket.io")
// const path = require("path")
const app = express();
app.use(cors())
const server = http.createServer(app)
// const baseUrl = "http://localhost:3000"
const baseUrl = "https://chat-room-28.netlify.app"
const io = new Server(server, {
cors: {
origin: baseUrl,
methods: ["GET", "POST"],
}
})
io.on("connection", (socket) => {
// console.log(`${socket.id} connected`);
socket.on("join_room", (data) => {
socket.join(data)
// console.log(`User ${socket.id} joined room ${data}`);
})
socket.on("send_message", (data) => {
// console.log(data);
socket.to(data.room).emit("receive_message", data)
})
socket.on("disconnect", () => {
// console.log(`${socket.id} disconnected`);
})
})
// app.get("/", (req, res) => {
// res.send("vanakam frans")
// })
app.get("/", (req, res) => {
res.send("Server running")
})
server.listen(process.env.PORT || 5000, () => {
console.log("Server running");
})