-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
151 lines (131 loc) · 3.72 KB
/
app.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
// LectureConnect Server
// Node and Socket.io server for running backend api for AccessMath
// Author: Michael Timbrook <[email protected]>
// Requires
var manage = require('./ManagementConsole')
, https = require('https')
, http = require('http')
, sio = require('socket.io')
, fs = require('fs')
, LectureRuntime = require('./LectureRuntime')
, lectures = require('./lectures');
// Socket SSL
// var options = {
// key : fs.readFileSync('./src/certs/server.key'),
// cert : fs.readFileSync('./src/certs/server.crt')
// };
var socketServer = http.createServer();
var io = sio.listen(socketServer);
socketServer.listen(9000);
// Connecting to the database
var mongoose = require('mongoose')
db = require('./db');
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
// Connected
});
// use lecture schema
var Lecture = mongoose.model('Lecture');
// Storing clients for later access
var clients = {};
exports.clientCount = function() {
var count = 0;
for (key in clients) {
count++;
}
return count;
}
// Setup the server
io.configure('development', function() {
io.set('log level', 1);
});
// Create connections
io.sockets.on('connection', function(socket) {
// Add the client to the dictionary
console.log(socket.id + " connected");
socket.name = "Unnamed";
socket.emit('Status',{info : "You have connected to AccessLecture development server."});
// Get name
socket.emit('get-name');
socket.on('set-name', function(data) {
socket.name = data;
});
// Handle dashboard
socket.on('manage-connect', function(data) {
// console.log(data);
});
// Current Data Query
socket.on('data-query', function (data) {
if (data.length < 2) return;
var cleandata = data.replace(/[^\w\s]/gi,'');
var re = new RegExp(cleandata, 'i');
var query = Lecture.find({name : re});
query.select("name description");
query.exec(function(err, lecture) {
if (!err) {
if (lecture != null) {
socket.emit('manage-founddata', lecture);
}
}
});
});
// Handle lecture request
socket.on('lecture-request', function(data){
console.log("Client request: " + data);
// Lookup object
var query = Lecture.findOne({name : data});
query.exec(function(err, lecture) {
if (!err) {
if (lecture == null) {
console.log("Lecture not found");
socket.emit('lecture-response-failed', {message : "Lecture not found"})
return;
}
console.log(lecture);
socket.emit('lecture-response', lecture);
}
})
});
// Handles streaming request
socket.on('steaming-request', function(data){
console.log("Client " + socket.id + " requested connection to " + data);
if (data in lectures) {
if (socket.id in clients) {
clients[socket.id].removeClient(socket.id);
}
var lect = lectures[data];
clients[socket.id] = lect;
lect.addClient(socket.id, socket);
} else {
socket.emit('info', {request : "failed"});
};
});
// Handles client disconnection
socket.on('disconnect', function() {
if (socket.id in clients && clients[socket.id]) {
clients[socket.id].removeClient(socket.id);
delete clients[socket.id];
}
console.log(socket.id + " has disconnected");
});
// Set up streamer to lecture.
socket.on('connect-teacher', function(data){
console.log(data);
if ( data.name in lectures ) {
var lecture = lectures[data.name];
lecture.setStream(socket);
} else {
socket.emit('status', {status : 'error', message : 'No lecture found'});
}
});
});
// Restore last state
Lecture.find().exec(function(err, objs) {
if (err) console.log(err);
for(i in objs) {
if( objs[i].isActive ) {
var lectureRuntime = new LectureRuntime(objs[i].name, objs[i].description, objs[i]);
lectures[objs[i].name] = lectureRuntime;
}
}
});