obs-websocket-community-projects / obs-websocket-js

Consumes https://github.com/obsproject/obs-websocket
MIT License
661 stars 95 forks source link

Cannot connect to Websocket #278

Closed stuhowarth closed 2 years ago

stuhowarth commented 2 years ago

Every time i run the obs.connect() function I am getting the following errors:


base.ts:110
WebSocket connection to 'ws://127.0.0.1:4444/' failed: WebSocket is closed before the connection is established.
  | disconnect | @ | base.ts:110 -- | -- | -- | --   | connect | @ | base.ts:68   | OBSConnect | @ | OBSConnect.js:9   | App | @ | App.js:8   | renderWithHooks | @ | react-dom.development.js:14985   | mountIndeterminateComponent | @ | react-dom.development.js:17883   | beginWork | @ | react-dom.development.js:19049   | beginWork$1 | @ | react-dom.development.js:23940   | performUnitOfWork | @ | react-dom.development.js:22776   | workLoopSync | @ | react-dom.development.js:22707   | renderRootSync | @ | react-dom.development.js:22670   | performSyncWorkOnRoot | @ | react-dom.development.js:22293   | scheduleUpdateOnFiber | @ | react-dom.development.js:21881   | updateContainer | @ | react-dom.development.js:25482   | (anonymous) | @ | react-dom.development.js:26021   | unbatchedUpdates | @ | react-dom.development.js:22431   | legacyRenderSubtreeIntoContainer | @ | react-dom.development.js:26020   | render | @ | react-dom.development.js:26103   | ./src/index.js | @ | index.js:7   | options.factory | @ | react refresh:6   | webpack_require | @ | bootstrap:24   | (anonymous) | @ | startup:7   | (anonymous) | @ | startup:7

I also get the following error:

Failed to connect 1006
Which is being fired from my console log and its firing a 1006 error code.

Weirdly though when I load the page and these errors appear I see an OBS systray notification saying:

New WebSocket connection
Client: 127.0.0.1:64552 connected

And then a few seconds later I get another notification saying it has disconnected so clearly the JS is connecting to the websocket in some sort of capacity.

This is my code:


import OBSWebSocket from 'obs-websocket-js';

const obs = new OBSWebSocket();
const OBSConnect = async() => {
    try {
        console.log('Connecting to OBS Webscoket')
        const {
            obsWebSocketVersion,
            negotiatedRpcVersion
        } = await obs.connect('ws://127.0.0.1:4444', 'password');
        console.log(`Connected to server ${obsWebSocketVersion} (using RPC ${negotiatedRpcVersion})`)
    } catch (error) {
    console.error('Failed to connect', error.code, error.message);
    }
}

export default OBSConnect;

What could be going wrong? I have tried top rule out firewall issues by disabling my firewall to and it isn't that. Any help is greatly appreciated :)

System Versions:

OBS - 27.2.3
obs-websocket - 4.9.1
Node - 16.14.2

The OBS websocket appears in OBS and I have kept the port to 4444 and tried both with and without a password.

t2t2 commented 2 years ago

obs-websocket - 4.9.1

Use v4(.0.3) of obs-websocket-js - documentation

stuhowarth commented 2 years ago

So I have downgraded to the version mentioned and I am still getting an error:

WebSocket connection to 'ws://localhost:4444/' failed: WebSocket is closed before the connection is established.
  | connect | @ | Socket.js:31 -- | -- | -- | --   | OBSConnect | @ | OBSConnect.js:7   | App | @ | App.js:8   | renderWithHooks | @ | react-dom.development.js:14985   | mountIndeterminateComponent | @ | react-dom.development.js:17883   | beginWork | @ | react-dom.development.js:19049   | beginWork$1 | @ | react-dom.development.js:23940   | performUnitOfWork | @ | react-dom.development.js:22776   | workLoopSync | @ | react-dom.development.js:22707   | renderRootSync | @ | react-dom.development.js:22670   | performSyncWorkOnRoot | @ | react-dom.development.js:22293   | scheduleUpdateOnFiber | @ | react-dom.development.js:21881   | updateContainer | @ | react-dom.development.js:25482   | (anonymous) | @ | react-dom.development.js:26021   | unbatchedUpdates | @ | react-dom.development.js:22431   | legacyRenderSubtreeIntoContainer | @ | react-dom.development.js:26020   | render | @ | react-dom.development.js:26103   | ./src/index.js | @ | index.js:7   | options.factory | @ | react refresh:6   | __webpack_require__ | @ | bootstrap:24   | (anonymous) | @ | startup:7   | (anonymous) | @ | startup:7

As you can see it is now using Socket.js file.

t2t2 commented 2 years ago

Enable debug logging in obs-websocket settings and check what log (Help ->Log Files -> View Current Log) shows after connecting.

image

Also useful to check the websocket request in the network tab of the browser's dev tools (in chrome click on the request and check messages tab, in firefox click on the request and check response tab, firefox would also show a more detailed close reason)

image

stuhowarth commented 2 years ago

Thank you for your help. I now have this working, it was because i was using the old async calls. I have changed my code to the following:

const OBSWebSocket = require('obs-websocket-js');
const obs = new OBSWebSocket();
obs.connect({ address: 'localhost:4444', password: 'password' }).then(() =>{

    obs.sendCallback('GetSceneList', {}, (err, data) => {
        console.log('Using callbacks:', err, data);
      });
})

I can confirm this now opens the connection and I can see the scenes. Thank you.