HTML and CSS Reference
In-Depth Information
When the server receives an answer message, it determines a result and then adjusts the score accordingly. For
the score, you can use the get and set functions on the socket, as shown in Listing 11-12; these let you store arbitrary
data that you want to retain for the lifetime of the socket.
Listing 11-12. The get and set Functions
socket.on('answer', function(msg) {
// Score answer
var currentScore;
var result = scoreAnswer(msg);
socket.get('score', function(err, score) {
currentScore = score;
// Emit message only to this client.
socket.emit('answer', {
score:(currentScore + result.points)
socket.set('score', currentScore + result.points);
Finally, you have a bit of code for retrieving a question at random, masking its answer, and sending it to all
connected sockets, as shown in Listing 11-13. The quiz game start screen is displayed in Figure 11-3 .
Listing 11-13. Sending a Random Question to All Connected Sockets
var sendQuestion = function (questionId) {
var q = _.clone(findQuestionById(questionId))
q.correctAnswer = -1;
io.sockets.emit('question', q);
setInterval(function() {
}, 10000);
Search WWH ::

Custom Search