-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
149 lines (114 loc) · 4.57 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
var express = require('express');
var https = require('https');
var _ = require('underscore');
var fs = require('fs');
var options = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
}
var app = express();
app.use(express.static('./public'));
app.use(express.bodyParser());
var port = process.env.PORT || 3800;
var io = require('socket.io').listen(https.createServer(options, app).listen(port));
var tutors={};
var students={};
var studentsInWaiting={};
var test='';
var studentsWhoAreAskingQuestion = {};
io.sockets.on('connection', function(socket){
//console.log('current sockets are '+ io.sockets.clients());
var clients = io.sockets.clients();
var roomglobal = null;
socket.on('PeerMessage', function(data){
console.log('rtcmessage is ' + data);
socket.broadcast.emit('PeerMessageFromSocket', data);
});
socket.on('QNAPeerMessage', function(data){
console.log('rtcmessage is ' + data);
socket.broadcast.emit('QNAPeerMessageFromSocket', data);
});
socket.on('joinClassRequest', function(data){
if(tutors[data.classid]){
var tutorsocket = tutors[data.classid].socketInfo;
tutorsocket.emit('joinClassRequestFromStudent', {classid: data.classid, studentname: data.name});
if(studentsInWaiting[data.classid]){
studentsInWaiting[data.classid].push({name: data.name, socketInfo: socket, classid: data.classid, id: socket.id });
}
else{
var newStudentsInWaitingArray= [];
newStudentsInWaitingArray[0] = {name: data.name, socketInfo: socket, classid: data.classid, id: socket.id };
studentsInWaiting[data.classid] = newStudentsInWaitingArray;
}
}
});
socket.on('studentAccept', function(data){
if(data.accept) {
var studentsInWaitingForThisClass = studentsInWaiting[data.classid];
var student = _.where(studentsInWaitingForThisClass, {name: data.studentname});
if(student.length>0){
var studentSocket = student[0].socketInfo;
studentSocket.emit('studentAcceptResponse', {message: 'success', classid: data.classid});
studentSocket.join(data.classid);
if(students[data.classid])
{
students[data.classid].push({name: data.studentname, socketInfo: studentSocket, id: studentSocket.id });
}
else
{
var newStudentArray = [];
newStudentArray[0] = {name: data.studentname, socketInfo: studentSocket, id: studentSocket.id};
students[data.classid] = newStudentArray;
}
var studentsList = students[data.classid];
var studentsdataArr = [];
for(var i=0;i<studentsList.length; i++){
studentsdataArr.push(_.omit(studentsList[i], 'socketInfo'));
}
console.log('next is studentJoinedAnnouncement' + studentsdataArr);
io.sockets.in(data.classid).emit('studentJoinedAnnouncement', {studentsData: studentsdataArr});
roomglobal = data.classid;
}
}
});
socket.on('createClassrequest',function(data){
if(tutors[data.classid])
{
socket.emit('createclass_status', {message: 'This id is already taken. Try some other id', classid: null});
return;
}
socket.join(data.classid);
tutors[data.classid] = {name: data.name, socketInfo: socket};
test = 'Good';
socket.emit('createclass_status', {message: 'success', classid: data.classid});
});
socket.emit('message', {message: 'Welcome to the chat'});
socket.on('send',function(data){
io.sockets.in(roomglobal).emit('message',data);
});
socket.on('sendClassData', function(data){
io.sockets.in(roomglobal).emit('editorContentChanged',data.editorContent);
});
socket.on('studentQuestion', function(data){
console.log('studentQuestion class id is '+ data.classid);
console.log(tutors[data.classid]);
console.log(tutors);
if(tutors[data.classid])
{
console.log('tutor still there in studentQuestion');
var tutorsocket = tutors[data.classid].socketInfo;
tutorsocket.emit('studentAskingQuestion', {classid: data.classid, studentname: data.name, studentId: socket.id });
if(studentsWhoAreAskingQuestion[data.classid]){
studentsWhoAreAskingQuestion[data.classid].push({name: data.name, socketInfo: socket, classid: data.classid, id: socket.id });
}
else{
var newStudentsAskingQuestions= [];
newStudentsAskingQuestions[0] = {name: data.name, socketInfo: socket, classid: data.classid, id: socket.id };
studentsWhoAreAskingQuestion[data.classid] = newStudentsAskingQuestions;
}
}
});
socket.on('endQnASession',function(data){
io.sockets.in(roomglobal).emit('qnASessionEnded', data);
});
});