Fix the disconnects/reconnects
Fix the disconnects/reconnects
This commit is contained in:
248
hqnode.js
248
hqnode.js
@@ -34,144 +34,160 @@ request('https://api-quiz.hype.space/shows/now', (error, response, body) => {
|
||||
|
||||
let TIMEOUT = null;
|
||||
|
||||
const ping = (ws) => {
|
||||
ws.ping();
|
||||
TIMEOUT = setTimeout(() => {
|
||||
const ping = (ws, reconnect) => {
|
||||
try {
|
||||
ws.ping();
|
||||
TIMEOUT = setTimeout(() => {
|
||||
console.log('Master connection closed. Attempting to reconnnect...');
|
||||
reconnect();
|
||||
}, 2000);
|
||||
} catch (error) {
|
||||
console.log('Master connection closed. Attempting to reconnnect...');
|
||||
ws.terminate();
|
||||
ws = new WebSocket(socketUrl, playerOptions);
|
||||
}, 2000);
|
||||
reconnect();
|
||||
}
|
||||
};
|
||||
|
||||
const pong = () => {
|
||||
clearTimeout(TIMEOUT);
|
||||
};
|
||||
|
||||
const ws = new WebSocket(socketUrl, options);
|
||||
|
||||
ws.on('error', (err) => {
|
||||
console.log('An error occurred in the WebSocket: ', err);
|
||||
});
|
||||
|
||||
ws.on('pong', () => pong(TIMEOUT));
|
||||
|
||||
ws.on('open', () => {
|
||||
setInterval(() => ping(ws, TIMEOUT), 10000);
|
||||
console.log('Master connected to websocket');
|
||||
|
||||
playersInGame.forEach((player) => {
|
||||
let pTimeout = null;
|
||||
const playerOptions = {
|
||||
headers: {
|
||||
Authorization: `Bearer ${player.accessToken}`,
|
||||
},
|
||||
};
|
||||
const pws = new WebSocket(socketUrl, playerOptions);
|
||||
const ping = (ws) => {
|
||||
const connectPlayer = (player) => {
|
||||
let pTimeout = null;
|
||||
const playerOptions = {
|
||||
headers: {
|
||||
Authorization: `Bearer ${player.accessToken}`,
|
||||
},
|
||||
};
|
||||
const pws = new WebSocket(socketUrl, playerOptions);
|
||||
const ping = (ws, reconnect) => {
|
||||
try {
|
||||
ws.ping();
|
||||
pTimeout = setTimeout(() => {
|
||||
console.log(`Player ${player.username} connection closed. Attempting to reconnect...`);
|
||||
ws.terminate();
|
||||
ws = new WebSocket(socketUrl, playerOptions);
|
||||
reconnect();
|
||||
}, 2000);
|
||||
};
|
||||
|
||||
const pong = () => {
|
||||
clearTimeout(pTimeout);
|
||||
};
|
||||
pws.on('pong', pong);
|
||||
pws.on('open', () => {
|
||||
setInterval(() => ping(pws), 10000);
|
||||
console.log(`\nPlayer ${player.username} has connected to websocket`);
|
||||
pws.send(JSON.stringify({ type:'subscribe', broadcastId }), () => {
|
||||
console.log(`\tPlayer ${player.username} has subscribed to game`);
|
||||
player.activate(broadcastId, pws);
|
||||
});
|
||||
} catch (error) {
|
||||
console.log(`Player ${player.username} connection closed. Attempting to reconnect...`);
|
||||
reconnect();
|
||||
}
|
||||
};
|
||||
|
||||
const pong = () => {
|
||||
clearTimeout(pTimeout);
|
||||
};
|
||||
pws.on('pong', pong);
|
||||
pws.on('open', () => {
|
||||
setInterval(() => ping(pws, () => connectPlayer(player)), 10000);
|
||||
console.log(`\nPlayer ${player.username} has connected to websocket`);
|
||||
pws.send(JSON.stringify({ type:'subscribe', broadcastId }), () => {
|
||||
console.log(`\tPlayer ${player.username} has subscribed to game`);
|
||||
player.activate(broadcastId, pws);
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
ws.on('message', (data) => {
|
||||
const { answers = [], type = null } = JSON.parse(data);
|
||||
const connect = () => {
|
||||
const ws = new WebSocket(socketUrl, options);
|
||||
|
||||
// check if data type is a question not just like chat messages
|
||||
if (type === 'questionFinished') {
|
||||
console.log(`You have ${playersInGame.length} players still in game.`);
|
||||
}
|
||||
if (type === 'questionSummary') {
|
||||
const questionSummary = JSON.parse(data);
|
||||
const correctAnswer = questionSummary.answerCounts.find(answer => answer.correct).answerId;
|
||||
const wrongPlayers = [];
|
||||
ws.on('error', (err) => {
|
||||
console.log('An error occurred in the WebSocket: ', err);
|
||||
});
|
||||
|
||||
ws.on('pong', () => pong(TIMEOUT));
|
||||
|
||||
ws.on('open', () => {
|
||||
setInterval(() => ping(ws, connect), 10000);
|
||||
console.log('Master connected to websocket')
|
||||
|
||||
playersInGame.forEach((player) => {
|
||||
if (player.lastAnswerId !== correctAnswer) {
|
||||
//console.log(`Player ${player.username} has been eliminated.`);
|
||||
wrongPlayers.push(player.userId);
|
||||
}
|
||||
connectPlayer(player);
|
||||
});
|
||||
});
|
||||
|
||||
console.log(`${wrongPlayers.length} players have been eliminated.`);
|
||||
ws.on('message', (data) => {
|
||||
const { answers = [], type = null } = JSON.parse(data);
|
||||
|
||||
wrongPlayers.forEach(userId => removePlayer(userId));
|
||||
}
|
||||
if (type !== 'question') return;
|
||||
const { questionNumber, question: theQuestion, questionId, timeLeftMs } = JSON.parse(data);
|
||||
let timesUp = false;
|
||||
/*
|
||||
setTimeout(() => {
|
||||
timesUp = true;
|
||||
console.log('\nNo Answer: Splitting 1,2,3 to ensure automatically...');
|
||||
playersInGame.forEach((player, index) => {
|
||||
console.log(`Player ${player.username} sent answer ${(index % 3) + 1}`);
|
||||
player.sendAnswer(questionId, answers[index % 3].answerId)
|
||||
});
|
||||
}, timeLeftMs - 300);*/
|
||||
// check if data type is a question not just like chat messages
|
||||
if (type === 'questionFinished') {
|
||||
console.log(`You have ${playersInGame.length} players still in game.`);
|
||||
}
|
||||
if (type === 'questionSummary') {
|
||||
const questionSummary = JSON.parse(data);
|
||||
const correctAnswer = questionSummary.answerCounts.find(answer => answer.correct).answerId;
|
||||
const wrongPlayers = [];
|
||||
|
||||
console.log('\n==================================\n');
|
||||
console.log(`Question #${questionNumber} with ${Math.floor(timeLeftMs / 1000)} seconds left:`);
|
||||
console.log(`\n${theQuestion}`);
|
||||
console.log(`\nChoice 1: ${answers[0].text}`);
|
||||
console.log(`Choice 2: ${answers[1].text}`);
|
||||
console.log(`Choice 3: ${answers[2].text}\n`);
|
||||
playersInGame.forEach((player) => {
|
||||
if (player.lastAnswerId !== correctAnswer) {
|
||||
//console.log(`Player ${player.username} has been eliminated.`);
|
||||
wrongPlayers.push(player.userId);
|
||||
}
|
||||
});
|
||||
|
||||
if (playersInGame.length > 0) {
|
||||
prompt.start();
|
||||
prompt.get(['answer'], (err, res) => {
|
||||
if (!timesUp) {
|
||||
const inputAnswer = res.answer.trim();
|
||||
if (!inputAnswer) {
|
||||
playersInGame.forEach((player, index) => {
|
||||
console.log(`Player ${player.username} sent answer ${(index % 3) + 1}`);
|
||||
player.sendAnswer(questionId, answers[index % 3].answerId)
|
||||
});
|
||||
} else {
|
||||
const input = inputAnswer.split(',');
|
||||
switch (input.length) {
|
||||
case 1:
|
||||
playersInGame.forEach((player, index) => {
|
||||
console.log(`Player ${player.username} sent answer ${input[0]}`);
|
||||
player.sendAnswer(questionId, answers[input[0] - 1].answerId);
|
||||
});
|
||||
break;
|
||||
case 2:
|
||||
playersInGame.forEach((player, index) => {
|
||||
console.log(`Player ${player.username} sent answer ${input[index % 2]}`);
|
||||
player.sendAnswer(questionId, answers[input[index % 2] - 1].answerId)
|
||||
});
|
||||
break;
|
||||
case 3:
|
||||
playersInGame.forEach((player, index) => {
|
||||
console.log(`Player ${player.username} sent answer ${input[index % 3]}`);
|
||||
player.sendAnswer(questionId, answers[input[index % 3] - 1].answerId)
|
||||
});
|
||||
break;
|
||||
default:
|
||||
console.log('ERROR: you sent too many answer choices');
|
||||
break;
|
||||
console.log(`${wrongPlayers.length} players have been eliminated.`);
|
||||
|
||||
wrongPlayers.forEach(userId => removePlayer(userId));
|
||||
}
|
||||
if (type !== 'question') return;
|
||||
const { questionNumber, question: theQuestion, questionId, timeLeftMs } = JSON.parse(data);
|
||||
let timesUp = false;
|
||||
/*
|
||||
setTimeout(() => {
|
||||
timesUp = true;
|
||||
console.log('\nNo Answer: Splitting 1,2,3 to ensure automatically...');
|
||||
playersInGame.forEach((player, index) => {
|
||||
console.log(`Player ${player.username} sent answer ${(index % 3) + 1}`);
|
||||
player.sendAnswer(questionId, answers[index % 3].answerId)
|
||||
});
|
||||
}, timeLeftMs - 300);*/
|
||||
|
||||
console.log('\n==================================\n');
|
||||
console.log(`Question #${questionNumber} with ${Math.floor(timeLeftMs / 1000)} seconds left:`);
|
||||
console.log(`\n${theQuestion}`);
|
||||
console.log(`\nChoice 1: ${answers[0].text}`);
|
||||
console.log(`Choice 2: ${answers[1].text}`);
|
||||
console.log(`Choice 3: ${answers[2].text}\n`);
|
||||
|
||||
if (playersInGame.length > 0) {
|
||||
prompt.start();
|
||||
prompt.get(['answer'], (err, res) => {
|
||||
if (!timesUp) {
|
||||
const inputAnswer = res.answer.trim();
|
||||
if (!inputAnswer) {
|
||||
playersInGame.forEach((player, index) => {
|
||||
console.log(`Player ${player.username} sent answer ${(index % 3) + 1}`);
|
||||
player.sendAnswer(questionId, answers[index % 3].answerId)
|
||||
});
|
||||
} else {
|
||||
const input = inputAnswer.split(',');
|
||||
switch (input.length) {
|
||||
case 1:
|
||||
playersInGame.forEach((player, index) => {
|
||||
console.log(`Player ${player.username} sent answer ${input[0]}`);
|
||||
player.sendAnswer(questionId, answers[input[0] - 1].answerId);
|
||||
});
|
||||
break;
|
||||
case 2:
|
||||
playersInGame.forEach((player, index) => {
|
||||
console.log(`Player ${player.username} sent answer ${input[index % 2]}`);
|
||||
player.sendAnswer(questionId, answers[input[index % 2] - 1].answerId)
|
||||
});
|
||||
break;
|
||||
case 3:
|
||||
playersInGame.forEach((player, index) => {
|
||||
console.log(`Player ${player.username} sent answer ${input[index % 3]}`);
|
||||
player.sendAnswer(questionId, answers[input[index % 3] - 1].answerId)
|
||||
});
|
||||
break;
|
||||
default:
|
||||
console.log('ERROR: you sent too many answer choices');
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
connect();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user