BarRaider / obs-websocket-dotnet

C# .NET library to communicate with an obs-websocket server
MIT License
224 stars 104 forks source link

[BUG] IsConnected only reflects the state of the websocket connection #122

Closed ProbablePrime closed 2 years ago

ProbablePrime commented 2 years ago

Issue Type

Describe the bug IsConnected currently only reflects the status of the internal WebSocket of this client. It doesn't reflect if a connection has actually been made and setup correctly.

If requests to OBS that require identification are made before the connection is fully identified OBS will disconnect you with an error reason of basically "Unidentified".

To get around this, I have to maintain my own separate boolean which gets flipped in response to the actual Connected/Disconnected events which this client outputs. While this works, It is confusing.

To Reproduce

  1. Init a new connection to a valid copy of OBS
  2. Check if IsConnected is set to true repeatedly
  3. Once IsConnected gets set to true, immediately try and make a request such as GetStats
  4. A disconnect will follow

Expected behavior I expect IsConnected to mirror the state reflected by the connected and disconnected events of this client. Right now there are two definitions of connected and they both mean different things:

  1. The connection events - These signify a proper fully connected setup
  2. The IsConnected boolean - This just signifies if the WebSocket open.

I expect these two meanings to be identical.

Screenshots If applicable, add screenshots to help explain your problem.

Versions OBS WebSocket Dotnet (this library) Version: Latest, you can just see this in the source code: https://github.com/BarRaider/obs-websocket-dotnet/blob/master/obs-websocket-dotnet/OBSWebsocket.cs#L58

Additional context In slightly brash terms, I consider the IsConnected boolean to be lieing.

BarRaider commented 2 years ago

You must subscribe to the Connected and Disconnected events after opening a connection. Do not rely on IsConnected before receiving a Connected event

ProbablePrime commented 2 years ago

Is that not a bug then?

Should not the IsConnected boolean reflect the accurate status?

IsConnected is effectively useless.

BarRaider commented 2 years ago

If you don't wait for the Connected event then yes, you are relying on just the Websocket connection without the actual authentication. This was a change introduced in Websocket v5 (previously connection already passed the authentication). I don't consider it a bug since IsConnected always checked the Websocket connection and not whether or not you can start sending commands. To send commands you MUST wait for a connected event. I cab update the readme to explain the correct connection flow.

ProbablePrime commented 2 years ago

Ok, thank you

Joshimuz commented 11 months ago

That's all well and all, using the events is the better approach, but then this should be changed. image