Class: Anyt::Client

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

Overview

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

Instance Method Summary collapse

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_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)
  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.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
        messages << DisconnectedError.new(event)
        has_messages.release
        closed.set
      else
        message = JSON.parse(event.data)

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

        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 Attribute Details

#urlObject (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 allow_messages || @messages.empty?

  @ws.close
  wait_for_close
end

#closed?Boolean

Returns:

  • (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(message)
  @ws.send(JSON.generate(message))
end

#wait_for_closeObject



139
140
141
# File 'lib/anyt/client.rb', line 139

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