forked from kartavyashankar/Mega-Project-RTE
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
59 lines (53 loc) · 1.77 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
51
52
53
54
55
56
57
58
59
const express = require('express');
const socketio = require('socket.io');
const http = require('http');
const {
userJoin,
getCurrentUser,
userLeave,
getRoomUsers
} = require('./utils/users');
const app = express();
const server = http.createServer(app);
const io = socketio(server);
// Set static folder
app.use(express.static('public'));
// Run when client connects
io.on('connection', socket => {
// Listen for code Shared
socket.on('joinRoom', ({ username, room }) => {
const user = userJoin(socket.id, username, room);
socket.join(user.room);
socket.emit('message', 'You have joined the room!');
socket.broadcast.to(user.room).emit('message', `${user.username} has joined the room!`);
io.to(user.room).emit('roomUsers', {
room: user.room,
users: getRoomUsers(user.room)
});
socket.on('chatMessage', msg => {
socket.broadcast.to(user.room).emit('chat', {
username : user.username,
text : msg
});
});
socket.on('send', ({shared_code, team_current}) => {
const user = getCurrentUser(socket.id);
socket.broadcast.to(user.room).emit('receive', {shared_code, team_current});
socket.broadcast.to(user.room).emit('message', `${user.username} has shared the code!`);
socket.emit('message', 'You have shared your code!');
});
});
socket.on('disconnect', () => {
const user = userLeave(socket.id);
if(user) {
socket.broadcast.to(user.room).emit('message', `${user.username} has left the room!`);
socket.emit('message', 'You are offline! Please reload the page...');
io.to(user.room).emit('roomUsers', {
room : user.room,
users: getRoomUsers(user.room)
});
}
});
});
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));