Compare commits

...

5 Commits

8 changed files with 32 additions and 33 deletions

View File

@ -1,8 +1,7 @@
PATH PATH
remote: . remote: .
specs: specs:
obsws (0.2.0) obsws (0.2.1)
observer (~> 0.1.1)
waitutil (~> 0.2.1) waitutil (~> 0.2.1)
websocket-driver (~> 0.7.5) websocket-driver (~> 0.7.5)
@ -14,7 +13,6 @@ GEM
language_server-protocol (3.17.0.3) language_server-protocol (3.17.0.3)
lint_roller (1.1.0) lint_roller (1.1.0)
minitest (5.16.3) minitest (5.16.3)
observer (0.1.1)
parallel (1.23.0) parallel (1.23.0)
parser (3.2.2.3) parser (3.2.2.3)
ast (~> 2.4.1) ast (~> 2.4.1)

View File

@ -11,7 +11,7 @@ class Main
@e_client = OBSWS::Events::Client.new(**kwargs) @e_client = OBSWS::Events::Client.new(**kwargs)
@e_client.add_observer(self) @e_client.add_observer(self)
puts info.join("\n") puts infostring
@running = true @running = true
end end
@ -19,14 +19,14 @@ class Main
sleep(0.1) while running sleep(0.1) while running
end end
def info 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:", "With websocket version:",
resp.obs_web_socket_version resp.obs_web_socket_version
] ].join("\n")
end end
def on_current_program_scene_changed(data) def on_current_program_scene_changed(data)

View File

@ -2,7 +2,6 @@ require "socket"
require "websocket/driver" require "websocket/driver"
require "digest/sha2" require "digest/sha2"
require "json" require "json"
require "observer"
require "waitutil" require "waitutil"
require_relative "mixin" require_relative "mixin"
@ -23,10 +22,10 @@ module OBSWS
end end
class Base class Base
include Observable
include Mixin::OPCodes include Mixin::OPCodes
attr_reader :id, :driver, :closed attr_reader :closed
attr_writer :updater
def initialize(**kwargs) def initialize(**kwargs)
host = kwargs[:host] || "localhost" host = kwargs[:host] || "localhost"
@ -56,7 +55,7 @@ module OBSWS
) { @identified } ) { @identified }
end end
def start_driver private def start_driver
Thread.new do Thread.new do
@driver.start @driver.start
@ -68,6 +67,12 @@ module OBSWS
end end
end end
public def stop_driver
@driver.close
end
private
def auth_token(salt:, challenge:) def auth_token(salt:, challenge:)
Digest::SHA256.base64digest( Digest::SHA256.base64digest(
Digest::SHA256.base64digest(@password + salt) + challenge Digest::SHA256.base64digest(@password + salt) + challenge
@ -99,12 +104,11 @@ module OBSWS
when Mixin::OPCodes::IDENTIFIED when Mixin::OPCodes::IDENTIFIED
@identified = true @identified = true
when Mixin::OPCodes::EVENT, Mixin::OPCodes::REQUESTRESPONSE when Mixin::OPCodes::EVENT, Mixin::OPCodes::REQUESTRESPONSE
changed @updater.call(data[:op], data[:d])
notify_observers(data[:op], data[:d])
end end
end end
def req(id, type_, data = nil) public def req(id, type_, data = nil)
payload = { payload = {
op: Mixin::OPCodes::REQUEST, op: Mixin::OPCodes::REQUEST,
d: { d: {

View File

@ -70,20 +70,18 @@ module OBSWS
kwargs[:subs] ||= SUBS::LOW_VOLUME kwargs[:subs] ||= SUBS::LOW_VOLUME
@base_client = Base.new(**kwargs) @base_client = Base.new(**kwargs)
LOGGER.info("#{self} succesfully identified with server") LOGGER.info("#{self} succesfully identified with server")
@base_client.add_observer(self) @base_client.updater = ->(op_code, data) {
if op_code == Mixin::OPCodes::EVENT
event = data[:eventType]
data = data.fetch(:eventData, {})
notify_observers(event, Mixin::Data.new(data, data.keys))
end
}
end end
def to_s def to_s
self.class.name.split("::").last(2).join("::") self.class.name.split("::").last(2).join("::")
end end
def update(op_code, data)
if op_code == Mixin::OPCodes::EVENT
event = data[:eventType]
data = data.key?(:eventData) ? data[:eventData] : {}
notify_observers(event, Mixin::Data.new(data, data.keys))
end
end
end end
end end
end end

View File

@ -18,7 +18,7 @@ module OBSWS
def initialize(resp, fields) def initialize(resp, fields)
@resp = resp @resp = resp
@fields = fields @fields = fields
self.make_field_methods *fields make_field_methods(*fields)
end end
def empty? = @fields.empty? def empty? = @fields.empty?
@ -33,9 +33,11 @@ module OBSWS
end end
module TearDown module TearDown
def close def stop_driver
@base_client.driver.close @base_client.stop_driver
end end
alias_method :close, :stop_driver
end end
module OPCodes module OPCodes

View File

@ -15,7 +15,9 @@ module OBSWS
def initialize(**kwargs) def initialize(**kwargs)
@base_client = Base.new(**kwargs) @base_client = Base.new(**kwargs)
LOGGER.info("#{self} succesfully identified with server") LOGGER.info("#{self} succesfully identified with server")
@base_client.add_observer(self) @base_client.updater = ->(op_code, data) {
@response = data if op_code == Mixin::OPCodes::REQUESTRESPONSE
}
@response = {requestId: 0} @response = {requestId: 0}
end end
@ -26,7 +28,7 @@ module OBSWS
def run def run
yield(self) yield(self)
ensure ensure
close stop_driver
WaitUtil.wait_for_condition( WaitUtil.wait_for_condition(
"driver to close", "driver to close",
delay_sec: 0.01, delay_sec: 0.01,
@ -34,10 +36,6 @@ module OBSWS
) { @base_client.closed } ) { @base_client.closed }
end end
def update(op_code, data)
@response = data if op_code == Mixin::OPCodes::REQUESTRESPONSE
end
def call(req, data = nil) def call(req, data = nil)
id = rand(1..1000) id = rand(1..1000)
@base_client.req(id, req, data) @base_client.req(id, req, data)

View File

@ -11,7 +11,7 @@ module OBSWS
end end
def patch def patch
0 1
end end
def to_a def to_a

View File

@ -13,7 +13,6 @@ Gem::Specification.new do |spec|
spec.extra_rdoc_files = Dir["README.md", "CHANGELOG.md", "LICENSE"] spec.extra_rdoc_files = Dir["README.md", "CHANGELOG.md", "LICENSE"]
spec.homepage = "https://rubygems.org/gems/obsws" spec.homepage = "https://rubygems.org/gems/obsws"
spec.license = "MIT" spec.license = "MIT"
spec.add_runtime_dependency "observer", "~> 0.1.1"
spec.add_runtime_dependency "websocket-driver", "~> 0.7.5" spec.add_runtime_dependency "websocket-driver", "~> 0.7.5"
spec.add_runtime_dependency "waitutil", "~> 0.2.1" spec.add_runtime_dependency "waitutil", "~> 0.2.1"
spec.add_development_dependency "standard", "~> 1.30" spec.add_development_dependency "standard", "~> 1.30"