Class: Alondra::Connection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(websocket, session = {}) ⇒ Connection

Returns a new instance of Connection.



30
31
32
33
34
35
36
# File 'lib/alondra/connection.rb', line 30

def initialize(websocket, session = {})
  @session = session.symbolize_keys
  @websocket   = websocket
  @uuid = UUIDTools::UUID.random_create

  Connections[websocket] = self
end

Instance Attribute Details

#channelsObject (readonly)

Returns the value of attribute channels.



28
29
30
# File 'lib/alondra/connection.rb', line 28

def channels
  @channels
end

#sessionObject (readonly)

Returns the value of attribute session.



27
28
29
# File 'lib/alondra/connection.rb', line 27

def session
  @session
end

#uuidObject (readonly)

Returns the value of attribute uuid.



25
26
27
# File 'lib/alondra/connection.rb', line 25

def uuid
  @uuid
end

#websocketObject (readonly)

Returns the value of attribute websocket.



26
27
28
# File 'lib/alondra/connection.rb', line 26

def websocket
  @websocket
end

Instance Method Details

#destroy!Object



47
48
49
50
# File 'lib/alondra/connection.rb', line 47

def destroy!
  channels.each { |c| c.unsubscribe self }
  Connections.delete self.websocket
end

#receive(event_or_message) ⇒ Object



42
43
44
45
# File 'lib/alondra/connection.rb', line 42

def receive(event_or_message)
  Log.info "sending: #{event_or_message.to_json}"
  websocket.send event_or_message.to_json
end