Class: Phoenix::Socket
- Inherits:
-
Object
- Object
- Phoenix::Socket
- Includes:
- MonitorMixin
- Defined in:
- lib/yatapp/socket.rb
Instance Attribute Summary collapse
-
#connect_options_proc ⇒ Object
Returns the value of attribute connect_options_proc.
-
#inbox ⇒ Object
readonly
Returns the value of attribute inbox.
-
#join_options_proc ⇒ Object
Returns the value of attribute join_options_proc.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
-
#topic ⇒ Object
readonly
Returns the value of attribute topic.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Instance Method Summary collapse
- #connect_options ⇒ Object
-
#initialize ⇒ Socket
constructor
A new instance of Socket.
- #join_options ⇒ Object
-
#request_reply(event:, payload: {}, timeout: 5) ⇒ Object
timeout in seconds.
Constructor Details
#initialize ⇒ Socket
Returns a new instance of Socket.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/yatapp/socket.rb', line 20 def initialize initialize_configuration @path = "ws://run.yatapp.net/socket/websocket?api_token=#{api_access_token}" @topic = "translations:#{project_id}" @join_options = {} @connect_options = {} @inbox = Phoenix::Inbox.new(ttl: 15) super() # MonitorMixin @inbox_cond = new_cond @thread_ready = new_cond @topic_cond = new_cond reset_state_conditions request_reply(event: "ping", payload: {}) end |
Instance Attribute Details
#connect_options_proc ⇒ Object
Returns the value of attribute connect_options_proc.
17 18 19 |
# File 'lib/yatapp/socket.rb', line 17 def @connect_options_proc end |
#inbox ⇒ Object (readonly)
Returns the value of attribute inbox.
16 17 18 |
# File 'lib/yatapp/socket.rb', line 16 def inbox @inbox end |
#join_options_proc ⇒ Object
Returns the value of attribute join_options_proc.
17 18 19 |
# File 'lib/yatapp/socket.rb', line 17 def @join_options_proc end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
16 17 18 |
# File 'lib/yatapp/socket.rb', line 16 def path @path end |
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
16 17 18 |
# File 'lib/yatapp/socket.rb', line 16 def socket @socket end |
#topic ⇒ Object (readonly)
Returns the value of attribute topic.
16 17 18 |
# File 'lib/yatapp/socket.rb', line 16 def topic @topic end |
#verbose ⇒ Object
Returns the value of attribute verbose.
17 18 19 |
# File 'lib/yatapp/socket.rb', line 17 def verbose @verbose end |
Instance Method Details
#connect_options ⇒ Object
66 67 68 69 |
# File 'lib/yatapp/socket.rb', line 66 def return @connect_options unless .call(@connect_options) end |
#join_options ⇒ Object
61 62 63 64 |
# File 'lib/yatapp/socket.rb', line 61 def return @join_options unless .call(@join_options) end |
#request_reply(event:, payload: {}, timeout: 5) ⇒ Object
timeout in seconds
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 |
# File 'lib/yatapp/socket.rb', line 35 def request_reply(event:, payload: {}, timeout: 5) # timeout in seconds ref = SecureRandom.uuid synchronize do ensure_connection @topic_cond.wait_until { @topic_joined } EM.next_tick { socket.send({ topic: topic, event: event, payload: payload, ref: ref }.to_json) } log [event, ref] # Ruby's condition variables only support timeout on the basic 'wait' method; # This should behave roughly as if wait_until also support a timeout: # `inbox_cond.wait_until(timeout) { inbox.key?(ref) || @dead } # # Note that this serves only to unblock the main thread, and should not halt execution of the # socket connection. Therefore, there is a possibility that the inbox may pile up with # unread messages if a lot of timeouts are encountered. A self-sweeping inbox will # be implemented to prevent this. ts = Time.now loop do inbox_cond.wait(timeout) # waits until time expires or signaled break if inbox.key?(ref) || @dead raise 'timeout' if timeout && Time.now > (ts + timeout) end inbox.delete(ref) { raise "reply #{ref} not found" } end end |