Reverted SocketIO to ws.

This commit is contained in:
ThePendulum 2024-01-13 01:29:10 +01:00
parent b3cab90810
commit cb5de62e1c
1 changed files with 12 additions and 25 deletions

View File

@ -3,8 +3,7 @@
const config = require('config'); const config = require('config');
const { setTimeout: delay } = require('timers/promises'); const { setTimeout: delay } = require('timers/promises');
const bhttp = require('bhttp'); const bhttp = require('bhttp');
// const WebSocket = require('ws'); const WebSocket = require('ws');
const io = require('socket.io-client');
const logger = require('simple-node-logger').createSimpleLogger(); const logger = require('simple-node-logger').createSimpleLogger();
const { argv } = require('yargs'); const { argv } = require('yargs');
@ -144,29 +143,13 @@ async function connect(bot, games) {
logger.info(`Attempting to connect to ${config.socket}`); logger.info(`Attempting to connect to ${config.socket}`);
const { origin, pathname } = new URL(config.socket); socket.ws = new WebSocket(`${config.socket}?${new URLSearchParams({ v: wsCreds.wsId, t: wsCreds.timestamp }).toString()}`, [], {
headers: {
socket.io = io(origin, {
transports: ['websocket'],
path: pathname,
query: {
v: wsCreds.wsId,
t: wsCreds.timestamp,
},
extraHeaders: {
cookie: sessionCookie, cookie: sessionCookie,
}, },
}); });
socket.io.on('connect', () => { socket.ws.on('message', async (msg) => {
logger.info(`Connected to ${config.socket}`);
});
socket.io.on('connect_error', (error) => {
logger.info(`Failed to connect to ${config.socket}: ${error}`);
});
socket.io.on('_', async (msg) => {
const [domain, data] = JSON.parse(msg); const [domain, data] = JSON.parse(msg);
logger.debug(`Received ${domain}: ${JSON.stringify(data)}`); logger.debug(`Received ${domain}: ${JSON.stringify(data)}`);
@ -180,14 +163,18 @@ async function connect(bot, games) {
} }
}); });
/* reconnect logic handled by SocketIO socket.ws.on('close', async (info) => {
socket.io.on('disconnect', async (info) => {
logger.error(`WebSocket closed, reconnecting in ${config.reconnectDelay} seconds: ${info}`); logger.error(`WebSocket closed, reconnecting in ${config.reconnectDelay} seconds: ${info}`);
await delay(config.reconnectDelay * 1000); await delay(config.reconnectDelay * 1000);
socket.connect(); socket.connect();
}); });
*/
socket.ws.on('error', async (error) => {
logger.error(`WebSocket error: ${error.message}`);
});
logger.info(`Connected to ${config.socket}`);
} catch (error) { } catch (error) {
logger.error(`Failed to connect, retrying in ${config.reconnectDelay} seconds: ${error.message}`); logger.error(`Failed to connect, retrying in ${config.reconnectDelay} seconds: ${error.message}`);
@ -197,7 +184,7 @@ async function connect(bot, games) {
}; };
socket.transmit = (domain, data) => { socket.transmit = (domain, data) => {
socket.io.emit('_', JSON.stringify([domain, data])); socket.ws.send(JSON.stringify([domain, data]));
}; };
socket.connect(); socket.connect();