-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
46 lines (34 loc) · 1.09 KB
/
index.js
File metadata and controls
46 lines (34 loc) · 1.09 KB
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
// const io = require('socket.io')(8000, {
// cors: {
// origin: ['http://127.0.0.1:5501']
// }
// })
'use strict';
const express = require("express");
const socketIO = require('socket.io');
const PORT = process.env.PORT || 8000;
const path = require("path")
const app = express();
const staticPath = path.join(__dirname,"/public");
app.use(express.static(staticPath));
const server = app.listen(PORT,()=>{
console.log("Listining PORT 8000 visit: http://localhost:8000")
});
const io = socketIO(server);
const users = {};
io.on('connection', socket => {
console.log("Backend Connected")
console.log("user connected: " + socket.id);
socket.on('new-user-joined', name => {
console.log("New user", name);
users[socket.id] = name;
socket.broadcast.emit("user-joined", name);
});
socket.on('send', message => {
socket.broadcast.emit('receive', { message: message, name: users[socket.id] })
})
socket.on('disconnect', message => {
socket.broadcast.emit('leave',users[socket.id])
delete users[socket.id]
})
})