Compare commits

17 Commits

Author SHA1 Message Date
daa8c6ada1 adds Events::Client.on method
allows registering blocks to be called back later

examples, readme updated

minor bump
2023-08-17 23:09:32 +01:00
662f14282f move identified before start_driver 2023-08-17 16:28:43 +01:00
2e0e584d3a refactor 2023-08-17 16:18:50 +01:00
a8425cf7cd refactor 2023-08-17 16:13:57 +01:00
7e580dc91a check identified state in Event::Client
add passwordless test for event client

patch bump
2023-08-11 22:12:28 +01:00
92174219a7 patch bump 2023-08-11 22:05:28 +01:00
0c71eb2398 added no password error test 2023-08-11 22:05:14 +01:00
3ea21cd371 Identified class added. tracks identified state 2023-08-11 22:05:01 +01:00
70b60b3cac reword 2023-08-11 18:23:42 +01:00
c97d14abe2 move logger back into rescue block 2023-08-11 17:10:17 +01:00
123b9c55ca patch bump 2023-08-11 17:05:47 +01:00
299351cac0 refactor tests 2023-08-11 17:05:33 +01:00
3ef4396885 request ids are now UUID's. 2023-08-11 17:04:38 +01:00
c6bb8d07ff patch bump 2023-08-11 16:15:42 +01:00
48b94a2682 remove running reader method 2023-08-11 16:15:27 +01:00
210d13ba1e update main example to print mute state 2023-08-11 16:15:17 +01:00
59bcf2a338 assign timeout_sec directly 2023-08-11 16:14:53 +01:00
13 changed files with 151 additions and 133 deletions

View File

@@ -1,7 +1,7 @@
PATH PATH
remote: . remote: .
specs: specs:
obsws (0.5.3) obsws (0.5.8)
waitutil (~> 0.2.1) waitutil (~> 0.2.1)
websocket-driver (~> 0.7.5) websocket-driver (~> 0.7.5)

View File

@@ -30,12 +30,16 @@ Pass `host`, `port` and `password` as keyword arguments.
require "obsws" require "obsws"
class Main class Main
INPUT = "Mic/Aux"
def run def run
OBSWS::Requests::Client OBSWS::Requests::Client
.new(host: "localhost", port: 4455, password: "strongpassword") .new(host: "localhost", port: 4455, password: "strongpassword")
.run do |client| .run do |client|
# Toggle the mute state of your Mic input # Toggle the mute state of your Mic input and print its new mute state
client.toggle_input_mute("Mic/Aux") client.toggle_input_mute(INPUT)
resp = client.get_input_mute(INPUT)
puts "Input '#{INPUT}' was set to #{resp.input_muted}"
end end
end end
end end
@@ -63,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:
@@ -71,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
``` ```
@@ -115,7 +110,9 @@ If a connection attempt fails or times out an `OBSWSConnectionError` will be rai
If a request fails an `OBSWSRequestError` will be raised with a status code. If a request fails an `OBSWSRequestError` will be raised with a status code.
- The request name and code are retrievable through attributes {OBSWSRequestError}.req_name and {OBSWSRequestError}.code - The request name and code are retrievable through the following attributes:
- `req_name`
- `code`
For a full list of status codes refer to [Codes](https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md#requeststatus) For a full list of status codes refer to [Codes](https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md#requeststatus)

View File

@@ -1,21 +1,28 @@
require_relative "../../lib/obsws" require_relative "../../lib/obsws"
require "yaml" require "yaml"
class Main class Main
attr_reader :running
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
@@ -26,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

View File

@@ -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

View File

@@ -1,9 +1,10 @@
require "digest/sha2" require "digest/sha2"
require "json" require "json"
require "waitutil"
require "socket"
require "websocket/driver"
require "logger" require "logger"
require "securerandom"
require "socket"
require "waitutil"
require "websocket/driver"
require_relative "obsws/logger" require_relative "obsws/logger"
require_relative "obsws/driver" require_relative "obsws/driver"
@@ -24,19 +25,12 @@ module OBSWS
class OBSWSRequestError < OBSWSError class OBSWSRequestError < OBSWSError
attr_reader :req_name, :code attr_reader :req_name, :code
def initialize(req_name, code, msg) def initialize(req_name, code, comment)
@req_name = req_name @req_name = req_name
@code = code @code = code
@msg = msg message = "Request #{@req_name} returned code #{@code}."
message << " With message: #{comment}" if comment
super(message) super(message)
end end
def message
msg = [
"Request #{@req_name} returned code #{@code}."
]
msg << "With message: #{@msg}" if @msg
msg.join(" ")
end
end end
end end

View File

@@ -1,10 +1,27 @@
module OBSWS module OBSWS
class Identified
attr_accessor :state
def initialize
@state = :pending
end
def error_message
case @state
when :passwordless
"auth enabled but no password provided"
else
"failed to identify client with the websocket server"
end
end
end
class Base class Base
include Logging include Logging
include Driver::Director include Driver::Director
include Mixin::OPCodes include Mixin::OPCodes
attr_reader :closed attr_reader :closed, :identified
attr_writer :updater attr_writer :updater
def initialize(**kwargs) def initialize(**kwargs)
@@ -12,13 +29,13 @@ module OBSWS
port = kwargs[:port] || 4455 port = kwargs[:port] || 4455
@password = kwargs[:password] || "" @password = kwargs[:password] || ""
@subs = kwargs[:subs] || 0 @subs = kwargs[:subs] || 0
@connect_timeout = kwargs[:connect_timeout] || 3 @identified = Identified.new
setup_driver(host, port) and start_driver setup_driver(host, port) and start_driver
WaitUtil.wait_for_condition( WaitUtil.wait_for_condition(
"successful identification", "successful identification",
delay_sec: 0.01, delay_sec: 0.01,
timeout_sec: @connect_timeout timeout_sec: kwargs[:connect_timeout] || 3
) { @identified } ) { @identified.state != :pending }
end end
private private
@@ -39,7 +56,8 @@ module OBSWS
} }
if auth if auth
if @password.empty? if @password.empty?
raise OBSWSError("auth enabled but no password provided") @identified.state = :passwordless
return
end end
logger.info("initiating authentication") logger.info("initiating authentication")
payload[:d][:authentication] = auth_token(**auth) payload[:d][:authentication] = auth_token(**auth)
@@ -52,7 +70,7 @@ module OBSWS
when Mixin::OPCodes::HELLO when Mixin::OPCodes::HELLO
identify(data[:d][:authentication]) identify(data[:d][:authentication])
when Mixin::OPCodes::IDENTIFIED when Mixin::OPCodes::IDENTIFIED
@identified = true @identified.state = :identified
when Mixin::OPCodes::EVENT, Mixin::OPCodes::REQUESTRESPONSE when Mixin::OPCodes::EVENT, Mixin::OPCodes::REQUESTRESPONSE
@updater.call(data[:op], data[:d]) @updater.call(data[:op], data[:d])
end end

View File

@@ -28,49 +28,46 @@ 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
def initialize(**kwargs) def initialize(**kwargs)
kwargs[:subs] ||= SUBS::LOW_VOLUME kwargs[:subs] ||= SUBS::LOW_VOLUME
@base_client = Base.new(**kwargs) @base_client = Base.new(**kwargs)
unless @base_client.identified.state == :identified
err_msg = @base_client.identified.error_message
logger.error(err_msg)
raise OBSWSConnectionError.new(err_msg)
end
logger.info("#{self} successfully identified with server") logger.info("#{self} successfully identified with server")
rescue Errno::ECONNREFUSED, WaitUtil::TimeoutError => e rescue Errno::ECONNREFUSED, WaitUtil::TimeoutError => e
msg = "#{e.class.name}: #{e.message}" msg = "#{e.class.name}: #{e.message}"
@@ -82,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

View File

@@ -7,6 +7,11 @@ module OBSWS
def initialize(**kwargs) def initialize(**kwargs)
@base_client = Base.new(**kwargs) @base_client = Base.new(**kwargs)
unless @base_client.identified.state == :identified
err_msg = @base_client.identified.error_message
logger.error(err_msg)
raise OBSWSConnectionError.new(err_msg)
end
logger.info("#{self} successfully identified with server") logger.info("#{self} successfully identified with server")
rescue Errno::ECONNREFUSED, WaitUtil::TimeoutError => e rescue Errno::ECONNREFUSED, WaitUtil::TimeoutError => e
logger.error("#{e.class.name}: #{e.message}") logger.error("#{e.class.name}: #{e.message}")
@@ -35,13 +40,13 @@ module OBSWS
end end
def call(req, data = nil) def call(req, data = nil)
id = rand(1..1000) uuid = SecureRandom.uuid
@base_client.req(id, req, data) @base_client.req(uuid, req, data)
WaitUtil.wait_for_condition( WaitUtil.wait_for_condition(
"reponse id to match request id", "reponse id to match request id",
delay_sec: 0.001, delay_sec: 0.001,
timeout_sec: 3 timeout_sec: 3
) { @response[:requestId] == id } ) { @response[:requestId] == uuid }
unless @response[:requestStatus][:result] unless @response[:requestStatus][:result]
raise OBSWSRequestError.new(@response[:requestType], @response[:requestStatus][:code], @response[:requestStatus][:comment]) raise OBSWSRequestError.new(@response[:requestType], @response[:requestStatus][:code], @response[:requestStatus][:comment])
end end

View File

@@ -7,11 +7,11 @@ module OBSWS
end end
def minor def minor
5 6
end end
def patch def patch
4 0
end end
def to_a def to_a

16
main.rb
View File

@@ -1,14 +1,16 @@
require "obsws" require "obsws"
class Main class Main
INPUT = "Mic/Aux"
def run def run
OBSWS::Requests::Client.new( OBSWS::Requests::Client
host: "localhost", .new(host: "localhost", port: 4455, password: "strongpassword")
port: 4455, .run do |client|
password: "strongpassword" # Toggle the mute state of your Mic input and print its new mute state
).run do |client| client.toggle_input_mute(INPUT)
# Toggle the mute state of your Mic input resp = client.get_input_mute(INPUT)
client.toggle_input_mute("Mic/Aux") puts "Input '#{INPUT}' was set to #{resp.input_muted}"
end end
end end
end end

View File

@@ -1,6 +1,6 @@
require_relative "../minitest_helper" require_relative "../minitest_helper"
class AttrsTest < OBSWSTest class AttrsTest < Minitest::Test
def test_get_version_attrs def test_get_version_attrs
resp = OBSWSTest.r_client.get_version resp = OBSWSTest.r_client.get_version
assert resp.attrs == assert resp.attrs ==

View File

@@ -2,16 +2,35 @@ require_relative "../minitest_helper"
class OBSWSConnectionErrorTest < Minitest::Test class OBSWSConnectionErrorTest < Minitest::Test
def test_it_raises_an_obsws_connection_error_on_wrong_password def test_it_raises_an_obsws_connection_error_on_wrong_password
e = assert_raises(OBSWS::OBSWSConnectionError) { OBSWS::Requests::Client.new(host: "localhost", port: 4455, password: "wrongpassword", connect_timeout: 1).new } e = assert_raises(OBSWS::OBSWSConnectionError) do
assert_equal(e.message, "Timed out waiting for successful identification (1 seconds elapsed)") OBSWS::Requests::Client
.new(host: "localhost", port: 4455, password: "wrongpassword", connect_timeout: 0.1)
end
assert_equal("Timed out waiting for successful identification (0.1 seconds elapsed)", e.message)
end
def test_it_raises_an_obsws_connection_error_on_auth_enabled_but_no_password_provided_for_request_client
e = assert_raises(OBSWS::OBSWSConnectionError) do
OBSWS::Requests::Client
.new(host: "localhost", port: 4455, password: "")
end
assert_equal("auth enabled but no password provided", e.message)
end
def test_it_raises_an_obsws_connection_error_on_auth_enabled_but_no_password_provided_for_event_client
e = assert_raises(OBSWS::OBSWSConnectionError) do
OBSWS::Events::Client
.new(host: "localhost", port: 4455, password: "")
end
assert_equal("auth enabled but no password provided", e.message)
end end
end end
class OBSWSRequestErrorTest < Minitest::Test class OBSWSRequestErrorTest < Minitest::Test
def test_it_raises_an_obsws_request_error_on_invalid_request def test_it_raises_an_obsws_request_error_on_invalid_request
e = assert_raises(OBSWS::OBSWSRequestError) { OBSWSTest.r_client.toggle_input_mute("unknown") } e = assert_raises(OBSWS::OBSWSRequestError) { OBSWSTest.r_client.toggle_input_mute("unknown") }
assert_equal(e.req_name, "ToggleInputMute") assert_equal("ToggleInputMute", e.req_name)
assert_equal(e.code, 600) assert_equal(600, e.code)
assert_equal(e.message, "Request ToggleInputMute returned code 600. With message: No source was found by the name of `unknown`.") assert_equal("Request ToggleInputMute returned code 600. With message: No source was found by the name of `unknown`.", e.message)
end end
end end

View File

@@ -1,6 +1,6 @@
require_relative "../minitest_helper" require_relative "../minitest_helper"
class RequestTest < OBSWSTest class RequestTest < Minitest::Test
def test_it_checks_obs_major_version def test_it_checks_obs_major_version
resp = OBSWSTest.r_client.get_version resp = OBSWSTest.r_client.get_version
ver = resp.obs_version.split(".").map(&:to_i) ver = resp.obs_version.split(".").map(&:to_i)