mirror of
https://github.com/onyx-and-iris/obsws-ruby.git
synced 2024-11-15 18:50:47 +00:00
52 lines
1.0 KiB
Ruby
52 lines
1.0 KiB
Ruby
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
|