Skip to content

Instantly share code, notes, and snippets.

@blairdow
Created November 28, 2016 20:39
Show Gist options
  • Select an option

  • Save blairdow/68a1a169701a54002b167c67aa6277af to your computer and use it in GitHub Desktop.

Select an option

Save blairdow/68a1a169701a54002b167c67aa6277af to your computer and use it in GitHub Desktop.
Jeopardy friends sockets... to be moved server side
//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);})
}
// 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