-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
77 lines (58 loc) · 2.19 KB
/
server.js
File metadata and controls
77 lines (58 loc) · 2.19 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const express = require('express');
const moment = require('moment');
const {userJoin, userLeave, getRoomUsers} = require('./public/user')
const app = express()
const http = require('http').createServer(app);
const time = moment().format('h:mm a');
const PORT = process.env.PORT || 5000
http.listen(PORT, () =>{
console.log(`Listening on port ${PORT}`);
})
app.use(express.static(__dirname + '/public'))
app.get('/', (req, res) =>{
// res.send("Hello Satish")
res.sendFile(__dirname + '/home.html');
})
// Node server which will handle socket io connections
const io = require('socket.io')(http)
const users = {};
io.on('connection', (socket) =>{
console.log('Connected...');
socket.on('current-time', time =>{
socket.emit('presentTime', time);
})
socket.on('joinRoom', ({username, room})=>{
const user = userJoin(socket.id, username, room);
socket.join(user.room);
// Send users and room info
io.to(user.room).emit('roomUsers', {
room: user.room,
users: getRoomUsers(user.room)
});
// If any new user joins, let other users connected to the server know!
socket.on('new-user-joined', name =>{
users[socket.id] = name;
socket.broadcast.to(user.room).emit('user-joined', name);
socket.emit('all-joined-users', name);
});
// If someone sends a message, broadcast it to other people
socket.on('send', message =>{
// const name = getCurrentUser(socket.id);
socket.broadcast.to(user.room).emit('receive', {message: message, name: users[socket.id], time: time})
});
// If someone leaves the chat, let others know
socket.on('disconnect', message =>{
const user = userLeave(socket.id);
if(socket.broadcast.to(user.room).emit('left', users[socket.id])){
delete users[socket.id];
}
// Send users and room info
if(user){
io.to(user.room).emit('roomUsers', {
room: user.room,
users: getRoomUsers(user.room)
});
}
});
})
})