Class: Turntabler::Connection Private
- Inherits:
-
Object
- Object
- Turntabler::Connection
- Includes:
- Assertions, Loggable
- Defined in:
- lib/turntabler/connection.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Represents the interface for sending and receiving data in Turntable
Constant Summary collapse
- HTTP_APIS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Tracks the list of APIs that don’t work through the web socket – these must be requested through the HTTP channel
%w(room.directory_rooms user.get_prefs)
Instance Attribute Summary collapse
-
#handler ⇒ Proc
private
The callback to run when a message is received from the underlying socket.
-
#url ⇒ String
readonly
private
The URL that this connection is bound to.
Instance Method Summary collapse
-
#close ⇒ true
private
Closes the connection (if one was previously opened).
-
#connected? ⇒ Boolean
private
Whether this connection’s socket is currently open.
-
#initialize(url, options = {}) ⇒ Connection
constructor
private
Builds a new connection for sending / receiving data via the given url.
-
#publish(params) ⇒ Fixnum
private
Publishes the given params to the underlying web socket.
-
#start ⇒ true
private
Initiates the connection with turntable.
Methods included from Assertions
#assert_valid_keys, #assert_valid_values
Constructor Details
#initialize(url, options = {}) ⇒ Connection
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This will not open the connection – #start must be explicitly called in order to do so.
Builds a new connection for sending / receiving data via the given url.
37 38 39 40 41 42 43 44 |
# File 'lib/turntabler/connection.rb', line 37 def initialize(url, = {}) assert_valid_keys(, :timeout, :params) @url = url @message_id = 0 @timeout = [:timeout] @default_params = [:params] || {} end |
Instance Attribute Details
#handler ⇒ Proc
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The callback to run when a message is received from the underlying socket. The data passed to the callback will always be a hash.
27 28 29 |
# File 'lib/turntabler/connection.rb', line 27 def handler @handler end |
#url ⇒ String (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The URL that this connection is bound to
22 23 24 |
# File 'lib/turntabler/connection.rb', line 22 def url @url end |
Instance Method Details
#close ⇒ true
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Closes the connection (if one was previously opened)
60 61 62 63 |
# File 'lib/turntabler/connection.rb', line 60 def close @socket.close if @socket true end |
#connected? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Whether this connection’s socket is currently open
68 69 70 |
# File 'lib/turntabler/connection.rb', line 68 def connected? @connected end |
#publish(params) ⇒ Fixnum
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Publishes the given params to the underlying web socket. The defaults initially configured as part of the connection will also be included in the message.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/turntabler/connection.rb', line 78 def publish(params) params[:msgid] = = params = @default_params.merge(params) logger.debug "Message sent: #{params.inspect}" if HTTP_APIS.include?(params[:api]) publish_to_http(params) else publish_to_socket(params) end # Add timeout handler EventMachine.add_timer(@timeout) do dispatch('msgid' => , 'command' => 'response_received', 'error' => 'timed out') end if @timeout end |
#start ⇒ true
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initiates the connection with turntable
49 50 51 52 53 54 55 |
# File 'lib/turntabler/connection.rb', line 49 def start @socket = Faye::WebSocket::Client.new(url) @socket.onopen = lambda {|event| on_open(event)} @socket.onclose = lambda {|event| on_close(event)} @socket. = lambda {|event| (event)} true end |