From 3ea21cd371af6e8c9871ae920e2b3b1e0ec2d517 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Fri, 11 Aug 2023 22:05:01 +0100 Subject: [PATCH] Identified class added. tracks identified state --- lib/obsws/base.rb | 27 +++++++++++++++++++++++---- lib/obsws/req.rb | 5 +++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/lib/obsws/base.rb b/lib/obsws/base.rb index 742d18d..64cc542 100644 --- a/lib/obsws/base.rb +++ b/lib/obsws/base.rb @@ -1,10 +1,27 @@ 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 + attr_reader :closed, :identified attr_writer :updater def initialize(**kwargs) @@ -13,11 +30,12 @@ module OBSWS @password = kwargs[:password] || "" @subs = kwargs[:subs] || 0 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 } + ) { @identified.state != :pending } end private @@ -38,7 +56,8 @@ module OBSWS } if auth if @password.empty? - raise OBSWSError("auth enabled but no password provided") + @identified.state = :passwordless + return end logger.info("initiating authentication") payload[:d][:authentication] = auth_token(**auth) @@ -51,7 +70,7 @@ module OBSWS when Mixin::OPCodes::HELLO identify(data[:d][:authentication]) when Mixin::OPCodes::IDENTIFIED - @identified = true + @identified.state = :identified when Mixin::OPCodes::EVENT, Mixin::OPCodes::REQUESTRESPONSE @updater.call(data[:op], data[:d]) end diff --git a/lib/obsws/req.rb b/lib/obsws/req.rb index b26f473..d684c55 100644 --- a/lib/obsws/req.rb +++ b/lib/obsws/req.rb @@ -7,6 +7,11 @@ 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}")