Class: Slanger::Connection
- Inherits:
-
Object
- Object
- Slanger::Connection
- Defined in:
- lib/slanger/connection.rb
Instance Attribute Summary collapse
-
#socket ⇒ Object
Returns the value of attribute socket.
-
#socket_id ⇒ Object
Returns the value of attribute socket_id.
Instance Method Summary collapse
- #error(e) ⇒ Object
- #establish ⇒ Object
-
#initialize(socket, socket_id = nil) ⇒ Connection
constructor
A new instance of Connection.
- #send_message(m) ⇒ Object
- #send_payload(*args) ⇒ Object
Constructor Details
#initialize(socket, socket_id = nil) ⇒ Connection
Returns a new instance of Connection.
7 8 9 |
# File 'lib/slanger/connection.rb', line 7 def initialize(socket, socket_id = nil) @socket, @socket_id = socket, socket_id end |
Instance Attribute Details
#socket ⇒ Object
Returns the value of attribute socket.
5 6 7 |
# File 'lib/slanger/connection.rb', line 5 def socket @socket end |
#socket_id ⇒ Object
Returns the value of attribute socket_id.
5 6 7 |
# File 'lib/slanger/connection.rb', line 5 def socket_id @socket_id end |
Instance Method Details
#error(e) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/slanger/connection.rb', line 21 def error(e) begin send_payload nil, "pusher:error", e rescue EventMachine::WebSocket::WebSocketError # Raised if connecection already closed. Only seen with Thor load testing tool end end |
#establish ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/slanger/connection.rb', line 29 def establish @socket_id = "%d.%d" % [Process.pid, SecureRandom.random_number(10 ** 6)] send_payload nil, "pusher:connection_established", { socket_id: @socket_id, activity_timeout: Slanger::Config.activity_timeout, } end |
#send_message(m) ⇒ Object
11 12 13 14 15 |
# File 'lib/slanger/connection.rb', line 11 def (m) msg = Oj.strict_load m s = msg.delete "socket_id" socket.send Oj.dump(msg, mode: :compat) unless s == socket_id end |
#send_payload(*args) ⇒ Object
17 18 19 |
# File 'lib/slanger/connection.rb', line 17 def send_payload(*args) socket.send format(*args) end |