-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
39 lines (31 loc) · 1.13 KB
/
index.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
const express = require('express');
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
app.use(express.static(__dirname));
// getting index.html file
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
const users = {};
io.on('connection', socket => {
// If any new user joins, let other users connected to the server know!
socket.on('new-user-joined', name => {
console.log(name);
users[socket.id] = name;
socket.broadcast.emit('user-joined', name);
});
// If someone sends a message, broadcast it to other people
socket.on('send', message => {
socket.broadcast.emit('receive', { message: message, name: users[socket.id] })
});
// If someone leaves the chat, let others know
socket.on('disconnect', message => {
socket.broadcast.emit('left', users[socket.id]);
delete users[socket.id];
});
})
const server_port = process.env.YOUR_PORT || process.env.PORT || 8000;
http.listen(server_port, () => {
console.log(`Server is running on..... http://localhost:${server_port}`);
});