Class: Anyt::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/anyt/client.rb

Overview

Defined Under Namespace

Classes: TimeoutError

Constant Summary collapse

WAIT_WHEN_EXPECTING_EVENT =
5
WAIT_WHEN_NOT_EXPECTING_EVENT =
0.5

Instance Method Summary collapse

Constructor Details

#initialize(ignore: [], url: Anyt.config.target_url, qs: "", cookies: "", headers: {}, timeout_multiplier: Anyt.config.timeout_multiplier) ⇒ Client

rubocop: disable Metrics/AbcSize rubocop: disable Metrics/MethodLength rubocop: disable Metrics/BlockLength



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/anyt/client.rb', line 18

def initialize(
  ignore: [], url: Anyt.config.target_url, qs: "",
  cookies: "", headers: {},
  timeout_multiplier: Anyt.config.timeout_multiplier
)
  ignore_message_types = @ignore_message_types = ignore
  messages = @messages = Queue.new
  closed = @closed = Concurrent::Event.new
  has_messages = @has_messages = Concurrent::Semaphore.new(0)

  @timeout_multiplier = timeout_multiplier

  headers = headers.merge("cookie" => cookies)

  open = Concurrent::Promise.new

  @ws = WebSocket::Client::Simple.connect(
    url + "?#{qs}",
    headers: headers
  ) do |ws|
    ws.on(:error) do |event|
      event = RuntimeError.new(event.message) unless event.is_a?(Exception)

      if open.pending?
        open.fail(event)
      else
        messages << event
        has_messages.release
      end
    end

    ws.on(:open) do |_event|
      open.set(true)
    end

    ws.on(:message) do |event|
      next if event.type == :ping
      if event.type == :close
        closed.set
      else
        message = JSON.parse(event.data)

        next if ignore_message_types.include?(message["type"])

        AnyCable.logger.debug "Message received: #{message}"

        messages << message
        has_messages.release
      end
    end

    ws.on(:close) do |_event|
      closed.set
    end
  end

  open.wait!(WAIT_WHEN_EXPECTING_EVENT * @timeout_multiplier)
end

Instance Method Details

#close(allow_messages: false) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/anyt/client.rb', line 96

def close(allow_messages: false)
  sleep WAIT_WHEN_NOT_EXPECTING_EVENT * @timeout_multiplier

  raise "#{@messages.size} messages unprocessed" unless allow_messages || @messages.empty?

  @ws.close
  wait_for_close
end

#closed?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/anyt/client.rb', line 109

def closed?
  @closed.set?
end

#receive(timeout: WAIT_WHEN_EXPECTING_EVENT) ⇒ Object

rubocop: enable Metrics/BlockLength rubocop: enable Metrics/AbcSize rubocop: enable Metrics/MethodLength

Raises:



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/anyt/client.rb', line 80

def receive(timeout: WAIT_WHEN_EXPECTING_EVENT)
  timeout *= @timeout_multiplier

  raise TimeoutError, "Timed out to receive message" unless
    @has_messages.try_acquire(1, timeout)

  msg = @messages.pop(true)
  raise msg if msg.is_a?(Exception)

  msg
end

#send(message) ⇒ Object



92
93
94
# File 'lib/anyt/client.rb', line 92

def send(message)
  @ws.send(JSON.generate(message))
end

#wait_for_closeObject



105
106
107
# File 'lib/anyt/client.rb', line 105

def wait_for_close
  @closed.wait(WAIT_WHEN_EXPECTING_EVENT * @timeout_multiplier)
end