mirror of
https://github.com/Mastermindzh/tidal-hifi.git
synced 2024-11-22 13:32:42 +01:00
Added RPC connection retrying
This commit is contained in:
parent
f96cf2e8da
commit
6be5774001
@ -13,6 +13,8 @@ const clientId = "833617820704440341";
|
|||||||
export let rpc: Client;
|
export let rpc: Client;
|
||||||
|
|
||||||
const ACTIVITY_LISTENING = 2;
|
const ACTIVITY_LISTENING = 2;
|
||||||
|
const MAX_RETRIES = 5;
|
||||||
|
const RETRY_DELAY = 10000;
|
||||||
|
|
||||||
const observer = () => {
|
const observer = () => {
|
||||||
if (rpc) {
|
if (rpc) {
|
||||||
@ -77,26 +79,32 @@ const getActivity = (): SetActivity => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Try to login to RPC and retry if it errors
|
||||||
|
* @param retryCount Max retry count
|
||||||
|
*/
|
||||||
|
const connectWithRetry = async (retryCount = 0) => {
|
||||||
|
try {
|
||||||
|
await rpc.login();
|
||||||
|
Logger.log('Connected to Discord');
|
||||||
|
rpc.on("ready", updateActivity);
|
||||||
|
Object.values(globalEvents).forEach(event => ipcMain.on(event, observer));
|
||||||
|
} catch (error) {
|
||||||
|
if (retryCount < MAX_RETRIES) {
|
||||||
|
Logger.log(`Failed to connect to Discord, retrying in ${RETRY_DELAY/1000} seconds... (Attempt ${retryCount + 1}/${MAX_RETRIES})`);
|
||||||
|
setTimeout(() => connectWithRetry(retryCount + 1), RETRY_DELAY);
|
||||||
|
} else {
|
||||||
|
Logger.log('Failed to connect to Discord after maximum retry attempts');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set up the discord rpc and listen on globalEvents.updateInfo
|
* Set up the discord rpc and listen on globalEvents.updateInfo
|
||||||
*/
|
*/
|
||||||
export const initRPC = () => {
|
export const initRPC = () => {
|
||||||
rpc = new Client({ transport: {type: "ipc"}, clientId });
|
rpc = new Client({ transport: {type: "ipc"}, clientId });
|
||||||
rpc.login().then(
|
connectWithRetry();
|
||||||
() => {
|
|
||||||
rpc.on("ready", () => {
|
|
||||||
updateActivity();
|
|
||||||
});
|
|
||||||
|
|
||||||
const { updateInfo, play, pause, playPause } = globalEvents;
|
|
||||||
[updateInfo, play, pause, playPause].forEach((status) => {
|
|
||||||
ipcMain.on(status, observer);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
() => {
|
|
||||||
Logger.log("Can't connect to Discord, is it running?");
|
|
||||||
}
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user