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.



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

def initialize socket, socket_id=nil
  @socket, @socket_id = socket, socket_id
end

Instance Attribute Details

#socketObject

Returns the value of attribute socket.



3
4
5
# File 'lib/slanger/connection.rb', line 3

def socket
  @socket
end

#socket_idObject

Returns the value of attribute socket_id.



3
4
5
# File 'lib/slanger/connection.rb', line 3

def socket_id
  @socket_id
end

Instance Method Details

#error(e) ⇒ Object



19
20
21
# File 'lib/slanger/connection.rb', line 19

def error e
  send_payload nil, 'pusher:error', e
end

#establishObject



23
24
25
26
27
28
29
# File 'lib/slanger/connection.rb', line 23

def establish
  @socket_id = SecureRandom.uuid
  send_payload nil, 'pusher:connection_established', {
      socket_id: @socket_id,
      activity_timeout: Slanger::Config.activity_timeout
    }
end

#send_message(m) ⇒ Object



9
10
11
12
13
# File 'lib/slanger/connection.rb', line 9

def send_message m
  msg = JSON.parse m
  s = msg.delete 'socket_id'
  socket.send msg.to_json unless s == socket_id
end

#send_payload(*args) ⇒ Object



15
16
17
# File 'lib/slanger/connection.rb', line 15

def send_payload *args
  socket.send format(*args)
end