-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobsAPI.js
More file actions
55 lines (48 loc) · 2.06 KB
/
Copy pathobsAPI.js
File metadata and controls
55 lines (48 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { OBSWebSocket } from "obs-websocket-js"
const obs = new OBSWebSocket()
import * as waybar from './servers/waybar.js'
import settings from './settings.js'
const obsAPI = {}
/**
* Refreshes the browser source in OBS
*
* @param {string} inputName - The name of the input to refresh
* @returns {Promise<void>} - A promise that resolves when the refresh is complete
*/
const refreshBrowserSource = async inputName => {
try {
await obs.call('PressInputPropertiesButton', {inputName, propertyName: 'refreshnocache'})
console.log(`Refreshed obs browser source ${inputName}`)
} catch (err) {
console.log(`Failed to refresh obs browser source ${inputName}`)
}
}
/**
* Connects to the OBS WebSocket server, sets up event listeners, and refreshes browser sources
*
* @returns {Promise<void>} - A promise that resolves when the connection is established
*/
obsAPI.connect = async () => {
try {
const {obsWebSocketVersion, negotiatedRpcVersion} = await obs.connect('ws://localhost:4455', '40QxzWqNCK7dLKGW', {rpcVersion: 1})
console.log(`Connected to obs server ${obsWebSocketVersion} (using RPC ${negotiatedRpcVersion})`)
/* Refresh Browser Source is called to refresh the webpages each time the program is launched.
It expects each repective source to have the following names. If you don't use these names in OBS, change them here.*/
refreshBrowserSource('Chat')
refreshBrowserSource('Fetch')
refreshBrowserSource('Waybar')
refreshBrowserSource('Lock')
refreshBrowserSource('Calendar')
} catch (err) {
console.log('Failed to connect to obs server');
console.log(`Attempting reconnect to OBS in ${settings.obsInterval / 1000} second(s)`)
return setTimeout(obsAPI.connect, settings.obsInterval)
}
obs.on('CurrentProgramSceneChanged', event => {
waybar.io.emit('CurrentProgramSceneChanged', event)
})
obs.on('InputActiveStateChanged', event => {
waybar.io.emit('InputActiveStateChanged', event)
})
}
export default obsAPI