Last active
August 29, 2015 14:21
-
-
Save having-fun-coding/1d2ccdc0a630bc9d589e to your computer and use it in GitHub Desktop.
Real Time Web with Node.js | 6.7 Answering Questions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Clients can also answer each other's questions, | |
so let's build that feature by first listening for | |
the 'answer' event on the client, which will send us | |
both the question and answer, which we want to broadcast | |
out to the rest of the connected clients. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var app = express(); | |
var server = require('http').createServer(app); | |
var io = require('socket.io')(server); | |
io.sockets.on('connection', function(client) { | |
console.log("Client connected..."); | |
client.on('answer', function(question, answer) { | |
client.broadcast.emit('answer', question, answer); | |
}); | |
client.on('question', function(question) { | |
if(!client.question_asked) { | |
client.question_asked = true; | |
client.broadcast.emit('question', question); | |
} | |
}); | |
}); | |
server.listen(8080); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment