diff --git a/lib/obsws.rb b/lib/obsws.rb index 52e8082..2e517d7 100644 --- a/lib/obsws.rb +++ b/lib/obsws.rb @@ -1,9 +1,10 @@ require "digest/sha2" require "json" -require "waitutil" -require "socket" -require "websocket/driver" require "logger" +require "securerandom" +require "socket" +require "waitutil" +require "websocket/driver" require_relative "obsws/logger" require_relative "obsws/driver" diff --git a/lib/obsws/req.rb b/lib/obsws/req.rb index add98ec..b03a0f0 100644 --- a/lib/obsws/req.rb +++ b/lib/obsws/req.rb @@ -35,20 +35,19 @@ module OBSWS end def call(req, data = nil) - id = rand(1..1000) - @base_client.req(id, req, data) + uuid = SecureRandom.uuid + @base_client.req(uuid, req, data) WaitUtil.wait_for_condition( "reponse id to match request id", delay_sec: 0.001, timeout_sec: 3 - ) { @response[:requestId] == id } + ) { @response[:requestId] == uuid } unless @response[:requestStatus][:result] - raise OBSWSRequestError.new(@response[:requestType], @response[:requestStatus][:code], @response[:requestStatus][:comment]) + OBSWSRequestError.new(@response[:requestType], @response[:requestStatus][:code], @response[:requestStatus][:comment]) => e + logger.error(["#{e.class.name}: #{e.message}", *e.backtrace].join("\n")) + raise e end @response[:responseData] - rescue OBSWSRequestError => e - logger.error(["#{e.class.name}: #{e.message}", *e.backtrace].join("\n")) - raise rescue WaitUtil::TimeoutError => e logger.error(["#{e.class.name}: #{e.message}", *e.backtrace].join("\n")) raise OBSWSError.new([e.message, *e.backtrace].join("\n"))