class WSDirector::Client

WebSocket client

Constants

WAIT_WHEN_EXPECTING_EVENT

Attributes

id[R]
ws[R]

Public Class Methods

new(ignore: nil) click to toggle source

Create new WebSocket client and connect to WSDirector ws URL.

Optionally provide an ignore pattern (to ignore incoming message, for example, pings)

# File lib/wsdirector/client.rb, line 18
def initialize(ignore: nil)
  @ignore = ignore
  has_messages = @has_messages = Concurrent::Semaphore.new(0)
  messages = @messages = Queue.new
  path = WSDirector.config.ws_url
  open = Concurrent::Promise.new
  client = self

  @id = SecureRandom.hex(6)
  @ws = WebSocket::Client::Simple.connect(path) do |ws|
    ws.on(:open) do |_event|
      open.set(true)
    end

    ws.on :message do |msg|
      data = msg.data
      next if data.empty?
      next if client.ignored?(data)
      messages << data
      has_messages.release
    end

    ws.on :error do |e|
      messages << Error.new("WebSocket Error #{e.inspect} #{e.backtrace}")
    end
  end

  open.wait!(WAIT_WHEN_EXPECTING_EVENT)
rescue Errno::ECONNREFUSED
  raise Error, "Failed to connect to #{path}"
end

Public Instance Methods

ignored?(msg) click to toggle source
# File lib/wsdirector/client.rb, line 61
def ignored?(msg)
  return false unless @ignore
  @ignore.any? { |pattern| msg =~ pattern }
end
receive(timeout = WAIT_WHEN_EXPECTING_EVENT) click to toggle source
# File lib/wsdirector/client.rb, line 50
def receive(timeout = WAIT_WHEN_EXPECTING_EVENT)
  @has_messages.try_acquire(1, timeout)
  msg = @messages.pop(true)
  raise msg if msg.is_a?(Exception)
  msg
end
send(msg) click to toggle source
# File lib/wsdirector/client.rb, line 57
def send(msg)
  @ws.send(msg)
end