Class: Slanger::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/slanger/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#socketObject

Returns the value of attribute socket.



5
6
7
# File 'lib/slanger/connection.rb', line 5

def socket
  @socket
end

#socket_idObject

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

#establishObject



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 send_message 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