Class: SockJS::Connection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session_class, options) ⇒ Connection

Returns a new instance of Connection.



6
7
8
9
10
# File 'lib/sockjs/connection.rb', line 6

def initialize(session_class, options)
  self.status = :not_connected
  @session_class = session_class
  @options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



11
12
13
# File 'lib/sockjs/connection.rb', line 11

def options
  @options
end

#statusObject

Returns the value of attribute status.



11
12
13
# File 'lib/sockjs/connection.rb', line 11

def status
  @status
end

Instance Method Details

#create_session(session_key) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/sockjs/connection.rb', line 36

def create_session(session_key)
  SockJS.debug "Creating session at #{session_key.inspect}"
  raise "Session already exists for #{session_key.inspect}" if sessions.has_key?(session_key)
  session = @session_class.new(self)
  sessions[session_key] = session
  session.opened
  session
end

#get_session(session_key) ⇒ Object



31
32
33
34
# File 'lib/sockjs/connection.rb', line 31

def get_session(session_key)
  SockJS.debug "Looking up session at #{session_key.inspect}"
  sessions.fetch(session_key)
end

#sessionsObject

XXX TODO: remove dead sessions as they’re get_session’d, along with a recurring clearout



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sockjs/connection.rb', line 15

def sessions
  SockJS.debug "Refreshing sessions"

  if @sessions
    @sessions.delete_if do |_, session|
      unless session.alive?
        SockJS.debug "Removing closed session #{_}"
      end

      !session.alive?
    end
  else
    @sessions = {}
  end
end