Retrying on connection failure.
This commit is contained in:
65
src/app.js
65
src/app.js
@@ -269,46 +269,53 @@ async function connect(bot, games) {
|
||||
const socket = { ws: { readyState: 0 } };
|
||||
|
||||
socket.connect = async () => {
|
||||
const { user, httpSession, sessionCookie } = await auth();
|
||||
const wsCreds = await getWsId(httpSession);
|
||||
try {
|
||||
const { user, httpSession, sessionCookie } = await auth();
|
||||
const wsCreds = await getWsId(httpSession);
|
||||
|
||||
bot.user = user;
|
||||
bot.httpSession = httpSession;
|
||||
bot.user = user;
|
||||
bot.httpSession = httpSession;
|
||||
|
||||
logger.info(`Attempting to connect to ${config.socket}`);
|
||||
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 = new WebSocket(`${config.socket}?${new URLSearchParams({ v: wsCreds.wsId, t: wsCreds.timestamp }).toString()}`, [], {
|
||||
headers: {
|
||||
cookie: sessionCookie,
|
||||
},
|
||||
});
|
||||
|
||||
socket.ws.on('message', async (msg) => {
|
||||
const [domain, data] = JSON.parse(msg);
|
||||
socket.ws.on('message', async (msg) => {
|
||||
const [domain, data] = JSON.parse(msg);
|
||||
|
||||
logger.debug(`Received ${domain}: ${JSON.stringify(data)}`);
|
||||
logger.debug(`Received ${domain}: ${JSON.stringify(data)}`);
|
||||
|
||||
if (messageHandlers[domain]) {
|
||||
try {
|
||||
await messageHandlers[domain](data, bot, games);
|
||||
} catch (error) {
|
||||
handleError(error, socket, domain, data);
|
||||
if (messageHandlers[domain]) {
|
||||
try {
|
||||
await messageHandlers[domain](data, bot, games);
|
||||
} catch (error) {
|
||||
handleError(error, socket, domain, data);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
socket.ws.on('close', async (info) => {
|
||||
logger.error(`WebSocket closed, reconnecting in ${config.reconnectDelay} seconds: ${info}`);
|
||||
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}`);
|
||||
} catch (error) {
|
||||
logger.error(`Failed to connect, retrying in ${config.reconnectDelay} seconds: ${error.message}`);
|
||||
|
||||
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) => {
|
||||
|
||||
@@ -49,8 +49,8 @@ function onCommand(args, context) {
|
||||
|
||||
const messages = [
|
||||
`How could you miss *that*, @${context.user.username}...?!`,
|
||||
`Better luck next time, @${context.user.username}.`,
|
||||
`The duck got away, @${context.user.username}`,
|
||||
`That's a miss! Better luck next time, @${context.user.username}.`,
|
||||
`The duck outsmarted you, @${context.user.username}`,
|
||||
`Channeling Gareth Southgate, @${context.user.username}? You missed!`,
|
||||
];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user