Class: Garufa::Connection
- Inherits:
-
Object
- Object
- Garufa::Connection
- Defined in:
- lib/garufa/connection.rb
Instance Attribute Summary collapse
-
#socket_id ⇒ Object
readonly
Returns the value of attribute socket_id.
Instance Method Summary collapse
- #cleanup ⇒ Object
- #close ⇒ Object
- #error(code, message) ⇒ Object
- #establish ⇒ Object
- #handle_incomming_data(data) ⇒ Object
-
#initialize(socket, logger) ⇒ Connection
constructor
A new instance of Connection.
- #send_message(message) ⇒ Object
Constructor Details
#initialize(socket, logger) ⇒ Connection
Returns a new instance of Connection.
12 13 14 15 16 17 |
# File 'lib/garufa/connection.rb', line 12 def initialize(socket, logger) @socket = socket @logger = logger @socket_id = build_socket_id @subscriptions = {} end |
Instance Attribute Details
#socket_id ⇒ Object (readonly)
Returns the value of attribute socket_id.
10 11 12 |
# File 'lib/garufa/connection.rb', line 10 def socket_id @socket_id end |
Instance Method Details
#cleanup ⇒ Object
56 57 58 59 |
# File 'lib/garufa/connection.rb', line 56 def cleanup @subscriptions.each { |_, subscription| unsubscribe(subscription) } @subscriptions.clear end |
#close ⇒ Object
52 53 54 |
# File 'lib/garufa/connection.rb', line 52 def close @socket.close end |
#error(code, message) ⇒ Object
42 43 44 |
# File 'lib/garufa/connection.rb', line 42 def error(code, ) Message.error(code, ) end |
#establish ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/garufa/connection.rb', line 19 def establish if valid_app_key? Message.connection_established(@socket_id) else error(4001, "Could not find app by key #{app_key}") close end end |
#handle_incomming_data(data) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/garufa/connection.rb', line 28 def handle_incomming_data(data) @logger.debug "Incomming message. #{@socket_id}: #{data}" = Message.new(JSON.parse(data)) event, data = .event, .data case event when /^pusher:/ handle_pusher_event(event, data) when /^client-/ handle_client_event(event, data, .channel) end end |
#send_message(message) ⇒ Object
46 47 48 49 50 |
# File 'lib/garufa/connection.rb', line 46 def () @logger.debug "Outgoing message. #{@socket_id}: #{message.to_json}" @socket.send .to_json end |