Skip to content

Commit

Permalink
Add socket server backend on receiving new question.
Browse files Browse the repository at this point in the history
- Broadcast the question to each group in the array provided by source client socket.
  • Loading branch information
glutSolidSphere committed Mar 18, 2017
1 parent e7524f8 commit 474d006
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
33 changes: 19 additions & 14 deletions source/model/lobby.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,18 +240,18 @@ lobbyio.on ('connection', function (socket) {

//Update the client that login is successful.
socket.emit ('login', {
userType: socket.userType,
username: socket.username,
numUsers: lobby.numUsers,
defaultGroup: lobby.namespace
'userType': socket.userType,
'username': socket.username,
'numUsers': lobby.numUsers,
'defaultGroup': lobby.namespace
});

updateUsers (lobby, socket);

//Update all clients whenever a user successfully joins the lobby.
socket.broadcast.to(socket.namespace).emit ('user joined', {
username: socket.username,
numUsers: lobby.numUsers
'username': socket.username,
'numUsers': lobby.numUsers
});

//Initialise the socket
Expand All @@ -261,34 +261,39 @@ lobbyio.on ('connection', function (socket) {
// we tell the client to execute 'new message'
var lobby = lobbyList.getLobby(socket.moduleGroup, socket.tutorialGroup);
lobby.broadcastToGroup (socket, data.group, 'new message', {
username: socket.username,
message: data.message,
group: data.group
'username': socket.username,
'message': data.message,
'group': data.group
});
});

//Broadcast that the client is typing a message to other clients connected.
socket.on ('typing', function (data) {
socket.broadcast.to(socket.namespace).emit ('typing', {
username: socket.username,
group: data
'username': socket.username,
'group': data
});
});

//Broadcast when the client stops typing a message to other clients connected.
socket.on ('stop typing', function () {
socket.broadcast.to(socket.namespace).emit ('stop typing', {
username: socket.username
'username': socket.username
});
});

//Listen and execute broadcast of message on receiving 'new message' emission from the client.
//TODO separate out the different listeners for tutors and students.
socket.on ('new question', function (data) {
var lobby = lobbyList.getLobby(socket.moduleGroup, socket.tutorialGroup);
console.log (data);
lobby.questions.push (data.question);

data.groups.forEach (function (groupName, i) {
lobby.broadcastToGroup (socket, groupName, 'add question', {
'sourceId': socket.id,
'username': socket.username,
'question': data.question
});
});
//socket.broadcast.to(socket.namespace).emit ('add question', parsedData);
});

Expand Down
3 changes: 3 additions & 0 deletions source/view/Lobby/lobby.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -440,13 +440,16 @@
//Receives questions received from other users.
socket.on ('add question', function (data) {
console.log (data);
/*
data.answered = false;
data.submitted = false;
data.remarks = "";
data.options.forEach ( function (option) {
option.selected = false;
});
$scope.questions.push (data);
*/
});
//Receives questions received from other users.
Expand Down

0 comments on commit 474d006

Please sign in to comment.