Identified class added. tracks identified state

This commit is contained in:
onyx-and-iris 2023-08-11 22:05:01 +01:00
parent 70b60b3cac
commit 3ea21cd371
2 changed files with 28 additions and 4 deletions

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)
@ -13,11 +30,12 @@ module OBSWS
@password = kwargs[:password] || "" @password = kwargs[:password] || ""
@subs = kwargs[:subs] || 0 @subs = kwargs[:subs] || 0
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: kwargs[:connect_timeout] || 3
) { @identified } ) { @identified.state != :pending }
end end
private private
@ -38,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)
@ -51,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

@ -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}")