Class: Anyt::Client
- Inherits:
-
Object
- Object
- Anyt::Client
- Defined in:
- lib/anyt/client.rb
Overview
Synchronous websocket client Based on github.com/rails/rails/blob/v5.0.1/actioncable/test/client_test.rb
Defined Under Namespace
Classes: DisconnectedError, Error, TimeoutError
Constant Summary collapse
- WAIT_WHEN_EXPECTING_EVENT =
5
- WAIT_WHEN_NOT_EXPECTING_EVENT =
0.5
Instance Attribute Summary collapse
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #close(allow_messages: false) ⇒ Object
- #closed? ⇒ Boolean
-
#initialize(ignore: [], url: Anyt.config.target_url, qs: "", cookies: "", headers: {}, protocol: "actioncable-v1-json", timeout_multiplier: Anyt.config.timeout_multiplier, logger: AnyCable.logger) ⇒ Client
constructor
A new instance of Client.
-
#receive(timeout: WAIT_WHEN_EXPECTING_EVENT) ⇒ Object
rubocop: enable Metrics/BlockLength rubocop: enable Metrics/AbcSize rubocop: enable Metrics/MethodLength.
- #send(message) ⇒ Object
- #wait_for_close ⇒ Object
Constructor Details
#initialize(ignore: [], url: Anyt.config.target_url, qs: "", cookies: "", headers: {}, protocol: "actioncable-v1-json", timeout_multiplier: Anyt.config.timeout_multiplier, logger: AnyCable.logger) ⇒ Client
Returns a new instance of Client.
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/anyt/client.rb', line 37 def initialize( ignore: [], url: Anyt.config.target_url, qs: "", cookies: "", headers: {}, protocol: "actioncable-v1-json", timeout_multiplier: Anyt.config.timeout_multiplier, logger: AnyCable.logger ) @logger = logger = @ignore_message_types = ignore = @messages = Queue.new closed = @closed = Concurrent::Event.new = @has_messages = Concurrent::Semaphore.new(0) @timeout_multiplier = timeout_multiplier headers = headers.merge("cookie" => ) headers["Sec-WebSocket-Protocol"] = protocol open = Concurrent::Promise.new @url = url if !qs.empty? @url += @url.include?("?") ? "&" : "?" @url += qs end @ws = WebSocket::Client::Simple.connect( @url, headers: headers ) do |ws| ws.on(:error) do |event| event = RuntimeError.new(event.) unless event.is_a?(Exception) if open.pending? open.fail(event) else << event .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 << DisconnectedError.new(event) .release closed.set else = JSON.parse(event.data) next if .include?(["type"]) logger.debug "Message received: #{}" << .release end end ws.on(:close) do |_event| closed.set end end open.wait!(WAIT_WHEN_EXPECTING_EVENT * @timeout_multiplier) end |
Instance Attribute Details
#url ⇒ Object (readonly)
Returns the value of attribute url.
35 36 37 |
# File 'lib/anyt/client.rb', line 35 def url @url end |
Instance Method Details
#close(allow_messages: false) ⇒ Object
130 131 132 133 134 135 136 137 |
# File 'lib/anyt/client.rb', line 130 def close(allow_messages: false) sleep WAIT_WHEN_NOT_EXPECTING_EVENT * @timeout_multiplier raise "#{@messages.size} messages unprocessed" unless || @messages.empty? @ws.close wait_for_close end |
#closed? ⇒ Boolean
143 144 145 |
# File 'lib/anyt/client.rb', line 143 def closed? @closed.set? end |
#receive(timeout: WAIT_WHEN_EXPECTING_EVENT) ⇒ Object
rubocop: enable Metrics/BlockLength rubocop: enable Metrics/AbcSize rubocop: enable Metrics/MethodLength
112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/anyt/client.rb', line 112 def receive(timeout: WAIT_WHEN_EXPECTING_EVENT) timeout *= @timeout_multiplier unless @has_messages.try_acquire(1, timeout) raise DisconnectedError if closed? raise TimeoutError, "Timed out to receive message" end msg = @messages.pop(true) raise msg if msg.is_a?(Exception) || msg.is_a?(Error) msg end |
#send(message) ⇒ Object
126 127 128 |
# File 'lib/anyt/client.rb', line 126 def send() @ws.send(JSON.generate()) end |
#wait_for_close ⇒ Object
139 140 141 |
# File 'lib/anyt/client.rb', line 139 def wait_for_close @closed.wait(WAIT_WHEN_EXPECTING_EVENT * @timeout_multiplier) end |