Compare commits

..

No commits in common. "7e580dc91a39ad186da644fed8adf66a50970efd" and "dc8ac155eced7f7c7b3344c2387813520ade1a6a" have entirely different histories.

12 changed files with 34 additions and 88 deletions

View File

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

View File

@ -30,17 +30,13 @@ Pass `host`, `port` and `password` as keyword arguments.
require "obsws"
class Main
INPUT = "Mic/Aux"
def run
OBSWS::Requests::Client
.new(host: "localhost", port: 4455, password: "strongpassword")
.run do |client|
# Toggle the mute state of your Mic input and print its new mute state
client.toggle_input_mute(INPUT)
resp = client.get_input_mute(INPUT)
puts "Input '#{INPUT}' was set to #{resp.input_muted}"
end
# Toggle the mute state of your Mic input
client.toggle_input_mute("Mic/Aux")
end
end
end
@ -119,9 +115,7 @@ 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.
- The request name and code are retrievable through the following attributes:
- `req_name`
- `code`
- The request name and code are retrievable through attributes {OBSWSRequestError}.req_name and {OBSWSRequestError}.code
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

@ -3,6 +3,8 @@ require "yaml"
class Main
attr_reader :running
def initialize(**kwargs)
@r_client = OBSWS::Requests::Client.new(**kwargs)
@e_client = OBSWS::Events::Client.new(**kwargs)
@ -13,7 +15,7 @@ class Main
end
def run
sleep(0.1) while @running
sleep(0.1) while running
end
def infostring

View File

@ -1,10 +1,9 @@
require "digest/sha2"
require "json"
require "logger"
require "securerandom"
require "socket"
require "waitutil"
require "socket"
require "websocket/driver"
require "logger"
require_relative "obsws/logger"
require_relative "obsws/driver"

View File

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

View File

@ -71,11 +71,6 @@ module OBSWS
def initialize(**kwargs)
kwargs[:subs] ||= SUBS::LOW_VOLUME
@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")
rescue Errno::ECONNREFUSED, WaitUtil::TimeoutError => e
msg = "#{e.class.name}: #{e.message}"

View File

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

View File

@ -11,7 +11,7 @@ module OBSWS
end
def patch
8
4
end
def to_a

16
main.rb
View File

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

View File

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

View File

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

View File

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