2022-10-23 05:20:05 +01:00
|
|
|
[![Gem Version](https://badge.fury.io/rb/obsws.svg)](https://badge.fury.io/rb/obsws)
|
2022-10-23 05:25:47 +01:00
|
|
|
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/onyx-and-iris/obsws-ruby/blob/dev/LICENSE)
|
2023-07-19 15:14:28 +01:00
|
|
|
[![Ruby Code Style](https://img.shields.io/badge/code_style-standard-violet.svg)](https://github.com/standardrb/standard)
|
2022-10-22 22:30:40 +01:00
|
|
|
|
2023-07-26 16:12:38 +01:00
|
|
|
# Ruby Clients for OBS Studio WebSocket v5.0
|
2022-10-22 22:30:40 +01:00
|
|
|
|
|
|
|
## Requirements
|
|
|
|
|
2023-07-19 15:14:28 +01:00
|
|
|
- [OBS Studio](https://obsproject.com/)
|
|
|
|
- [OBS Websocket v5 Plugin](https://github.com/obsproject/obs-websocket/releases/tag/5.0.0)
|
|
|
|
- With the release of OBS Studio version 28, Websocket plugin is included by default. But it should be manually installed for earlier versions of OBS.
|
|
|
|
- Ruby 3.0 or greater
|
2022-10-22 22:30:40 +01:00
|
|
|
|
2022-10-24 02:01:30 +01:00
|
|
|
## Installation
|
|
|
|
|
|
|
|
### Bundler
|
|
|
|
|
|
|
|
```
|
2023-07-27 14:55:00 +01:00
|
|
|
bundle add obsws
|
2022-10-24 02:01:30 +01:00
|
|
|
bundle install
|
|
|
|
```
|
|
|
|
|
2022-10-22 22:30:40 +01:00
|
|
|
## `Use`
|
|
|
|
|
|
|
|
#### Example `main.rb`
|
|
|
|
|
2023-07-21 06:37:14 +01:00
|
|
|
Pass `host`, `port` and `password` as keyword arguments.
|
2022-10-22 22:30:40 +01:00
|
|
|
|
|
|
|
```ruby
|
2022-10-22 22:35:55 +01:00
|
|
|
require "obsws"
|
2022-10-22 22:30:40 +01:00
|
|
|
|
2023-07-21 06:37:14 +01:00
|
|
|
class Main
|
|
|
|
def run
|
|
|
|
OBSWS::Requests::Client
|
|
|
|
.new(host: "localhost", port: 4455, password: "strongpassword")
|
|
|
|
.run do |client|
|
|
|
|
# Toggle the mute state of your Mic input
|
|
|
|
client.toggle_input_mute("Mic/Aux")
|
|
|
|
end
|
|
|
|
end
|
2022-10-22 22:30:40 +01:00
|
|
|
end
|
|
|
|
|
2023-07-21 06:37:14 +01:00
|
|
|
Main.new.run if $PROGRAM_NAME == __FILE__
|
2022-10-22 22:30:40 +01:00
|
|
|
```
|
|
|
|
|
2023-07-21 06:37:14 +01:00
|
|
|
Passing OBSWS::Requests::Client.run a block closes the socket once the block returns.
|
|
|
|
|
2022-10-22 22:30:40 +01:00
|
|
|
### Requests
|
|
|
|
|
2023-07-21 06:37:14 +01:00
|
|
|
Method names for requests match the API calls but snake cased.
|
2022-10-22 22:30:40 +01:00
|
|
|
|
|
|
|
example:
|
|
|
|
|
|
|
|
```ruby
|
2023-07-21 06:37:14 +01:00
|
|
|
# GetVersion
|
|
|
|
resp = r_client.get_version
|
2022-10-22 22:30:40 +01:00
|
|
|
|
2023-07-21 06:37:14 +01:00
|
|
|
# SetCurrentProgramScene
|
|
|
|
r_client.set_current_program_scene("BRB")
|
2022-10-22 22:30:40 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
For a full list of requests refer to [Requests](https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md#requests)
|
|
|
|
|
|
|
|
### Events
|
|
|
|
|
2023-07-27 14:55:00 +01:00
|
|
|
Register `on_` callback methods. Method names should match the api event but snake cased.
|
2022-10-22 22:30:40 +01:00
|
|
|
|
|
|
|
example:
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
class Observer
|
|
|
|
def initialize
|
2023-07-27 14:55:00 +01:00
|
|
|
@e_client = OBSWS::Events::Client.new(host: "localhost", port: 4455, password: "strongpassword")
|
|
|
|
# register callback methods with the Event client
|
|
|
|
@e_client.register(
|
|
|
|
[
|
|
|
|
method(:on_current_program_scene_changed),
|
|
|
|
method(:on_input_mute_state_changed)
|
|
|
|
]
|
|
|
|
)
|
2022-10-22 22:30:40 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# define "on_" event methods.
|
2023-07-27 14:55:00 +01:00
|
|
|
def on_current_program_scene_changed(data)
|
2022-10-22 22:30:40 +01:00
|
|
|
...
|
|
|
|
end
|
2023-07-27 14:55:00 +01:00
|
|
|
def on_input_mute_state_changed(data)
|
2022-10-22 22:30:40 +01:00
|
|
|
...
|
|
|
|
end
|
|
|
|
...
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
|
|
|
For a full list of events refer to [Events](https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md#events)
|
|
|
|
|
|
|
|
### Attributes
|
|
|
|
|
|
|
|
For both request responses and event data you may inspect the available attributes using `attrs`.
|
|
|
|
|
|
|
|
example:
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
resp = cl.get_version
|
|
|
|
p resp.attrs
|
|
|
|
|
|
|
|
def on_scene_created(data):
|
|
|
|
p data.attrs
|
|
|
|
```
|
|
|
|
|
|
|
|
### Errors
|
|
|
|
|
2023-08-11 02:22:14 +01:00
|
|
|
If a general error occurs an `OBSWSError` will be raised.
|
|
|
|
|
|
|
|
If a connection attempt fails or times out an `OBSWSConnectionError` will be raised.
|
|
|
|
|
|
|
|
If a request fails an `OBSWSRequestError` will be raised with a status code.
|
|
|
|
|
2023-08-11 13:10:44 +01:00
|
|
|
- The request name and code are retrievable through attributes {OBSWSRequestError}.req_name and {OBSWSRequestError}.code
|
2022-10-22 22:30:40 +01:00
|
|
|
|
|
|
|
For a full list of status codes refer to [Codes](https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md#requeststatus)
|
|
|
|
|
2022-11-25 17:57:56 +00:00
|
|
|
### Logging
|
|
|
|
|
2023-07-26 14:37:49 +01:00
|
|
|
To enable logs set an environmental variable `OBSWS_LOG_LEVEL` to the appropriate level.
|
2022-11-25 17:57:56 +00:00
|
|
|
|
2023-07-26 14:37:49 +01:00
|
|
|
example in powershell:
|
2022-11-25 17:57:56 +00:00
|
|
|
|
2023-07-26 14:37:49 +01:00
|
|
|
```powershell
|
|
|
|
$env:OBSWS_LOG_LEVEL="DEBUG"
|
2022-11-25 17:57:56 +00:00
|
|
|
```
|
|
|
|
|
2022-10-22 22:30:40 +01:00
|
|
|
### Tests
|
|
|
|
|
|
|
|
To run all tests:
|
|
|
|
|
|
|
|
```
|
|
|
|
bundle exec rake -v
|
|
|
|
```
|
|
|
|
|
|
|
|
### Official Documentation
|
|
|
|
|
|
|
|
For the full documentation:
|
|
|
|
|
2023-07-19 15:14:28 +01:00
|
|
|
- [OBS Websocket SDK](https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md#obs-websocket-501-protocol)
|