Class: Rocket::Server::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/rocket/server/session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_id) ⇒ Session

Returns a new instance of Session.



8
9
10
11
# File 'lib/rocket/server/session.rb', line 8

def initialize(app_id)
  @app = Rocket::Server::App.find(app_id)
  @subscriptions = {}
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



6
7
8
# File 'lib/rocket/server/session.rb', line 6

def app
  @app
end

#subscriptionsObject (readonly)

Returns the value of attribute subscriptions.



5
6
7
# File 'lib/rocket/server/session.rb', line 5

def subscriptions
  @subscriptions
end

Instance Method Details

#app_idObject

Returns id of current application.



14
15
16
# File 'lib/rocket/server/session.rb', line 14

def app_id
  @app.id
end

#authenticate!(secret) ⇒ Object

Authenticate current session with your secret key.



24
25
26
# File 'lib/rocket/server/session.rb', line 24

def authenticate!(secret)
  @authenticated = (app.secret == secret)
end

#authenticated?Boolean

Returns true when current session is authenticated with secret key.

Returns:

  • (Boolean)


19
20
21
# File 'lib/rocket/server/session.rb', line 19

def authenticated?
  !!@authenticated
end

#closeObject

Close current session and kill all active subscriptions.



49
50
51
52
53
54
# File 'lib/rocket/server/session.rb', line 49

def close
  subscriptions.keys.each do |id| 
    channel, sig = id.to_a.flatten
    Channel[app_id => channel].unsubscribe(subscriptions.delete(channel => sig))
  end
end

#subscribe(channel, connection) ⇒ Object

Subscribes specified channel by given connected client.

subscribe("my-awesome-channel", connection) # => subscription ID


32
33
34
35
36
# File 'lib/rocket/server/session.rb', line 32

def subscribe(channel, connection)
  sid = Channel[app_id => channel].subscribe {|msg| connection.send(msg) }
  subscriptions[channel => connection.signature] = sid
  sid
end

#unsubscribe(channel, connection) ⇒ Object

Unsubscribes specified channel for given client.

unsubscribe("my-awesome-channel", connection)


42
43
44
45
46
# File 'lib/rocket/server/session.rb', line 42

def unsubscribe(channel, connection)
  if sid = subscriptions.delete(channel => connection.signature)
    Channel[app_id => channel].unsubscribe(sid)
  end
end