-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
211 lines (178 loc) · 5.39 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Local imports
const room = require("./room");
const user = require("./user");
const config = require("./config");
const speech = require("./speech");
// External dependencies
const fs = require("fs");
const base64url = require("base64url");
const wav = require("wav");
// Express/endpoints setup
const app = require("express")();
const http = require("http").Server(app);
const io = require("socket.io")(http);
const bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json({ limit: '500mb' }));
app.use(bodyParser.urlencoded({ limit: '500mb', extended: true }));
// Current active rooms
let rooms = {};
// REST endpoints
/*
POST /createroom
Request parameters: none
Response parameters:
room_id: ID that uniquely identifies the room
(used for students to join)
*/
app.post("/createroom", (req, res) => {
const newRoom = room.createRoom();
rooms[newRoom.id] = newRoom;
res.status(200).send({
"room_id": newRoom.id
});
console.log(`Room ${newRoom.id} created`);
});
/*
POST /newquestion
Request parameters:
uid: Asker's userID
rid: Room ID for question
question: Question text
Response parameters: none (status code only)
*/
app.post("/newquestion", (req, res) => {
const uid = req.body ? req.body.uid : null;
const rid = req.body ? req.body.rid : null;
const question = req.body ? req.body.question : null;
if (uid && rid && question) {
queueQuestion(uid, rid, question);
res.sendStatus(200);
} else {
res.status(500).send("userID, roomID, or question text is missing");
}
});
/*
GET /getquestions
Request parameters:
rid: Room ID
Response parameters:
questions: Array of {user: User, text: str} objects representing
pending questions
*/
app.post("/getquestions", (req, res) => {
const rid = req.body ? req.body.rid : null;
if (!rid) {
res.status(500).send("Room ID (rid) field required");
} else {
res.status(200).send({
"questions": rooms[rid].questions
})
}
});
/*
POST /removequestion
Request parameters:
rid: Room ID
Response parameters: none (status code only)
*/
app.post("/removequestion", (req, res) => {
const rid = req.body ? req.body.rid : null;
const text = req.body ? req.body.text : null;
if (!rid || !text) {
res.status(500).send("Room ID (rid) and text fields required");
} else {
try {
const texts = rooms[rid].questions.map(q => q.text);
const index = texts.indexOf(text);
rooms[rid].questions.pop(index);
res.sendStatus(200);
} catch (e) {
res.status(500).send("Question couldn't be removed – list is empty");
}
}
});
/*
POST /newaudio
Request parameters:
rid: Room ID
data: Base64-encoded audio
Response parameters: none (status code only)
*/
let current = 0;
let buffer = "";
app.post("/newaudio", (req, res) => {
const rid = req.body ? req.body.rid : null;
const data = req.body ? req.body.data : null;
const decoded = base64url.decode(data);
if (current % 5 == 0) {
const path = `streams/stream_${rid}.wav`;
const writer = new wav.FileWriter(path);
writer.write(buffer);
writer.end();
const data = fs.readFileSync(path).toString("base64");
if (!rid || !data) {
res.status(500).send("Room ID (rid) and base 64-encoded data required for this endpoint");
} else {
speech.transcribe(data, (err, transcription) => {
if (err) {
res.status(500).send(`Transcription failed with error ${err}`);
} else {
console.log(transcription ? transcription : "NOPE");
res.sendStatus(200);
}
});
}
buffer = "";
} else {
buffer += decoded;
}
current++;
});
/*
POST /transcript
Request parameters:
rid: Room ID
Response parameters:
text: Current text of transcript
*/
app.post("/transcript", (req, res) => {
const rid = req.body ? req.body.rid : null;
if (!rid || !rooms[rid]) {
res.status(500).send("Room ID required for this endpoint");
} else {
res.status(200).send({
"text": rooms[rid].transcript
});
}
});
// Start the app
app.listen(config.port, () => {
console.log(`Server started on port ${config.port}`);
waitForConnections();
});
// Utility functions
// Waits for socket connections from a user
function waitForConnections() {
io.on("connection", (socket) => {
socket.on("start", (data) => {
const rid = data["rid"];
const name = data["name"];
let connected = true;
if (rooms[rid]) {
const uid = user.createUser(name, rooms[rid].users);
rooms[rid].users[uid].socket = socket;
} else {
connected = false;
}
socket.emit("join", { "status": `User ${connected ? "connected" : "not connected"}` });
});
});
}
// Queues a question for the room
function queueQuestion(uid, rid, question) {
rooms[rid].questions.push({
"user": rooms[rid].users[uid],
"text": question
});
}