Added socket reconnection.

This commit is contained in:
2021-11-04 01:51:43 +01:00
parent aed92bfefa
commit c3585ad08b
3 changed files with 155 additions and 123 deletions
+68 -40
View File
@@ -1,6 +1,7 @@
'use strict';
const config = require('config');
const { setTimeout: delay } = require('timers/promises');
const bhttp = require('bhttp');
const WebSocket = require('ws');
const fs = require('fs').promises;
@@ -11,7 +12,10 @@ const points = {};
async function auth() {
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,
});
@@ -38,18 +42,6 @@ async function getWsId(httpSession) {
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') {
const userKey = `${user.id}:${user.username}`;
@@ -96,7 +88,7 @@ function getLeaderboard(game, { user, room }) {
}
function onConnect(data, bot) {
bot.transmit('joinRooms', { rooms: config.channels });
bot.socket.transmit('joinRooms', { rooms: config.channels });
}
function onRooms({ rooms, users }, bot) {
@@ -108,7 +100,7 @@ function onRooms({ rooms, users }, bot) {
/* eslint-enable no-param-reassign */
rooms.forEach((room) => {
bot.transmit('message', {
bot.socket.transmit('message', {
roomId: room.id,
body: `Hi, I am ${config.user.username}, your game host!`,
style: config.style,
@@ -184,29 +176,12 @@ async function initPoints() {
}
}
async function init() {
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,
};
function getGames(bot) {
const games = config.games.reduce((acc, key) => {
const game = require(`./games/${key}`); // eslint-disable-line global-require, import/no-dynamic-require
const sendMessage = (body, roomId) => {
bot.transmit('message', {
bot.socket.transmit('message', {
roomId,
body: `[${game.name || key}] ${body}`,
style: config.style,
@@ -227,13 +202,66 @@ async function init() {
};
}, {});
ws.on('message', (msg) => {
const [domain, data] = JSON.parse(msg);
return games;
}
if (messageHandlers[domain]) {
messageHandlers[domain](data, bot, 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);
if (messageHandlers[domain]) {
messageHandlers[domain](data, bot, games);
}
});
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();
}