mirror of
https://github.com/onyx-and-iris/obsws-ruby.git
synced 2024-11-15 18:50:47 +00:00
Callbacks module extended
Now supports callback methods as well observer classes levels example now uses callback methods register,deregister now alias add/remove observer methods minor version bump
This commit is contained in:
parent
155cbe019a
commit
4fdebc8178
23
README.md
23
README.md
@ -16,14 +16,10 @@
|
|||||||
### Bundler
|
### Bundler
|
||||||
|
|
||||||
```
|
```
|
||||||
bundle add 'obsws'
|
bundle add obsws
|
||||||
bundle install
|
bundle install
|
||||||
```
|
```
|
||||||
|
|
||||||
### Gem
|
|
||||||
|
|
||||||
`gem install 'obsws'`
|
|
||||||
|
|
||||||
## `Use`
|
## `Use`
|
||||||
|
|
||||||
#### Example `main.rb`
|
#### Example `main.rb`
|
||||||
@ -67,23 +63,28 @@ For a full list of requests refer to [Requests](https://github.com/obsproject/ob
|
|||||||
|
|
||||||
### Events
|
### Events
|
||||||
|
|
||||||
Register an observer class and define `on_` methods for events. Method names should match the api event but snake cased.
|
Register `on_` callback methods. Method names should match the api event but snake cased.
|
||||||
|
|
||||||
example:
|
example:
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
class Observer
|
class Observer
|
||||||
def initialize
|
def initialize
|
||||||
@e_client = OBSWS::Events::Client.new(**kwargs)
|
@e_client = OBSWS::Events::Client.new(host: "localhost", port: 4455, password: "strongpassword")
|
||||||
# register class with the event client
|
# register callback methods with the Event client
|
||||||
@e_client.add_observer(self)
|
@e_client.register(
|
||||||
|
[
|
||||||
|
method(:on_current_program_scene_changed),
|
||||||
|
method(:on_input_mute_state_changed)
|
||||||
|
]
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
# define "on_" event methods.
|
# define "on_" event methods.
|
||||||
def on_current_program_scene_changed
|
def on_current_program_scene_changed(data)
|
||||||
...
|
...
|
||||||
end
|
end
|
||||||
def on_input_mute_state_changed
|
def on_input_mute_state_changed(data)
|
||||||
...
|
...
|
||||||
end
|
end
|
||||||
...
|
...
|
||||||
|
@ -21,11 +21,9 @@ class Main
|
|||||||
def infostring
|
def infostring
|
||||||
resp = @r_client.get_version
|
resp = @r_client.get_version
|
||||||
[
|
[
|
||||||
"Using obs version:",
|
"Using obs version: #{resp.obs_version}.",
|
||||||
resp.obs_version,
|
"With websocket version: #{resp.obs_web_socket_version}"
|
||||||
"With websocket version:",
|
].join(" ")
|
||||||
resp.obs_web_socket_version
|
|
||||||
].join("\n")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def on_current_program_scene_changed(data)
|
def on_current_program_scene_changed(data)
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
require_relative "../../lib/obsws"
|
require_relative "../../lib/obsws"
|
||||||
require "yaml"
|
require "yaml"
|
||||||
|
|
||||||
|
|
||||||
module LevelTypes
|
module LevelTypes
|
||||||
VU = 0
|
VU = 0
|
||||||
POSTFADER = 1
|
POSTFADER = 1
|
||||||
@ -14,7 +13,12 @@ class Main
|
|||||||
def initialize(**kwargs)
|
def initialize(**kwargs)
|
||||||
subs = OBSWS::Events::SUBS::LOW_VOLUME | OBSWS::Events::SUBS::INPUTVOLUMEMETERS
|
subs = OBSWS::Events::SUBS::LOW_VOLUME | OBSWS::Events::SUBS::INPUTVOLUMEMETERS
|
||||||
@e_client = OBSWS::Events::Client.new(subs:, **kwargs)
|
@e_client = OBSWS::Events::Client.new(subs:, **kwargs)
|
||||||
@e_client.add_observer(self)
|
@e_client.register(
|
||||||
|
[
|
||||||
|
method(:on_input_mute_state_changed),
|
||||||
|
method(:on_input_volume_meters)
|
||||||
|
]
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def run
|
def run
|
||||||
|
@ -43,7 +43,7 @@ module OBSWS
|
|||||||
|
|
||||||
def add_observer(observer)
|
def add_observer(observer)
|
||||||
observer = [observer] if !observer.respond_to? :each
|
observer = [observer] if !observer.respond_to? :each
|
||||||
observer.each { |o| observers << o }
|
observer.each { |o| observers << o unless observers.include? o }
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_observer(observer)
|
def remove_observer(observer)
|
||||||
@ -52,15 +52,18 @@ module OBSWS
|
|||||||
|
|
||||||
def notify_observers(event, data)
|
def notify_observers(event, data)
|
||||||
observers.each do |o|
|
observers.each do |o|
|
||||||
if o.respond_to? "on_#{snakecase(event)}"
|
if o.is_a? Method
|
||||||
if data.empty?
|
if o.name.to_s == "on_#{snakecase(event)}"
|
||||||
o.send("on_#{snakecase(event)}")
|
data.empty? ? o.call : o.call(data)
|
||||||
else
|
|
||||||
o.send("on_#{snakecase(event)}", data)
|
|
||||||
end
|
end
|
||||||
|
elsif o.respond_to? "on_#{snakecase(event)}"
|
||||||
|
data.empty? ? o.send("on_#{snakecase(event)}") : o.send("on_#{snakecase(event)}", data)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
alias_method :register, :add_observer
|
||||||
|
alias_method :deregister, :remove_observer
|
||||||
end
|
end
|
||||||
|
|
||||||
class Client
|
class Client
|
||||||
|
@ -7,11 +7,11 @@ module OBSWS
|
|||||||
end
|
end
|
||||||
|
|
||||||
def minor
|
def minor
|
||||||
3
|
4
|
||||||
end
|
end
|
||||||
|
|
||||||
def patch
|
def patch
|
||||||
3
|
0
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_a
|
def to_a
|
||||||
|
Loading…
Reference in New Issue
Block a user