-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
87 lines (68 loc) · 2.44 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const express = require('express');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
const { ExpressPeerServer } = require('peer');
const peerServer = ExpressPeerServer(server, {
debug: true
});
app.use('/peerjs', peerServer);
const { v4: uuidV4 } = require('uuid')
const {
enterUser,
getAllRoomUsers,
getCurrentUser,
userLeave
} = require("./routes/users");
// middlewares
app.use(express.urlencoded({extended: true}));
app.use(express.json());
app.set('view engine', 'ejs');
app.use("/", express.static(__dirname + '/public'));
// request to create a call
app.get('/create_call', (req, res) => {
res.redirect(`/${uuidV4()}`);
})
app.get('/:room', (req, res) => {
res.render('room', { roomId: req.params.room } );
})
// socket connection
io.on('connection', (socket) => {
socket.on('join-room', (roomId, userId, userName) => {
socket.join(roomId);
let user = enterUser(userId, userName, roomId, socket.id);
console.log(user);
// let roomUsers = getAllRoomUsers(roomId);
// if (roomUsers.length>1) {
// socket.to(roomUsers[0].socketId).emit("data_dijiye", socket.id);
// console.log("apna time");
// }
socket.to(roomId).emit('user-connected', userId);
console.log("A user is connected:", userName, "User ID:", userId, "Room ID:", roomId);
// socket.on("data_lijiye", (data, socketId) => {
// console.log("hum hain yaha");
// socket.to(socketId).emit("init", data);
// });
socket.on('drawing', (data) => socket.broadcast.emit('drawing', data));
socket.on('clearBoard', () => {
socket.to(roomId).emit('clearBoard');
});
socket.on('editor-change', (saamaan)=>{
socket.to(roomId).emit('editor-update-kar-rey', saamaan);
})
// messages functionality
socket.on('message', (message, username) => {
//send message to the same room
io.to(roomId).emit('createMessage', message, username);
});
socket.on('disconnect', () => {
let user = userLeave(socket.id);
socket.broadcast.emit('user-disconnected', userId);
console.log("Nikal gaya");
})
})
})
const port = process.env.PORT || 3000;
server.listen(port, ()=>{
console.log("Server started at: http://localhost:"+port);
});