Class: WSDirector::Protocols::Phoenix
- Defined in:
- lib/wsdirector/protocols/phoenix.rb
Overview
Phoenix Channels protocol See github.com/phoenixframework/phoenix/blob/master/lib/phoenix/socket/serializers/v2_json_serializer.ex
Constant Summary
Constants included from Utils
Instance Attribute Summary collapse
-
#join_refs_counter ⇒ Object
Returns the value of attribute join_refs_counter.
-
#refs_counter ⇒ Object
Returns the value of attribute refs_counter.
-
#topics_to_join_ref ⇒ Object
readonly
Returns the value of attribute topics_to_join_ref.
Attributes included from Utils
Instance Method Summary collapse
- #init_client(**options) ⇒ Object
-
#initialize ⇒ Phoenix
constructor
A new instance of Phoenix.
- #join(step) ⇒ Object
- #leave(step) ⇒ Object
- #receive(step) ⇒ Object
- #send(step) ⇒ Object
Methods inherited from Base
#debug, #handle_step, #receive_all, #sleep, #to_proc, #wait_all
Methods included from Utils
Constructor Details
#initialize ⇒ Phoenix
Returns a new instance of Phoenix.
11 12 13 14 15 16 17 |
# File 'lib/wsdirector/protocols/phoenix.rb', line 11 def initialize(...) super @refs_counter = 3 @join_refs_counter = 3 @topics_to_join_ref = {} end |
Instance Attribute Details
#join_refs_counter ⇒ Object
Returns the value of attribute join_refs_counter.
9 10 11 |
# File 'lib/wsdirector/protocols/phoenix.rb', line 9 def join_refs_counter @join_refs_counter end |
#refs_counter ⇒ Object
Returns the value of attribute refs_counter.
9 10 11 |
# File 'lib/wsdirector/protocols/phoenix.rb', line 9 def refs_counter @refs_counter end |
#topics_to_join_ref ⇒ Object (readonly)
Returns the value of attribute topics_to_join_ref.
8 9 10 |
# File 'lib/wsdirector/protocols/phoenix.rb', line 8 def topics_to_join_ref @topics_to_join_ref end |
Instance Method Details
#init_client(**options) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/wsdirector/protocols/phoenix.rb', line 19 def init_client(**) [:query] ||= {} # Make sure we use the v2 of the protocol [:query][:vsn] = "2.0.0" super end |
#join(step) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/wsdirector/protocols/phoenix.rb', line 27 def join(step) topic = step.fetch("topic") join_ref = join_refs_counter self.join_refs_counter += 1 ref = refs_counter self.refs_counter += 1 log { "Joining #{topic} (##{ref})" } cmd = new_command(:phx_join, topic, join_ref:, ref:) client.send(cmd.to_json) receive({"data>" => new_command(:phx_reply, topic, join_ref:, ref:, payload: {"status" => "ok"})}) log(:done) { "Joined #{topic} (##{ref})" } end |
#leave(step) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/wsdirector/protocols/phoenix.rb', line 47 def leave(step) topic = step.fetch("topic") join_ref = topics_to_join_ref.fetch(topic) ref = refs_counter self.refs_counter += 1 cmd = new_command(:phx_leave, topic, join_ref:, ref:) client.send(cmd.to_json) receive({"data>" => new_command(:phx_reply, topic, join_ref:, ref:, payload: {"status" => "ok"})}) log(nil) { "Left #{topic} (##{join_ref})" } end |
#receive(step) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/wsdirector/protocols/phoenix.rb', line 75 def receive(step) return super unless step.key?("topic") topic = step.fetch("topic") event = step.fetch("event") key = step.key?("data") ? "data" : "data>" payload = step.fetch(key) cmd = new_command(event, topic, payload:) super({key => cmd}) end |
#send(step) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/wsdirector/protocols/phoenix.rb', line 62 def send(step) return super unless step.key?("topic") ref = refs_counter self.refs_counter += 1 topic = step.fetch("topic") event = step.fetch("event") payload = step["data"] super({"data" => new_command(event, topic, payload:, ref:).to_json}) end |