-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
150 lines (146 loc) · 5 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const io = require('socket.io')();
let roomsDB = {};
io.on('connection', (socket) => {
let username = 'Anonymous';
let usersRoom = '';
socket.emit('connect', socket.id);
socket.on('sendUsername', function(incommingUserName) {
console.log('setUsername to: ', incommingUserName);
username = incommingUserName;
});
socket.on('getSessionId', function () {
console.log('getSessionId', socket.id)
socket.emit('setSessionId', socket.id);
});
// once a client has connected, we expect to get a ping from them saying what room they want to join
socket.on('room', function(room) {
console.log("joining room: " + room);
console.log("Client Id: " + socket.id);
console.log("Username: " + username);
socket.join(room);
usersRoom = room;
if(roomsDB[room]){
if(checkSessionId(socket.id, room)){
console.log(username + ' already in room ' + room);
} else {
console.log('Room ' + room +' exists set username: ' + username);
roomsDB[room].push({
userName: username,
sessionId: socket.id,
message: '',
showVotes: false,
});
}
} else {
console.log('New Room ' + room +' set username: ' + username);
roomsDB[room] = [];
roomsDB[room].push({
userName: username,
sessionId: socket.id,
message: '',
showVotes: false,
});
}
//Send out
for(user in roomsDB[room]) {
console.log(roomsDB[room][user]);
}
io.in(room).emit('readRoomUsers', roomsDB[room]);
io.in(room).emit('readRoomId', room);
});
socket.on('getroom', function(){
io.in(room).emit('readRoomId', usersRoom);
});
socket.on('disconnect', function(){
console.log('user disconnected from room' + usersRoom);
if(roomsDB[usersRoom]){
console.log("found room:"+ usersRoom);
for (user in roomsDB[usersRoom]) {
console.log('username: ' + roomsDB[usersRoom][user].userName);
if(roomsDB[usersRoom][user].sessionId === socket.id) {
console.log('splice out found sessionId');
roomsDB[usersRoom].splice(user, 1);
io.in(usersRoom).emit('readRoomUsers', roomsDB[usersRoom]);
}
}
if(roomsDB[usersRoom].length === 0){
console.log(usersRoom + ' is empty removing room');
delete roomsDB[usersRoom];
console.log(roomsDB);
}
socket.leave(usersRoom);
usersRoom = '';
} else {
console.log('problem with room:' + usersRoom, roomsDB);
}
});
socket.on('leaveRoom', function () {
console.log('user left from room: ' + usersRoom);
if (roomsDB[usersRoom]) {
console.log("found room:" + usersRoom);
for (user in roomsDB[usersRoom]) {
console.log('username: ' + roomsDB[usersRoom][user].userName);
console.log('roomsDB[usersRoom][user].sessionId' + roomsDB[usersRoom][user].sessionId);
if (roomsDB[usersRoom][user].sessionId === socket.id) {
console.log('splice out found sessionId');
roomsDB[usersRoom].splice(user, 1);
}
}
if (roomsDB[usersRoom].length === 0) {
console.log(usersRoom + ' is empty removing room');
delete roomsDB[usersRoom];
io.in(usersRoom).emit('readRoomUsers', []);
} else {
io.in(usersRoom).emit('readRoomUsers', roomsDB[room]);
}
io.in(usersRoom).emit('readRoomId', '');
socket.leave(usersRoom);
usersRoom = '';
} else {
console.log('problem with room:' + usersRoom, roomsDB);
}
});
socket.on('readMessage', (newMessage) => {
console.log("server chat message: ", newMessage)
console.log("server chat room: ", usersRoom)
console.log("socket.id: ", socket.id)
console.log(roomsDB[usersRoom])
for (user in roomsDB[usersRoom]) {
console.log("roomsDB[usersRoom][person].sessionId: ", roomsDB[usersRoom][user].sessionId)
if(roomsDB[usersRoom][user].sessionId === socket.id) {
roomsDB[usersRoom][user].message = newMessage;
}
}
io.in(usersRoom).emit('readRoomUsers', roomsDB[usersRoom]);
});
socket.on('showVotes', () => {
for (user in roomsDB[usersRoom]) {
console.log("User " + roomsDB[usersRoom][user].userName +" Wanted To Show Votes")
if (roomsDB[usersRoom][user].sessionId === socket.id) {
roomsDB[usersRoom][user].showVotes = true;
} else {
roomsDB[usersRoom][user].showVotes = false;
}
}
io.in(usersRoom).emit('readRoomUsers', roomsDB[usersRoom]);
});
socket.on('clearVotes', () => {
console.log("Clear Votes")
for (user in roomsDB[usersRoom]) {
roomsDB[usersRoom][user].message = '';
roomsDB[usersRoom][user].showVotes = '';
}
io.in(usersRoom).emit('readRoomUsers', roomsDB[usersRoom]);
});
});
const checkSessionId = (sessionId, room) => {
roomsDB[room].forEach(nextRoom => {
if(sessionId === nextRoom.sessionId) {
return true;
}
});
return false;
}
const port = 8000;
io.listen(port);
console.log('listening on port ', port);