2022-10-22 22:30:40 +01:00
|
|
|
require "socket"
|
|
|
|
require "websocket/driver"
|
|
|
|
require "digest/sha2"
|
|
|
|
require "json"
|
|
|
|
require "observer"
|
|
|
|
require "waitutil"
|
|
|
|
|
|
|
|
require_relative "mixin"
|
|
|
|
require_relative "error"
|
|
|
|
|
|
|
|
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 Observable
|
|
|
|
include Mixin::OPCodes
|
|
|
|
|
|
|
|
attr_reader :id, :driver, :closed
|
|
|
|
|
|
|
|
def initialize(**kwargs)
|
|
|
|
host = kwargs[:host] || "localhost"
|
|
|
|
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|
|
2022-11-25 17:57:56 +00:00
|
|
|
LOGGER.debug("received: #{msg.data}")
|
2022-10-22 22:30:40 +01:00
|
|
|
msg_handler(JSON.parse(msg.data, symbolize_names: true))
|
|
|
|
end
|
2022-10-23 04:39:22 +01:00
|
|
|
start_driver
|
2022-10-22 22:30:40 +01:00
|
|
|
WaitUtil.wait_for_condition(
|
2022-11-25 17:57:56 +00:00
|
|
|
"successful identification",
|
2022-10-22 22:30:40 +01:00
|
|
|
delay_sec: 0.01,
|
2022-10-23 04:39:22 +01:00
|
|
|
timeout_sec: 3
|
2022-10-23 23:33:30 +01:00
|
|
|
) { @identified }
|
2022-10-22 22:30:40 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def start_driver
|
2022-10-23 04:39:22 +01:00
|
|
|
Thread.new do
|
|
|
|
@driver.start
|
2022-10-22 22:30:40 +01:00
|
|
|
|
2022-10-23 04:39:22 +01:00
|
|
|
loop do
|
|
|
|
@driver.parse(@socket.readpartial(4096))
|
|
|
|
rescue EOFError
|
|
|
|
break
|
|
|
|
end
|
2022-10-22 22:30:40 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def auth_token(salt:, challenge:)
|
|
|
|
Digest::SHA256.base64digest(
|
|
|
|
Digest::SHA256.base64digest(@password + salt) + challenge
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-10-25 00:03:43 +01:00
|
|
|
def identify(auth)
|
2022-11-25 18:05:10 +00:00
|
|
|
payload = {
|
|
|
|
op: Mixin::OPCodes::IDENTIFY,
|
|
|
|
d: {
|
|
|
|
rpcVersion: 1,
|
|
|
|
eventSubscriptions: @subs
|
|
|
|
}
|
|
|
|
}
|
2022-11-25 17:57:56 +00:00
|
|
|
if auth
|
|
|
|
if @password.empty?
|
|
|
|
raise OBSWSError("auth enabled but no password provided")
|
|
|
|
end
|
|
|
|
LOGGER.info("initiating authentication")
|
|
|
|
payload[:d][:authentication] = auth_token(**auth)
|
|
|
|
end
|
2022-10-22 22:30:40 +01:00
|
|
|
@driver.text(JSON.generate(payload))
|
|
|
|
end
|
|
|
|
|
|
|
|
def msg_handler(data)
|
2022-10-23 23:33:30 +01:00
|
|
|
case data[:op]
|
2022-10-22 22:30:40 +01:00
|
|
|
when Mixin::OPCodes::HELLO
|
2022-10-25 00:03:43 +01:00
|
|
|
identify(data[:d][:authentication])
|
2022-10-22 22:30:40 +01:00
|
|
|
when Mixin::OPCodes::IDENTIFIED
|
2022-10-23 23:33:30 +01:00
|
|
|
@identified = true
|
2022-10-22 22:30:40 +01:00
|
|
|
when Mixin::OPCodes::EVENT, Mixin::OPCodes::REQUESTRESPONSE
|
|
|
|
changed
|
2022-10-23 23:33:30 +01:00
|
|
|
notify_observers(data[:op], data[:d])
|
2022-10-22 22:30:40 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def req(id, type_, data = nil)
|
|
|
|
payload = {
|
|
|
|
op: Mixin::OPCodes::REQUEST,
|
|
|
|
d: {
|
|
|
|
requestType: type_,
|
|
|
|
requestId: id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
payload[:d][:requestData] = data if data
|
2022-11-25 17:57:56 +00:00
|
|
|
LOGGER.debug("sending request: #{payload}")
|
2023-07-21 06:04:09 +01:00
|
|
|
@driver.text(JSON.generate(payload))
|
2022-10-22 22:30:40 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|