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