2023-07-26 16:38:36 +01:00
|
|
|
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
|
|
|
|
|
2023-07-26 16:55:59 +01:00
|
|
|
module Director
|
|
|
|
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
|
2023-07-26 16:38:36 +01:00
|
|
|
end
|
|
|
|
|
2023-07-26 16:55:59 +01:00
|
|
|
private def start_driver
|
|
|
|
Thread.new do
|
|
|
|
@driver.start
|
2023-07-26 16:38:36 +01:00
|
|
|
|
2023-07-26 16:55:59 +01:00
|
|
|
loop do
|
|
|
|
@driver.parse(@socket.readpartial(4096))
|
|
|
|
rescue EOFError
|
|
|
|
break
|
|
|
|
end
|
2023-07-26 16:38:36 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-07-26 16:55:59 +01:00
|
|
|
public def stop_driver
|
|
|
|
@driver.close
|
|
|
|
end
|
2023-07-26 16:38:36 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|