Reconnection bugfixes and improvements (#30)
* Reconnect bugfix * one line * added to the readme
This commit is contained in:
parent
0b8ac304b3
commit
842873b4cd
2 changed files with 59 additions and 30 deletions
|
@ -18,11 +18,10 @@
|
||||||
* Respects Discords 15sec limit when it comes to updating your status
|
* Respects Discords 15sec limit when it comes to updating your status
|
||||||
* Stable or Insiders build detection
|
* Stable or Insiders build detection
|
||||||
* Debug mode detection
|
* Debug mode detection
|
||||||
|
* Easily manually reconnect to discord
|
||||||
|
|
||||||
## The rich presence won't show after my PC has been put to sleep / after I lost internet!
|
## The rich presence won't show after my PC has been put to sleep / after I lost internet!
|
||||||
It will only attempt to reconnect 20 times. After it hit that threshold you will have to manually enable it again.
|
It will only attempt to reconnect 20 times. After it hit that threshold you will have to manually click the `Reconnect to Discord` button in the bottom left of the window
|
||||||
Just open the command pallette and execute the enable command for the extension / or reload the window.
|
|
||||||
You can also set the reconnectThreshold in the settings to something very high, for example 9999 or Infinity to never stop trying to reconnect.
|
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
|
|
|
@ -25,8 +25,8 @@ let rpc: Client;
|
||||||
const eventHandlers: Set<Disposable> = new Set();
|
const eventHandlers: Set<Disposable> = new Set();
|
||||||
// Define the config variable and its type.
|
// Define the config variable and its type.
|
||||||
let config;
|
let config;
|
||||||
// Define the reconnect timer and its type.
|
// Define the reconnecting var and its type.
|
||||||
let reconnectTimer: NodeJS.Timer;
|
let reconnecting: boolean;
|
||||||
// Define the reconnect counter and its type.
|
// Define the reconnect counter and its type.
|
||||||
let reconnectCounter = 0;
|
let reconnectCounter = 0;
|
||||||
// Define the last known file name and its type.
|
// Define the last known file name and its type.
|
||||||
|
@ -66,7 +66,8 @@ export function activate(context: ExtensionContext) {
|
||||||
const reconnecter = commands.registerCommand('discord.reconnect', async () => {
|
const reconnecter = commands.registerCommand('discord.reconnect', async () => {
|
||||||
if (rpc) try { await destroyRPC(); } catch {}
|
if (rpc) try { await destroyRPC(); } catch {}
|
||||||
initRPC(config.get('clientID'), true);
|
initRPC(config.get('clientID'), true);
|
||||||
window.showInformationMessage('Reconnecting to Discord RPC');
|
|
||||||
|
if (!config.get('silent')) window.showInformationMessage('Reconnecting to Discord RPC');
|
||||||
|
|
||||||
if (statusBarIcon) statusBarIcon.text = '$(pulse) Reconnecting';
|
if (statusBarIcon) statusBarIcon.text = '$(pulse) Reconnecting';
|
||||||
});
|
});
|
||||||
|
@ -88,18 +89,17 @@ function initRPC(clientID: string, loud?: boolean): void {
|
||||||
|
|
||||||
// Once the RPC Client is ready, set the activity.
|
// Once the RPC Client is ready, set the activity.
|
||||||
rpc.once('ready', () => {
|
rpc.once('ready', () => {
|
||||||
if (loud) window.showInformationMessage('Successfully reconnected to Discord RPC');
|
// Announce the reconnection
|
||||||
|
if (loud && !config.get('silent')) window.showInformationMessage('Successfully reconnected to Discord RPC');
|
||||||
|
|
||||||
// Remove icon if connected
|
// Remove icon if connected
|
||||||
if (statusBarIcon) statusBarIcon.dispose();
|
if (statusBarIcon) {
|
||||||
|
statusBarIcon.dispose();
|
||||||
// This is purely for safety measures.
|
statusBarIcon = null;
|
||||||
if (reconnectTimer) {
|
|
||||||
// Clear the reconnect interval.
|
|
||||||
clearInterval(reconnectTimer);
|
|
||||||
// Null reconnect variable.
|
|
||||||
reconnectTimer = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Stop from reconnecing.
|
||||||
|
reconnecting = false;
|
||||||
// This is purely for safety measures.
|
// This is purely for safety measures.
|
||||||
if (activityTimer) {
|
if (activityTimer) {
|
||||||
// Clear the activity interval.
|
// Clear the activity interval.
|
||||||
|
@ -117,11 +117,12 @@ function initRPC(clientID: string, loud?: boolean): void {
|
||||||
rpc.transport.once('close', async () => {
|
rpc.transport.once('close', async () => {
|
||||||
if (!config.get('enabled')) return;
|
if (!config.get('enabled')) return;
|
||||||
await destroyRPC();
|
await destroyRPC();
|
||||||
// Set an interval for reconnecting.
|
|
||||||
reconnectTimer = setInterval(() => {
|
// Set the client to begin reconnecting
|
||||||
reconnectCounter++;
|
reconnecting = true;
|
||||||
initRPC(config.get('clientID'));
|
initRPC(config.get('clientID'));
|
||||||
}, 5000);
|
// Create reconnecting button
|
||||||
|
createButon(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Update the user's activity to the `activity` variable.
|
// Update the user's activity to the `activity` variable.
|
||||||
|
@ -133,15 +134,24 @@ function initRPC(clientID: string, loud?: boolean): void {
|
||||||
|
|
||||||
// Log in to the RPC Client, and check whether or not it errors.
|
// Log in to the RPC Client, and check whether or not it errors.
|
||||||
rpc.login(clientID).catch(async error => {
|
rpc.login(clientID).catch(async error => {
|
||||||
if (reconnectTimer) {
|
// Check if the client is reconnecting
|
||||||
// Destroy and dispose of everything after a default of 20 reconnect attempts
|
if (reconnecting) {
|
||||||
|
// Destroy and dispose of everything after the set reconnect attempts
|
||||||
if (reconnectCounter >= config.get('reconnectThreshold')) {
|
if (reconnectCounter >= config.get('reconnectThreshold')) {
|
||||||
|
// Create reconnect button
|
||||||
createButon();
|
createButon();
|
||||||
await destroyRPC();
|
await destroyRPC();
|
||||||
} else {
|
} else {
|
||||||
|
// Increment the counter
|
||||||
|
reconnectCounter++;
|
||||||
|
// Create reconnecting button
|
||||||
|
createButon(true);
|
||||||
|
// Retry connection
|
||||||
|
initRPC(config.get('clientID'));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Announce failure
|
||||||
if (!config.get('silent')) {
|
if (!config.get('silent')) {
|
||||||
if (error.message.includes('ENOENT')) window.showErrorMessage('No Discord Client detected!');
|
if (error.message.includes('ENOENT')) window.showErrorMessage('No Discord Client detected!');
|
||||||
else window.showErrorMessage(`Couldn't connect to Discord via RPC: ${error.message}`);
|
else window.showErrorMessage(`Couldn't connect to Discord via RPC: ${error.message}`);
|
||||||
|
@ -151,14 +161,36 @@ function initRPC(clientID: string, loud?: boolean): void {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create reconnect button
|
// Create reconnect button
|
||||||
function createButon(): void {
|
function createButon(isReconnecting?: boolean): void {
|
||||||
|
// Check if the button already
|
||||||
if (!statusBarIcon) {
|
if (!statusBarIcon) {
|
||||||
|
// Create the icon
|
||||||
statusBarIcon = window.createStatusBarItem(StatusBarAlignment.Left);
|
statusBarIcon = window.createStatusBarItem(StatusBarAlignment.Left);
|
||||||
statusBarIcon.text = '$(plug) Reconnect to Discord';
|
// Check if the client is reconnecting
|
||||||
statusBarIcon.command = 'discord.reconnect';
|
if (isReconnecting) {
|
||||||
|
// Show attempts left
|
||||||
|
const attempts = config.get('reconnectThreshold') - reconnectCounter;
|
||||||
|
statusBarIcon.text = `$(issue-reopened) Reconnecting: ${attempts} attempt${attempts === 1 ? '' : 's'} left`;
|
||||||
|
statusBarIcon.command = '';
|
||||||
|
} else {
|
||||||
|
// Show button to reconnect
|
||||||
|
statusBarIcon.text = '$(plug) Reconnect to Discord';
|
||||||
|
statusBarIcon.command = 'discord.reconnect';
|
||||||
|
}
|
||||||
|
// Show the button
|
||||||
statusBarIcon.show();
|
statusBarIcon.show();
|
||||||
} else {
|
} else {
|
||||||
statusBarIcon.text = '$(plug) Reconnect to Discord';
|
// Check if the client is reconnecting
|
||||||
|
if (isReconnecting) {
|
||||||
|
// Show attempts left
|
||||||
|
const attempts = config.get('reconnectThreshold') - reconnectCounter;
|
||||||
|
statusBarIcon.text = `$(issue-reopened) Reconnecting: ${attempts} attempt${attempts === 1 ? '' : 's'} left`;
|
||||||
|
statusBarIcon.command = '';
|
||||||
|
} else {
|
||||||
|
// Show button to reconnect
|
||||||
|
statusBarIcon.text = '$(plug) Reconnect to Discord';
|
||||||
|
statusBarIcon.command = 'discord.reconnect';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -166,10 +198,8 @@ function createButon(): void {
|
||||||
async function destroyRPC(): Promise<void> {
|
async function destroyRPC(): Promise<void> {
|
||||||
// Do not continue if RPC isn't initalized.
|
// Do not continue if RPC isn't initalized.
|
||||||
if (!rpc) return;
|
if (!rpc) return;
|
||||||
// Clear the reconnect interval.
|
// Stop reconnecting.
|
||||||
if (reconnectTimer) clearInterval(reconnectTimer);
|
reconnecting = false;
|
||||||
// Null reconnect variable.
|
|
||||||
reconnectTimer = null;
|
|
||||||
// Clear the activity interval.
|
// Clear the activity interval.
|
||||||
if (activityTimer) clearInterval(activityTimer);
|
if (activityTimer) clearInterval(activityTimer);
|
||||||
// Null the activity timer.
|
// Null the activity timer.
|
||||||
|
|
Loading…
Reference in a new issue