Created
November 28, 2016 20:39
-
-
Save blairdow/68a1a169701a54002b167c67aa6277af to your computer and use it in GitHub Desktop.
Jeopardy friends sockets... to be moved server side
This file contains hidden or 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
| //below code to check answers and generate questions should probably be moved to the socket server side, instead of client side | |
| //check answer | |
| function checkAnswer(){ | |
| var msgCheck = newMsg.value.trim() | |
| var answerCheck = answer | |
| if (msgCheck.toLowerCase() == answerCheck.toLowerCase()) { | |
| console.log(userEmail + ' is the dawg now') | |
| socket.emit('right-answer', { | |
| answer: answer, | |
| email: userEmail | |
| }) | |
| //send new question if answer is right | |
| setTimeout(function(){ | |
| generateQuestion() | |
| }, 5000) | |
| } | |
| } | |
| //get question from API and send to socket | |
| function generateQuestion() { | |
| $.get('/api/random').then(function(data) { | |
| var stripHTML = data.info.answer.replace(/<[^>]*>/g, "") | |
| var stripAns = stripHTML.replace(/[^a-zA-Z ]/g, "") | |
| question = { | |
| question: data.info.question, | |
| category: data.info.category, | |
| answer: stripAns | |
| } | |
| socket.emit('new-question', question) | |
| }, function(err) {console.error(err);}) | |
| } |
This file contains hidden or 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
| // io.js | |
| var io = require('socket.io')(); | |
| var questionLoad = require('./config/question') | |
| // Listen for new connections from clients (socket) | |
| io.on('connection', function (socket) { | |
| socket.on('new-question', function (data){ | |
| io.emit('update-question', data) | |
| }) | |
| socket.on('add-message', function (data) { | |
| io.emit('add-message', data); | |
| }); | |
| socket.on('new-question', function (data){ | |
| questionLoad.setQuestion = data | |
| // question = generateQuestion() | |
| io.emit('update-question', data) | |
| }) | |
| socket.on('right-answer', function(data){ | |
| io.emit('right-answer', data) | |
| }) | |
| }); | |
| // io represents socket.io on the server - let's export it | |
| module.exports = io; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment