funky reconnect when losing internet

This commit is contained in:
DeSqBlocki
2025-02-10 17:27:53 +01:00
parent 96af757961
commit 0ca35deb6e
2 changed files with 9 additions and 10 deletions
+4 -10
View File
@@ -1,6 +1,7 @@
// Event triggered by client connected to server // Event triggered by client connected to server
const { tClient, mClient } = require("../.."); const { mClient, dClient } = require("../..");
var { tClient } = require('../..')
const { refreshAccessToken } = require("../../functions") const { refreshAccessToken } = require("../../functions")
const tmi = require('tmi.js') const tmi = require('tmi.js')
@@ -13,13 +14,6 @@ module.exports = {
try { try {
await refreshAccessToken(); await refreshAccessToken();
console.log('Reinitializing Twitch client after token refresh...'); console.log('Reinitializing Twitch client after token refresh...');
try {
await tClient.disconnect();
} catch (err) {
console.warn('Twitch client already disconnected:', err.message);
}
const db = mClient.db("clients"); const db = mClient.db("clients");
const credentialCollection = db.collection('credentials'); const credentialCollection = db.collection('credentials');
const tCreds = await credentialCollection.findOne({ service: 'twitch' }); const tCreds = await credentialCollection.findOne({ service: 'twitch' });
@@ -34,8 +28,8 @@ module.exports = {
}, },
channels: tClient.channels, channels: tClient.channels,
}); });
const { address, port } = await tClient.connect()
await tClient.connect(); dClient.emit("Twitch/Connected", address, port)
console.log('Twitch client reconnected successfully.'); console.log('Twitch client reconnected successfully.');
} catch (error) { } catch (error) {
console.error('Failed to refresh token and reconnect:', error); console.error('Failed to refresh token and reconnect:', error);
+5
View File
@@ -12,6 +12,7 @@ module.exports = {
name: 'Twitch/EventSub', name: 'Twitch/EventSub',
once: false, once: false,
async execute() { async execute() {
console.log('Starting Event Subs...')
const endpoint = `wss://eventsub.wss.twitch.tv/ws` const endpoint = `wss://eventsub.wss.twitch.tv/ws`
const db = mClient.db('clients') const db = mClient.db('clients')
@@ -38,6 +39,9 @@ module.exports = {
} }
function onDisconnect(reason) { function onDisconnect(reason) {
console.log(`Disconnecting due to reason`, reason) console.log(`Disconnecting due to reason`, reason)
if(connection.readyState === connection.OPEN){
connection.close(4003, 'manual disconnect')
}
} }
function connect() { function connect() {
connection = new ReconnectingWebSocket(endpoint, [], { connection = new ReconnectingWebSocket(endpoint, [], {
@@ -88,6 +92,7 @@ module.exports = {
subscriptionPayload, { subscriptionPayload, {
headers: headers headers: headers
}) })
console.log(res)
console.log(`Subscribed to ${type}`) console.log(`Subscribed to ${type}`)
subscribedEvents[type] = listener subscribedEvents[type] = listener
return true return true