funky reconnect when losing internet
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
// Event triggered by client connected to server
|
||||
|
||||
const { tClient, mClient } = require("../..");
|
||||
const { mClient, dClient } = require("../..");
|
||||
var { tClient } = require('../..')
|
||||
const { refreshAccessToken } = require("../../functions")
|
||||
const tmi = require('tmi.js')
|
||||
|
||||
@@ -13,13 +14,6 @@ module.exports = {
|
||||
try {
|
||||
await refreshAccessToken();
|
||||
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 credentialCollection = db.collection('credentials');
|
||||
const tCreds = await credentialCollection.findOne({ service: 'twitch' });
|
||||
@@ -34,8 +28,8 @@ module.exports = {
|
||||
},
|
||||
channels: tClient.channels,
|
||||
});
|
||||
|
||||
await tClient.connect();
|
||||
const { address, port } = await tClient.connect()
|
||||
dClient.emit("Twitch/Connected", address, port)
|
||||
console.log('Twitch client reconnected successfully.');
|
||||
} catch (error) {
|
||||
console.error('Failed to refresh token and reconnect:', error);
|
||||
|
||||
@@ -12,6 +12,7 @@ module.exports = {
|
||||
name: 'Twitch/EventSub',
|
||||
once: false,
|
||||
async execute() {
|
||||
console.log('Starting Event Subs...')
|
||||
const endpoint = `wss://eventsub.wss.twitch.tv/ws`
|
||||
|
||||
const db = mClient.db('clients')
|
||||
@@ -38,6 +39,9 @@ module.exports = {
|
||||
}
|
||||
function onDisconnect(reason) {
|
||||
console.log(`Disconnecting due to reason`, reason)
|
||||
if(connection.readyState === connection.OPEN){
|
||||
connection.close(4003, 'manual disconnect')
|
||||
}
|
||||
}
|
||||
function connect() {
|
||||
connection = new ReconnectingWebSocket(endpoint, [], {
|
||||
@@ -88,6 +92,7 @@ module.exports = {
|
||||
subscriptionPayload, {
|
||||
headers: headers
|
||||
})
|
||||
console.log(res)
|
||||
console.log(`Subscribed to ${type}`)
|
||||
subscribedEvents[type] = listener
|
||||
return true
|
||||
|
||||
Reference in New Issue
Block a user