Skip to content

Commit

Permalink
rework obs connection
Browse files Browse the repository at this point in the history
  • Loading branch information
danilopolani committed Sep 13, 2023
1 parent 48861bf commit c3c82d5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/main/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ app
HttpServer.init();
WorkflowQueue.init();
Twitch.init(process.env.TWITCH_CLIENT_ID!, process.env.TWITCH_CLIENT_SECRET!).connect();
OBSWebSocket;
OBSWebSocket.init();

// Auto updater
if (import.meta.env.PROD) {
Expand Down
22 changes: 19 additions & 3 deletions packages/main/src/workers/OBSWebSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ export const OBSWebSocket = new class {

constructor() {
this.obs = new OBSWebSocketLib();
}

async init() {
this.settings = (await this.getSettings()) || undefined;

if (!this.settings) {
tellRenderer({
subject: ObsWebSocketSubject.Connection,
message: IntegrationConnectionStatus.ObsConnectionToConfigure,
details: 'Connection needs configuration',
});
}

// Refresh app status check from time to time
setInterval(() => this.verifyObsOpen(), 1000);
Expand All @@ -26,9 +38,9 @@ export const OBSWebSocket = new class {
* Connect OBS WebSocket.
*/
async connect(shouldNotifyError = true): Promise<string | undefined> {
this.settings = (await (Setting<ObsWebSocketSettings>).findByPk(SettingName.ObsAuth)) || undefined;
this.settings = (await this.getSettings()) || undefined;

if (!this.settings?.value.shouldConnect) {
if (!this.settings) {
tellRenderer({
subject: ObsWebSocketSubject.Connection,
message: IntegrationConnectionStatus.ObsConnectionToConfigure,
Expand Down Expand Up @@ -94,6 +106,10 @@ export const OBSWebSocket = new class {
await this.obs.call('SetInputMute', { inputName: source, inputMuted: false });
}

async getSettings() {
return (Setting<ObsWebSocketSettings>).findByPk(SettingName.ObsAuth);
}

private async verifyObsOpen() {
// Avoid issues when this file is minified in actual workers such as WorkflowQueue
if (inWorkerContext()) {
Expand All @@ -109,7 +125,7 @@ export const OBSWebSocket = new class {
const isOpen = res.find((item) => item.name.toLocaleLowerCase().includes('obs')) !== undefined;

// If app was closed and now is open, retrying connection
if (!this.isObsOpen && isOpen) {
if (!this.isObsOpen && isOpen && this.settings) {
this.connect();
}

Expand Down
5 changes: 1 addition & 4 deletions packages/preload/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ export async function generalUpdateSettings(data: GeneralSettings): Promise<void
}

export async function obsConnect(settings: ObsWebSocketConnection): Promise<string | undefined> {
return ipcRenderer.invoke('obs:connect', {
...settings,
shouldConnect: true, // Force connection
} as ObsWebSocketSettings);
return ipcRenderer.invoke('obs:connect', settings);
}

export async function obsRetryConnection(): Promise<void> {
Expand Down
2 changes: 0 additions & 2 deletions packages/shared/ObsWebSocketSettings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export type ObsWebSocketSettings = {
shouldConnect: boolean
host: string
port: string
password: string
Expand All @@ -8,7 +7,6 @@ export type ObsWebSocketSettings = {
export type ObsWebSocketConnection = Pick<ObsWebSocketSettings, 'host' | 'port' | 'password'>;

export const ObsWebSocketSettingsDefaults: ObsWebSocketSettings = {
shouldConnect: false,
host: '127.0.0.1',
port: '4455',
password: '',
Expand Down

0 comments on commit c3c82d5

Please sign in to comment.