mirror of
https://gitlab.com/futo-org/fcast.git
synced 2025-06-24 21:25:23 +00:00
Receivers: Store connection state in main process
This commit is contained in:
parent
75f2f3dded
commit
5782fcc3ca
8 changed files with 32 additions and 16 deletions
|
@ -1,27 +1,27 @@
|
|||
import { Opcode } from 'common/Packets';
|
||||
|
||||
const connectionPingTimeout = 2500;
|
||||
const connections = [];
|
||||
const heartbeatRetries = {};
|
||||
let connections = [];
|
||||
let uiUpdateCallbacks = {
|
||||
onConnect: null,
|
||||
onDisconnect: null,
|
||||
}
|
||||
|
||||
// Window might be re-created while devices are still connected
|
||||
export function setUiUpdateCallbacks(callbacks: any) {
|
||||
uiUpdateCallbacks = callbacks;
|
||||
|
||||
window.targetAPI.getSessions().then((sessions: string[]) => {
|
||||
connections = sessions;
|
||||
if (connections.length > 0) {
|
||||
uiUpdateCallbacks.onConnect(connections, true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Window might be re-created while devices are still connected
|
||||
function onPingPong(value: any) {
|
||||
if (value) {
|
||||
heartbeatRetries[value.sessionId] = 0;
|
||||
|
||||
if (!connections.includes(value.sessionId)) {
|
||||
connections.push(value.sessionId);
|
||||
uiUpdateCallbacks.onConnect(connections, value.sessionId);
|
||||
}
|
||||
}
|
||||
heartbeatRetries[value.sessionId] = 0;
|
||||
}
|
||||
window.targetAPI.onPing((_event, value: any) => onPingPong(value));
|
||||
window.targetAPI.onPong((_event, value: any) => onPingPong(value));
|
||||
|
@ -29,7 +29,7 @@ window.targetAPI.onPong((_event, value: any) => onPingPong(value));
|
|||
window.targetAPI.onConnect((_event, value: any) => {
|
||||
console.log(`Device connected: ${JSON.stringify(value)}`);
|
||||
connections.push(value.sessionId);
|
||||
uiUpdateCallbacks.onConnect(connections, value);
|
||||
uiUpdateCallbacks.onConnect(connections);
|
||||
});
|
||||
window.targetAPI.onDisconnect((_event, value: any) => {
|
||||
console.log(`Device disconnected: ${JSON.stringify(value)}`);
|
||||
|
|
|
@ -50,6 +50,10 @@ export class TcpListenerService {
|
|||
this.sessionMap[sessionId]?.socket.destroy();
|
||||
}
|
||||
|
||||
public getSessions(): string[] {
|
||||
return Object.keys(this.sessionMap);
|
||||
}
|
||||
|
||||
private async handleServerError(err: NodeJS.ErrnoException) {
|
||||
errorHandler(err);
|
||||
}
|
||||
|
|
|
@ -49,6 +49,10 @@ export class WebSocketListenerService {
|
|||
this.sessionMap[sessionId]?.close();
|
||||
}
|
||||
|
||||
public getSessions(): string[] {
|
||||
return Object.keys(this.sessionMap);
|
||||
}
|
||||
|
||||
private async handleServerError(err: NodeJS.ErrnoException) {
|
||||
errorHandler(err);
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@ if (TARGET === 'electron') {
|
|||
electronAPI.contextBridge.exposeInMainWorld('targetAPI', {
|
||||
onDeviceInfo: (callback: any) => electronAPI.ipcRenderer.on('device-info', callback),
|
||||
getDeviceInfo: () => preloadData.deviceInfo,
|
||||
getSessions: () => electronAPI.ipcRenderer.invoke('get-sessions'),
|
||||
sendSessionMessage: (opcode: Opcode, message: any) => electronAPI.ipcRenderer.send('send-session-message', { opcode: opcode, message: message }),
|
||||
disconnectDevice: (session: string) => electronAPI.ipcRenderer.send('disconnect-device', session),
|
||||
onConnect: (callback: any) => electronAPI.ipcRenderer.on('connect', callback),
|
||||
|
|
|
@ -15,12 +15,12 @@ let qrWidth = null;
|
|||
window.addEventListener('resize', (event) => calculateQRCodeWidth());
|
||||
|
||||
connectionMonitor.setUiUpdateCallbacks({
|
||||
onConnect: (connections: string[], connectionInfo: any) => {
|
||||
onConnect: (connections: string[], initialUpdate: boolean = false) => {
|
||||
connectionStatusText.textContent = connections.length > 1 ? 'Multiple devices connected:\r\n Ready to cast' : 'Connected: Ready to cast';
|
||||
connectionStatusSpinner.style.display = 'none';
|
||||
connectionStatusCheck.style.display = 'inline-block';
|
||||
},
|
||||
onDisconnect: (connections: string[], connectionInfo: any) => {
|
||||
onDisconnect: (connections: string[]) => {
|
||||
if (connections.length === 0) {
|
||||
connectionStatusText.textContent = 'Waiting for a connection';
|
||||
connectionStatusSpinner.style.display = 'inline-block';
|
||||
|
|
|
@ -28,6 +28,7 @@ if (TARGET === 'electron') {
|
|||
onPause: (callback: any) => electronAPI.ipcRenderer.on("pause", callback),
|
||||
onResume: (callback: any) => electronAPI.ipcRenderer.on("resume", callback),
|
||||
onSeek: (callback: any) => electronAPI.ipcRenderer.on("seek", callback),
|
||||
getSessions: () => electronAPI.ipcRenderer.invoke('get-sessions'),
|
||||
sendSessionMessage: (opcode: Opcode, message: any) => electronAPI.ipcRenderer.send('send-session-message', { opcode: opcode, message: message }),
|
||||
disconnectDevice: (session: string) => electronAPI.ipcRenderer.send('disconnect-device', session),
|
||||
onConnect: (callback: any) => electronAPI.ipcRenderer.on('connect', callback),
|
||||
|
|
|
@ -333,10 +333,12 @@ function onPlay(_event, value: PlayMessage) {
|
|||
};
|
||||
|
||||
connectionMonitor.setUiUpdateCallbacks({
|
||||
onConnect: (connections: string[], connectionInfo: any) => {
|
||||
toast('Device connected', ToastIcon.INFO);
|
||||
onConnect: (connections: string[], initialUpdate: boolean = false) => {
|
||||
if (!initialUpdate) {
|
||||
toast('Device connected', ToastIcon.INFO);
|
||||
}
|
||||
},
|
||||
onDisconnect: (connections: string[], connectionInfo: any) => {
|
||||
onDisconnect: (connections: string[]) => {
|
||||
toast('Device disconnected. If you experience playback issues, please reconnect.', ToastIcon.INFO);
|
||||
},
|
||||
});
|
||||
|
|
|
@ -281,6 +281,10 @@ export class Main {
|
|||
window.setFullScreen(false);
|
||||
});
|
||||
|
||||
ipcMain.handle('get-sessions', () => {
|
||||
return [].concat(Main.tcpListenerService.getSessions(), Main.webSocketListenerService.getSessions());
|
||||
});
|
||||
|
||||
if (Main.shouldOpenMainWindow) {
|
||||
Main.openMainWindow();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue