-
Notifications
You must be signed in to change notification settings - Fork 11
/
server.js
114 lines (91 loc) · 3.62 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const path = require('path');
const express = require('express')
const http = require('http')
const moment = require('moment');
const socketio = require('socket.io');
const PORT = process.env.PORT || 3000;
const app = express();
const server = http.createServer(app);
const io = socketio(server);
app.use(express.static(path.join(__dirname, 'public')));
let rooms = {};
let socketroom = {};
let socketname = {};
let micSocket = {};
let videoSocket = {};
let roomBoard = {};
io.on('connect', socket => {
socket.on("join room", (roomid, username) => {
socket.join(roomid);
socketroom[socket.id] = roomid;
socketname[socket.id] = username;
micSocket[socket.id] = 'on';
videoSocket[socket.id] = 'on';
if (rooms[roomid] && rooms[roomid].length > 0) {
rooms[roomid].push(socket.id);
socket.to(roomid).emit('message', `${username} joined the room.`, 'Bot', moment().format(
"h:mm a"
));
io.to(socket.id).emit('join room', rooms[roomid].filter(pid => pid != socket.id), socketname, micSocket, videoSocket);
}
else {
rooms[roomid] = [socket.id];
io.to(socket.id).emit('join room', null, null, null, null);
}
io.to(roomid).emit('user count', rooms[roomid].length);
});
socket.on('action', msg => {
if (msg == 'mute')
micSocket[socket.id] = 'off';
else if (msg == 'unmute')
micSocket[socket.id] = 'on';
else if (msg == 'videoon')
videoSocket[socket.id] = 'on';
else if (msg == 'videooff')
videoSocket[socket.id] = 'off';
socket.to(socketroom[socket.id]).emit('action', msg, socket.id);
})
socket.on('video-offer', (offer, sid) => {
socket.to(sid).emit('video-offer', offer, socket.id, socketname[socket.id], micSocket[socket.id], videoSocket[socket.id]);
})
socket.on('video-answer', (answer, sid) => {
socket.to(sid).emit('video-answer', answer, socket.id);
})
socket.on('new icecandidate', (candidate, sid) => {
socket.to(sid).emit('new icecandidate', candidate, socket.id);
})
socket.on('message', (msg, username, roomid) => {
io.to(roomid).emit('message', msg, username, moment().format(
"h:mm a"
));
})
socket.on('getCanvas', () => {
if (roomBoard[socketroom[socket.id]])
socket.emit('getCanvas', roomBoard[socketroom[socket.id]]);
});
socket.on('draw', (newx, newy, prevx, prevy, color, size) => {
socket.to(socketroom[socket.id]).emit('draw', newx, newy, prevx, prevy, color, size);
})
socket.on('clearBoard', () => {
socket.to(socketroom[socket.id]).emit('clearBoard');
});
socket.on('store canvas', url => {
roomBoard[socketroom[socket.id]] = url;
})
socket.on('disconnect', () => {
if (!socketroom[socket.id]) return;
socket.to(socketroom[socket.id]).emit('message', `${socketname[socket.id]} left the chat.`, `Bot`, moment().format(
"h:mm a"
));
socket.to(socketroom[socket.id]).emit('remove peer', socket.id);
var index = rooms[socketroom[socket.id]].indexOf(socket.id);
rooms[socketroom[socket.id]].splice(index, 1);
io.to(socketroom[socket.id]).emit('user count', rooms[socketroom[socket.id]].length);
delete socketroom[socket.id];
console.log('--------------------');
console.log(rooms[socketroom[socket.id]]);
//toDo: push socket.id out of rooms
});
})
//
server.listen(PORT, () => console.log(`Server is up and running on port ${PORT}`));