mirror of
https://github.com/onyx-and-iris/obsws-ruby.git
synced 2024-11-15 18:50:47 +00:00
adds Events::Client.on method
allows registering blocks to be called back later examples, readme updated minor bump
This commit is contained in:
parent
662f14282f
commit
daa8c6ada1
19
README.md
19
README.md
@ -67,7 +67,7 @@ For a full list of requests refer to [Requests](https://github.com/obsproject/ob
|
|||||||
|
|
||||||
### Events
|
### Events
|
||||||
|
|
||||||
Register `on_` callback methods. Method names should match the api event but snake cased.
|
Register blocks with the Event client using the `on` method. The event data will be passed to the block.
|
||||||
|
|
||||||
example:
|
example:
|
||||||
|
|
||||||
@ -75,23 +75,14 @@ example:
|
|||||||
class Observer
|
class Observer
|
||||||
def initialize
|
def initialize
|
||||||
@e_client = OBSWS::Events::Client.new(host: "localhost", port: 4455, password: "strongpassword")
|
@e_client = OBSWS::Events::Client.new(host: "localhost", port: 4455, password: "strongpassword")
|
||||||
# register callback methods with the Event client
|
# register blocks on event types.
|
||||||
@e_client.register(
|
@e_client.on(:current_program_scene_changed) do |data|
|
||||||
[
|
|
||||||
method(:on_current_program_scene_changed),
|
|
||||||
method(:on_input_mute_state_changed)
|
|
||||||
]
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
# define "on_" event methods.
|
|
||||||
def on_current_program_scene_changed(data)
|
|
||||||
...
|
...
|
||||||
end
|
end
|
||||||
def on_input_mute_state_changed(data)
|
@e_client.on(:input_mute_state_changed) do |data|
|
||||||
...
|
...
|
||||||
end
|
end
|
||||||
...
|
end
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -1,19 +1,28 @@
|
|||||||
require_relative "../../lib/obsws"
|
require_relative "../../lib/obsws"
|
||||||
require "yaml"
|
require "yaml"
|
||||||
|
|
||||||
|
|
||||||
class Main
|
class Main
|
||||||
def initialize(**kwargs)
|
def initialize(**kwargs)
|
||||||
@r_client = OBSWS::Requests::Client.new(**kwargs)
|
@r_client = OBSWS::Requests::Client.new(**kwargs)
|
||||||
@e_client = OBSWS::Events::Client.new(**kwargs)
|
@e_client = OBSWS::Events::Client.new(**kwargs)
|
||||||
@e_client.add_observer(self)
|
|
||||||
|
|
||||||
puts infostring
|
@e_client.on(:current_program_scene_changed) do |data|
|
||||||
@running = true
|
puts "Switched to scene #{data.scene_name}"
|
||||||
|
end
|
||||||
|
@e_client.on(:scene_created) do |data|
|
||||||
|
puts "scene #{data.scene_name} has been created"
|
||||||
|
end
|
||||||
|
@e_client.on(:input_mute_state_changed) do |data|
|
||||||
|
puts "#{data.input_name} mute toggled"
|
||||||
|
end
|
||||||
|
@e_client.on(:exit_started) do
|
||||||
|
puts "OBS closing!"
|
||||||
|
@r_client.close
|
||||||
|
@e_client.close
|
||||||
|
@running = false
|
||||||
end
|
end
|
||||||
|
|
||||||
def run
|
puts infostring
|
||||||
sleep(0.1) while @running
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def infostring
|
def infostring
|
||||||
@ -24,23 +33,9 @@ class Main
|
|||||||
].join(" ")
|
].join(" ")
|
||||||
end
|
end
|
||||||
|
|
||||||
def on_current_program_scene_changed(data)
|
def run
|
||||||
puts "Switched to scene #{data.scene_name}"
|
@running = true
|
||||||
end
|
sleep(0.1) while @running
|
||||||
|
|
||||||
def on_scene_created(data)
|
|
||||||
puts "scene #{data.scene_name} has been created"
|
|
||||||
end
|
|
||||||
|
|
||||||
def on_input_mute_state_changed(data)
|
|
||||||
puts "#{data.input_name} mute toggled"
|
|
||||||
end
|
|
||||||
|
|
||||||
def on_exit_started
|
|
||||||
puts "OBS closing!"
|
|
||||||
@r_client.close
|
|
||||||
@e_client.close
|
|
||||||
@running = false
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -13,26 +13,13 @@ 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.register(
|
|
||||||
[
|
|
||||||
method(:on_input_mute_state_changed),
|
|
||||||
method(:on_input_volume_meters)
|
|
||||||
]
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
def run
|
@e_client.on(:input_mute_state_changed) do |data|
|
||||||
puts "press <Enter> to quit"
|
|
||||||
loop { break if gets.chomp.empty? }
|
|
||||||
end
|
|
||||||
|
|
||||||
def on_input_mute_state_changed(data)
|
|
||||||
if data.input_name == DEVICE
|
if data.input_name == DEVICE
|
||||||
puts "#{DEVICE} mute toggled"
|
puts "#{DEVICE} mute toggled"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@e_client.on(:input_volume_meters) do |data|
|
||||||
def on_input_volume_meters(data)
|
|
||||||
fget = ->(x) { (x > 0) ? (20 * Math.log(x, 10)).round(1) : -200.0 }
|
fget = ->(x) { (x > 0) ? (20 * Math.log(x, 10)).round(1) : -200.0 }
|
||||||
|
|
||||||
data.inputs.each do |d|
|
data.inputs.each do |d|
|
||||||
@ -45,6 +32,12 @@ class Main
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def run
|
||||||
|
puts "press <Enter> to quit"
|
||||||
|
loop { break if gets.chomp.empty? }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def conn_from_yaml
|
def conn_from_yaml
|
||||||
YAML.load_file("obs.yml", symbolize_names: true)[:connection]
|
YAML.load_file("obs.yml", symbolize_names: true)[:connection]
|
||||||
end
|
end
|
||||||
|
@ -28,43 +28,35 @@ module OBSWS
|
|||||||
ALL = LOW_VOLUME | HIGH_VOLUME
|
ALL = LOW_VOLUME | HIGH_VOLUME
|
||||||
end
|
end
|
||||||
|
|
||||||
module Callbacks
|
module EventDirector
|
||||||
include Util::String
|
include Util::String
|
||||||
|
|
||||||
def observers
|
def observers
|
||||||
@observers ||= []
|
@observers ||= {}
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_observer(observer)
|
def on(event, method = nil, &block)
|
||||||
observer = [observer] unless observer.respond_to? :each
|
(observers[event] ||= []) << (block || method)
|
||||||
observer.each { |o| observers << o unless observers.include? o }
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_observer(observer)
|
def register(cbs)
|
||||||
observer = [observer] unless observer.respond_to? :each
|
cbs = [cbs] unless cbs.respond_to? :each
|
||||||
observers.reject! { |o| observer.include? o }
|
cbs.each { |cb| on(cb.name[3..].to_sym, cb) }
|
||||||
end
|
end
|
||||||
|
|
||||||
private def notify_observers(event, data)
|
def deregister(cbs)
|
||||||
observers.each do |o|
|
cbs = [cbs] unless cbs.respond_to? :each
|
||||||
if o.is_a? Method
|
cbs.each { |cb| observers[cb.name[3..].to_sym]&.reject! { |o| cbs.include? o } }
|
||||||
if o.name.to_s == "on_#{snakecase(event)}"
|
|
||||||
data.empty? ? o.call : o.call(data)
|
|
||||||
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
|
||||||
|
|
||||||
alias_method :callbacks, :observers
|
def fire(event, data)
|
||||||
alias_method :register, :add_observer
|
observers[snakecase(event).to_sym]&.each { |block| data.empty? ? block.call : block.call(data) }
|
||||||
alias_method :deregister, :remove_observer
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Client
|
class Client
|
||||||
include Logging
|
include Logging
|
||||||
include Callbacks
|
include EventDirector
|
||||||
include Mixin::TearDown
|
include Mixin::TearDown
|
||||||
include Mixin::OPCodes
|
include Mixin::OPCodes
|
||||||
|
|
||||||
@ -87,7 +79,7 @@ module OBSWS
|
|||||||
logger.debug("received: #{data}")
|
logger.debug("received: #{data}")
|
||||||
event = data[:eventType]
|
event = data[:eventType]
|
||||||
data = data.fetch(:eventData, {})
|
data = data.fetch(:eventData, {})
|
||||||
notify_observers(event, Mixin::Data.new(data, data.keys))
|
fire(event, Mixin::Data.new(data, data.keys))
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
@ -7,11 +7,11 @@ module OBSWS
|
|||||||
end
|
end
|
||||||
|
|
||||||
def minor
|
def minor
|
||||||
5
|
6
|
||||||
end
|
end
|
||||||
|
|
||||||
def patch
|
def patch
|
||||||
8
|
0
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_a
|
def to_a
|
||||||
|
Loading…
Reference in New Issue
Block a user