From 2883fd42cca76e59226b6c461627ec10ea241c1e Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Wed, 26 Jul 2023 16:38:36 +0100 Subject: [PATCH] Socket class and driver methods moved into Driver module patch bump --- lib/obsws/base.rb | 50 ++++--------------------------------------- lib/obsws/driver.rb | 51 ++++++++++++++++++++++++++++++++++++++++++++ lib/obsws/version.rb | 2 +- 3 files changed, 56 insertions(+), 47 deletions(-) create mode 100644 lib/obsws/driver.rb diff --git a/lib/obsws/base.rb b/lib/obsws/base.rb index 7349919..b7f3cb4 100644 --- a/lib/obsws/base.rb +++ b/lib/obsws/base.rb @@ -1,29 +1,16 @@ -require "socket" -require "websocket/driver" require "digest/sha2" require "json" require "waitutil" -require_relative "mixin" +require_relative "driver" require_relative "error" require_relative "logger" +require_relative "mixin" module OBSWS - class Socket - attr_reader :url - - def initialize(url, socket) - @url = url - @socket = socket - end - - def write(s) - @socket.write(s) - end - end - class Base include Logging + include Driver include Mixin::OPCodes attr_reader :closed @@ -34,20 +21,7 @@ module OBSWS port = kwargs[:port] || 4455 @password = kwargs[:password] || "" @subs = kwargs[:subs] || 0 - - @socket = TCPSocket.new(host, port) - @driver = - WebSocket::Driver.client(Socket.new("ws://#{host}:#{port}", @socket)) - @driver.on :open do |msg| - logger.debug("driver socket open") - end - @driver.on :close do |msg| - logger.debug("driver socket closed") - @closed = true - end - @driver.on :message do |msg| - msg_handler(JSON.parse(msg.data, symbolize_names: true)) - end + setup_driver(host, port) start_driver WaitUtil.wait_for_condition( "successful identification", @@ -56,22 +30,6 @@ module OBSWS ) { @identified } end - private def start_driver - Thread.new do - @driver.start - - loop do - @driver.parse(@socket.readpartial(4096)) - rescue EOFError - break - end - end - end - - public def stop_driver - @driver.close - end - private def auth_token(salt:, challenge:) diff --git a/lib/obsws/driver.rb b/lib/obsws/driver.rb new file mode 100644 index 0000000..98d05d1 --- /dev/null +++ b/lib/obsws/driver.rb @@ -0,0 +1,51 @@ +require "socket" +require "websocket/driver" + +module OBSWS + module Driver + class Socket + attr_reader :url + + def initialize(url, socket) + @url = url + @socket = socket + end + + def write(s) + @socket.write(s) + end + end + + def setup_driver(host, port) + @socket = TCPSocket.new(host, port) + @driver = + WebSocket::Driver.client(Socket.new("ws://#{host}:#{port}", @socket)) + @driver.on :open do |msg| + logger.debug("driver socket open") + end + @driver.on :close do |msg| + logger.debug("driver socket closed") + @closed = true + end + @driver.on :message do |msg| + msg_handler(JSON.parse(msg.data, symbolize_names: true)) + end + end + + private def start_driver + Thread.new do + @driver.start + + loop do + @driver.parse(@socket.readpartial(4096)) + rescue EOFError + break + end + end + end + + public def stop_driver + @driver.close + end + end +end diff --git a/lib/obsws/version.rb b/lib/obsws/version.rb index 15a2cbb..8ad7ab2 100644 --- a/lib/obsws/version.rb +++ b/lib/obsws/version.rb @@ -11,7 +11,7 @@ module OBSWS end def patch - 0 + 1 end def to_a