Reverted SocketIO to ws.
This commit is contained in:
parent
b3cab90810
commit
cb5de62e1c
37
src/schat.js
37
src/schat.js
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue