-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
39 lines (28 loc) · 890 Bytes
/
server.js
File metadata and controls
39 lines (28 loc) · 890 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
31
32
33
34
35
36
37
38
39
const express = require('express');
const app = express();
const path =require('path')
const server = require('http').createServer(app);
const io = require('socket.io')(server,{
cors: {
origin: "*",
}
});
const PORT = process.env.PORT || 4000
let users=[{}];
app.use(express.static(path.join(__dirname,"./frontend/build")));
app.get("*",(req,res)=>{
res.sendFile(path.resolve(__dirname,"./frontend/build/index.html"))
})
io.on("connection",(socket)=>{
socket.on("sendMessage",(data)=>{
io.emit("recieveMsg",data)
})
socket.on("joined",(data)=>{
users[socket.id]=data.userName;
socket.broadcast.emit("recieveMsg",data)
})
socket.on("disconnect",()=>{
socket.broadcast.emit("recieveMsg",{type:"info",info:`${users[socket.id]} left chat`})
})
})
server.listen(PORT)