Class: Rocket::Server::Session
- Inherits:
-
Object
- Object
- Rocket::Server::Session
- Defined in:
- lib/rocket/server/session.rb
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#subscriptions ⇒ Object
readonly
Returns the value of attribute subscriptions.
Instance Method Summary collapse
-
#app_id ⇒ Object
Returns id of current application.
-
#authenticate!(secret) ⇒ Object
Authenticate current session with your secret key.
-
#authenticated? ⇒ Boolean
Returns
true
when current session is authenticated with secret key. -
#close ⇒ Object
Close current session and kill all active subscriptions.
-
#initialize(app_id) ⇒ Session
constructor
A new instance of Session.
-
#subscribe(channel, connection) ⇒ Object
Subscribes specified channel by given connected client.
-
#unsubscribe(channel, connection) ⇒ Object
Unsubscribes specified channel for given client.
Constructor Details
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
6 7 8 |
# File 'lib/rocket/server/session.rb', line 6 def app @app end |
#subscriptions ⇒ Object (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_id ⇒ Object
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.
19 20 21 |
# File 'lib/rocket/server/session.rb', line 19 def authenticated? !!@authenticated end |
#close ⇒ Object
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 |