[enhancement]: support viewing current/setting current time in remote

This commit is contained in:
Kendall Garner 2024-08-24 13:26:45 -07:00
parent b347b794b9
commit 5b2977e5e8
No known key found for this signature in database
GPG key ID: 18D2767419676C87
9 changed files with 61 additions and 13 deletions

View file

@ -34,13 +34,14 @@ interface MimeType {
js: string;
}
interface StatefulWebSocket extends WebSocket {
declare class StatefulWebSocket extends WebSocket {
alive: boolean;
auth: boolean;
}
let server: Server | undefined;
let wsServer: WsServer<StatefulWebSocket> | undefined;
let wsServer: WsServer<typeof StatefulWebSocket> | undefined;
const settings: RemoteConfig = {
enabled: false,
@ -327,9 +328,9 @@ const enableServer = (config: RemoteConfig): Promise<void> => {
});
server.listen(config.port, resolve);
wsServer = new WebSocketServer({ server });
wsServer = new WebSocketServer<typeof StatefulWebSocket>({ server });
wsServer.on('connection', (ws) => {
wsServer!.on('connection', (ws: StatefulWebSocket) => {
let authFail: number | undefined;
ws.alive = true;
@ -471,6 +472,15 @@ const enableServer = (config: RemoteConfig): Promise<void> => {
}
break;
}
case 'position': {
const { position } = json;
if (mprisPlayer) {
mprisPlayer.getPosition = () => position * 1e6;
}
getMainWindow()?.webContents.send('request-position', {
position,
});
}
}
} catch (error) {
console.error(error);
@ -496,7 +506,7 @@ const enableServer = (config: RemoteConfig): Promise<void> => {
});
}, PING_TIMEOUT_MS);
wsServer.on('close', () => {
wsServer!.on('close', () => {
clearInterval(heartBeat);
});
@ -649,3 +659,8 @@ if (mprisPlayer) {
broadcast({ data: volume, event: 'volume' });
});
}
ipcMain.on('update-position', (_event, position: number) => {
currentState.position = position;
broadcast({ data: position, event: 'position' });
});