mirror of
https://github.com/onyx-and-iris/obsws-ruby.git
synced 2025-01-18 13:30:48 +00:00
Socket class and driver methods
moved into Driver module patch bump
This commit is contained in:
parent
88b2eabc0c
commit
2883fd42cc
@ -1,29 +1,16 @@
|
|||||||
require "socket"
|
|
||||||
require "websocket/driver"
|
|
||||||
require "digest/sha2"
|
require "digest/sha2"
|
||||||
require "json"
|
require "json"
|
||||||
require "waitutil"
|
require "waitutil"
|
||||||
|
|
||||||
require_relative "mixin"
|
require_relative "driver"
|
||||||
require_relative "error"
|
require_relative "error"
|
||||||
require_relative "logger"
|
require_relative "logger"
|
||||||
|
require_relative "mixin"
|
||||||
|
|
||||||
module OBSWS
|
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
|
class Base
|
||||||
include Logging
|
include Logging
|
||||||
|
include Driver
|
||||||
include Mixin::OPCodes
|
include Mixin::OPCodes
|
||||||
|
|
||||||
attr_reader :closed
|
attr_reader :closed
|
||||||
@ -34,20 +21,7 @@ module OBSWS
|
|||||||
port = kwargs[:port] || 4455
|
port = kwargs[:port] || 4455
|
||||||
@password = kwargs[:password] || ""
|
@password = kwargs[:password] || ""
|
||||||
@subs = kwargs[:subs] || 0
|
@subs = kwargs[:subs] || 0
|
||||||
|
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
|
|
||||||
start_driver
|
start_driver
|
||||||
WaitUtil.wait_for_condition(
|
WaitUtil.wait_for_condition(
|
||||||
"successful identification",
|
"successful identification",
|
||||||
@ -56,22 +30,6 @@ module OBSWS
|
|||||||
) { @identified }
|
) { @identified }
|
||||||
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
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def auth_token(salt:, challenge:)
|
def auth_token(salt:, challenge:)
|
||||||
|
51
lib/obsws/driver.rb
Normal file
51
lib/obsws/driver.rb
Normal file
@ -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
|
@ -11,7 +11,7 @@ module OBSWS
|
|||||||
end
|
end
|
||||||
|
|
||||||
def patch
|
def patch
|
||||||
0
|
1
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_a
|
def to_a
|
||||||
|
Loading…
Reference in New Issue
Block a user