Here is some Javascript code to connect to OBS websocket. Rewrite it to run in the…
- User
Here is some Javascript code to connect to OBS websocket. Rewrite it to run in the browser without async, const+let, or await.
Assistant
Sure! Here's a version of JavaScript code to connect to an OBS WebSocket server that works in the browser, and avoids using:
async/awaitconstorlet(usesvarinstead)- Uses traditional callbacks and
Promise.then()if needed
This example connects to OBS Websocket, authenticates (if needed), and sends a basic request like changing the scene.
✅ Original Code (Modern ES6+)
Let’s assume your original code looked something like this:
const obs = new OBSWebSocket(); async function connectToOBS() { try { await obs.connect('ws://localhost:4444', 'password'); console.log('Connected to OBS'); const scenes = await obs.send('GetSceneList'); console.log('Scenes:', scenes); await obs.send('SetCurrentScene', { 'scene-name': scenes.scenes[0].name }); console.log('Changed scene'); } catch (error) { console.error('Connection failed:', error); } }
✅ Rewritten in ES5-Compatible Style (No async/await, no const/let)
Here's the equivalent code using older syntax:
<script src="https://cdn.jsdelivr.net/npm/@cloudnc/obs-websocket-js@2.1.0/dist/obs-websocket.min.js"></script> <script> var obs = new OBSWebSocket(); function connectToOBS() { obs.connect('ws://localhost:4444', 'password') .then(function () { console.log('Connected to OBS'); return obs.send('GetSceneList'); }) .then(function (scenes) { console.log('Scenes:', scenes); return obs.send('SetCurrentScene', { 'scene-name': scenes.scenes[0].name }); }) .then(function () { console.log('Changed scene'); }) .catch(function (error) { console.error('Connection failed:', error); }); } // Call the function connectToOBS(); </script>
🔒 Notes:
- Make sure you're connecting from a secure context (e.g., HTTPS or localhost).
- If your OBS is secured with a password, include it as shown.
- This uses the obs-websocket-js library via CDN.
Let me know if you want a version without even
Promise.then()— e.g., fully callback-based — though most modern libraries likeobs-websocket-jsrely on Promises.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.