Added socket reconnection.

This commit is contained in:
ThePendulum 2021-11-04 01:51:43 +01:00
parent aed92bfefa
commit c3585ad08b
3 changed files with 155 additions and 123 deletions

View File

@ -10,12 +10,14 @@ module.exports = {
birthdate: new Date(1952, 11, 10), birthdate: new Date(1952, 11, 10),
avatar: 'https://i.imgur.com/IZwrjjG.png', avatar: 'https://i.imgur.com/IZwrjjG.png',
}, },
uniqueUsername: true,
socket: 'ws://127.0.0.1:3000/socket', socket: 'ws://127.0.0.1:3000/socket',
api: 'http://127.0.0.1:3000/api', api: 'http://127.0.0.1:3000/api',
prefix: '~', prefix: '~',
style: { style: {
color: 'var(--message-56)', color: 'var(--message-56)',
}, },
games: ['mash'], games: ['mash', 'cursed'],
channels: ['GamesNight'], channels: ['GamesNight'],
reconnectDelay: 10, // seconds
}; };

View File

@ -1,6 +1,7 @@
'use strict'; 'use strict';
const config = require('config'); const config = require('config');
const { setTimeout: delay } = require('timers/promises');
const bhttp = require('bhttp'); const bhttp = require('bhttp');
const WebSocket = require('ws'); const WebSocket = require('ws');
const fs = require('fs').promises; const fs = require('fs').promises;
@ -11,7 +12,10 @@ const points = {};
async function auth() { async function auth() {
const httpSession = bhttp.session(); const httpSession = bhttp.session();
const res = await httpSession.post(`${config.api}/session`, config.user, { const res = await httpSession.post(`${config.api}/session`, {
...config.user,
username: config.uniqueUsername ? `${config.user.username}-${new Date().getTime().toString().slice(-5)}` : config.user.username,
}, {
encodeJSON: true, encodeJSON: true,
}); });
@ -38,18 +42,6 @@ async function getWsId(httpSession) {
return res.body; return res.body;
} }
function connect(wsCreds, sessionCookie) {
const ws = new WebSocket(`${config.socket}?${new URLSearchParams({ v: wsCreds.wsId, t: wsCreds.timestamp }).toString()}`, [], {
headers: {
cookie: sessionCookie,
},
});
logger.info(`Connected to ${config.socket}`);
return ws;
}
async function setPoints(gameKey, user, value, mode = 'add') { async function setPoints(gameKey, user, value, mode = 'add') {
const userKey = `${user.id}:${user.username}`; const userKey = `${user.id}:${user.username}`;
@ -96,7 +88,7 @@ function getLeaderboard(game, { user, room }) {
} }
function onConnect(data, bot) { function onConnect(data, bot) {
bot.transmit('joinRooms', { rooms: config.channels }); bot.socket.transmit('joinRooms', { rooms: config.channels });
} }
function onRooms({ rooms, users }, bot) { function onRooms({ rooms, users }, bot) {
@ -108,7 +100,7 @@ function onRooms({ rooms, users }, bot) {
/* eslint-enable no-param-reassign */ /* eslint-enable no-param-reassign */
rooms.forEach((room) => { rooms.forEach((room) => {
bot.transmit('message', { bot.socket.transmit('message', {
roomId: room.id, roomId: room.id,
body: `Hi, I am ${config.user.username}, your game host!`, body: `Hi, I am ${config.user.username}, your game host!`,
style: config.style, style: config.style,
@ -184,29 +176,12 @@ async function initPoints() {
} }
} }
async function init() { function getGames(bot) {
const { user, httpSession, sessionCookie } = await auth();
const wsCreds = await getWsId(httpSession);
const ws = connect(wsCreds, sessionCookie);
ws.transmit = (domain, data) => {
ws.send(JSON.stringify([domain, data]));
};
const bot = {
user,
ws,
httpSession,
rooms: [],
users: [],
transmit: ws.transmit,
};
const games = config.games.reduce((acc, key) => { const games = config.games.reduce((acc, key) => {
const game = require(`./games/${key}`); // eslint-disable-line global-require, import/no-dynamic-require const game = require(`./games/${key}`); // eslint-disable-line global-require, import/no-dynamic-require
const sendMessage = (body, roomId) => { const sendMessage = (body, roomId) => {
bot.transmit('message', { bot.socket.transmit('message', {
roomId, roomId,
body: `[${game.name || key}] ${body}`, body: `[${game.name || key}] ${body}`,
style: config.style, style: config.style,
@ -227,7 +202,22 @@ async function init() {
}; };
}, {}); }, {});
ws.on('message', (msg) => { return games;
}
async function connect(wsCreds, sessionCookie, bot, games) {
const socket = { ws: { readyState: 0 } };
socket.connect = () => {
logger.info(`Attempting to connect to ${config.socket}`);
socket.ws = new WebSocket(`${config.socket}?${new URLSearchParams({ v: wsCreds.wsId, t: wsCreds.timestamp }).toString()}`, [], {
headers: {
cookie: sessionCookie,
},
});
socket.ws.on('message', (msg) => {
const [domain, data] = JSON.parse(msg); const [domain, data] = JSON.parse(msg);
if (messageHandlers[domain]) { if (messageHandlers[domain]) {
@ -235,6 +225,44 @@ async function init() {
} }
}); });
socket.ws.on('close', async (info) => {
logger.error(`WebSocket closed, reconnecting in ${config.reconnectDelay} seconds: ${info}`);
await delay(config.reconnectDelay * 1000);
socket.connect();
});
socket.ws.on('error', async (error) => {
logger.error(`WebSocket error: ${error.message}`);
});
logger.info(`Connected to ${config.socket}`);
};
socket.transmit = (domain, data) => {
socket.ws.send(JSON.stringify([domain, data]));
};
socket.connect();
return socket;
}
async function init() {
const { user, httpSession, sessionCookie } = await auth();
const wsCreds = await getWsId(httpSession);
const bot = {
user,
httpSession,
rooms: [],
users: [],
};
const games = getGames(bot);
bot.socket = await connect(wsCreds, sessionCookie, bot, games);
await initPoints(); await initPoints();
} }

View File

@ -1,7 +1,9 @@
'use strict'; 'use strict';
const config = require('config'); const config = require('config');
const words = require('../../assets/mash-words.json'); const words = require('../../assets/mash-words.json');
let mash = null; let mash = null;
function start(length, context, attempt = 0) { function start(length, context, attempt = 0) {
@ -23,7 +25,7 @@ function start(length, context, attempt = 0) {
const wordEntries = Object.entries(lengthWords); const wordEntries = Object.entries(lengthWords);
const [key, answers] = wordEntries[Math.floor(Math.random() * wordEntries.length)]; const [key, answers] = wordEntries[Math.floor(Math.random() * wordEntries.length)];
const anagram = key.split('').sort(() => Math.random() > .5 ? 1 : -1).join(''); const anagram = key.split('').sort(() => (Math.random() > 0.5 ? 1 : -1)).join('');
if (answers.includes(anagram)) { if (answers.includes(anagram)) {
if (attempt >= 10) { if (attempt >= 10) {
@ -63,7 +65,7 @@ function play(word, context) {
if (mash.answers.includes(word)) { if (mash.answers.includes(word)) {
context.sendMessage(mash.answers.length === 1 context.sendMessage(mash.answers.length === 1
? `**${word}** is the right answer, @${context.user.username} now has **${context.user.points + 1} ${context.user.points === 0 ? 'point' : 'points'}**! There were no other options for **${mash.anagram}**.` ? `**${word}** is the right answer, @${context.user.username} now has **${context.user.points + 1} ${context.user.points === 0 ? 'point' : 'points'}**! There were no other options for **${mash.anagram}**.`
: `**${word}** is the right answer, @${context.user.username} now has **${context.user.points + 1} ${context.user.points === 0 ? 'point' : 'points'}**! Other options for **${mash.anagram}**: ${mash.answers.filter((answer) => answer !== word).map((word) => `**${word}**`).join(', ')}`, context.room.id); : `**${word}** is the right answer, @${context.user.username} now has **${context.user.points + 1} ${context.user.points === 0 ? 'point' : 'points'}**! Other options for **${mash.anagram}**: ${mash.answers.filter((answer) => answer !== word).map((answer) => `**${answer}**`).join(', ')}`, context.room.id);
context.logger.info(`Mash '${mash.anagram}' guessed by '${context.user.username}' with '${word}'`); context.logger.info(`Mash '${mash.anagram}' guessed by '${context.user.username}' with '${word}'`);
context.setPoints(context.user, 1); context.setPoints(context.user, 1);