mirror of
https://github.com/onyx-and-iris/obsws-ruby.git
synced 2025-04-19 10:53:45 +01:00
Compare commits
No commits in common. "7e580dc91a39ad186da644fed8adf66a50970efd" and "dc8ac155eced7f7c7b3344c2387813520ade1a6a" have entirely different histories.
7e580dc91a
...
dc8ac155ec
@ -1,7 +1,7 @@
|
|||||||
PATH
|
PATH
|
||||||
remote: .
|
remote: .
|
||||||
specs:
|
specs:
|
||||||
obsws (0.5.8)
|
obsws (0.5.3)
|
||||||
waitutil (~> 0.2.1)
|
waitutil (~> 0.2.1)
|
||||||
websocket-driver (~> 0.7.5)
|
websocket-driver (~> 0.7.5)
|
||||||
|
|
||||||
|
14
README.md
14
README.md
@ -30,17 +30,13 @@ 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 and print its new mute state
|
# Toggle the mute state of your Mic input
|
||||||
client.toggle_input_mute(INPUT)
|
client.toggle_input_mute("Mic/Aux")
|
||||||
resp = client.get_input_mute(INPUT)
|
end
|
||||||
puts "Input '#{INPUT}' was set to #{resp.input_muted}"
|
|
||||||
end
|
|
||||||
end
|
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.
|
If a request fails an `OBSWSRequestError` will be raised with a status code.
|
||||||
|
|
||||||
- The request name and code are retrievable through the following attributes:
|
- The request name and code are retrievable through attributes {OBSWSRequestError}.req_name and {OBSWSRequestError}.code
|
||||||
- `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)
|
||||||
|
|
||||||
|
@ -3,6 +3,8 @@ 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)
|
||||||
@ -13,7 +15,7 @@ class Main
|
|||||||
end
|
end
|
||||||
|
|
||||||
def run
|
def run
|
||||||
sleep(0.1) while @running
|
sleep(0.1) while running
|
||||||
end
|
end
|
||||||
|
|
||||||
def infostring
|
def infostring
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
require "digest/sha2"
|
require "digest/sha2"
|
||||||
require "json"
|
require "json"
|
||||||
require "logger"
|
|
||||||
require "securerandom"
|
|
||||||
require "socket"
|
|
||||||
require "waitutil"
|
require "waitutil"
|
||||||
|
require "socket"
|
||||||
require "websocket/driver"
|
require "websocket/driver"
|
||||||
|
require "logger"
|
||||||
|
|
||||||
require_relative "obsws/logger"
|
require_relative "obsws/logger"
|
||||||
require_relative "obsws/driver"
|
require_relative "obsws/driver"
|
||||||
|
@ -1,27 +1,10 @@
|
|||||||
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, :identified
|
attr_reader :closed
|
||||||
attr_writer :updater
|
attr_writer :updater
|
||||||
|
|
||||||
def initialize(**kwargs)
|
def initialize(**kwargs)
|
||||||
@ -29,13 +12,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
|
||||||
setup_driver(host, port) and start_driver
|
setup_driver(host, port) and start_driver
|
||||||
@identified = Identified.new
|
|
||||||
WaitUtil.wait_for_condition(
|
WaitUtil.wait_for_condition(
|
||||||
"successful identification",
|
"successful identification",
|
||||||
delay_sec: 0.01,
|
delay_sec: 0.01,
|
||||||
timeout_sec: kwargs[:connect_timeout] || 3
|
timeout_sec: @connect_timeout
|
||||||
) { @identified.state != :pending }
|
) { @identified }
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
@ -56,8 +39,7 @@ module OBSWS
|
|||||||
}
|
}
|
||||||
if auth
|
if auth
|
||||||
if @password.empty?
|
if @password.empty?
|
||||||
@identified.state = :passwordless
|
raise OBSWSError("auth enabled but no password provided")
|
||||||
return
|
|
||||||
end
|
end
|
||||||
logger.info("initiating authentication")
|
logger.info("initiating authentication")
|
||||||
payload[:d][:authentication] = auth_token(**auth)
|
payload[:d][:authentication] = auth_token(**auth)
|
||||||
@ -70,7 +52,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.state = :identified
|
@identified = true
|
||||||
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
|
||||||
|
@ -71,11 +71,6 @@ module OBSWS
|
|||||||
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}"
|
||||||
|
@ -7,11 +7,6 @@ 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}")
|
||||||
@ -40,13 +35,13 @@ module OBSWS
|
|||||||
end
|
end
|
||||||
|
|
||||||
def call(req, data = nil)
|
def call(req, data = nil)
|
||||||
uuid = SecureRandom.uuid
|
id = rand(1..1000)
|
||||||
@base_client.req(uuid, req, data)
|
@base_client.req(id, 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] == uuid }
|
) { @response[:requestId] == id }
|
||||||
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
|
||||||
|
@ -11,7 +11,7 @@ module OBSWS
|
|||||||
end
|
end
|
||||||
|
|
||||||
def patch
|
def patch
|
||||||
8
|
4
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_a
|
def to_a
|
||||||
|
16
main.rb
16
main.rb
@ -1,16 +1,14 @@
|
|||||||
require "obsws"
|
require "obsws"
|
||||||
|
|
||||||
class Main
|
class Main
|
||||||
INPUT = "Mic/Aux"
|
|
||||||
|
|
||||||
def run
|
def run
|
||||||
OBSWS::Requests::Client
|
OBSWS::Requests::Client.new(
|
||||||
.new(host: "localhost", port: 4455, password: "strongpassword")
|
host: "localhost",
|
||||||
.run do |client|
|
port: 4455,
|
||||||
# Toggle the mute state of your Mic input and print its new mute state
|
password: "strongpassword"
|
||||||
client.toggle_input_mute(INPUT)
|
).run do |client|
|
||||||
resp = client.get_input_mute(INPUT)
|
# Toggle the mute state of your Mic input
|
||||||
puts "Input '#{INPUT}' was set to #{resp.input_muted}"
|
client.toggle_input_mute("Mic/Aux")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
require_relative "../minitest_helper"
|
require_relative "../minitest_helper"
|
||||||
|
|
||||||
class AttrsTest < Minitest::Test
|
class AttrsTest < OBSWSTest
|
||||||
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 ==
|
||||||
|
@ -2,35 +2,16 @@ 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) do
|
e = assert_raises(OBSWS::OBSWSConnectionError) { OBSWS::Requests::Client.new(host: "localhost", port: 4455, password: "wrongpassword", connect_timeout: 1).new }
|
||||||
OBSWS::Requests::Client
|
assert_equal(e.message, "Timed out waiting for successful identification (1 seconds elapsed)")
|
||||||
.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("ToggleInputMute", e.req_name)
|
assert_equal(e.req_name, "ToggleInputMute")
|
||||||
assert_equal(600, e.code)
|
assert_equal(e.code, 600)
|
||||||
assert_equal("Request ToggleInputMute returned code 600. With message: No source was found by the name of `unknown`.", e.message)
|
assert_equal(e.message, "Request ToggleInputMute returned code 600. With message: No source was found by the name of `unknown`.")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
require_relative "../minitest_helper"
|
require_relative "../minitest_helper"
|
||||||
|
|
||||||
class RequestTest < Minitest::Test
|
class RequestTest < OBSWSTest
|
||||||
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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user