Class: Rocket::Server::Connection
- Inherits:
-
EM::WebSocket::Connection
- Object
- EM::WebSocket::Connection
- Rocket::Server::Connection
- Includes:
- Helpers
- Defined in:
- lib/rocket/server/connection.rb
Constant Summary collapse
- LOG_MESSAGES =
{ :app_not_found_error => "#%d : %s", :opening_connection => "%s #%d : Opening new connection...", :closing_connection => "%s #%d : Closing connection...", :auth_success => "%s #%d : Session authenticated successfully!", :auth_error => "%s #%d : Authentication error! Invalid secret key.", :web_socket_error => "%s #%d : Web socket error: %s", :invalid_json_error => "%s #%d : Invalid event's data! This is not valid JSON: %s", :subscribing_channel => "%s #%d : Subscribing the '%s' channel.", :unsubscribing_channel => "%s #%d : Unsubscribing the '%s' channel.", :trigger_error => "%s #%d : Invalid event's data! No channel or event name specified in: %s", :access_denied_error => "%s #%d : Action can't be performed, access denied!", :trigger_success => "%s #%d : Triggering '%s' on '%s' channel: %s", :subscribe_error => "%s #%d : Can't start subscription! Invalid data: %s", :unsubscribe_error => "%s #%d : Can't stop subscription! Invalid data: %s" }
- APP_PATH_PATTERN =
Only connections to path matching this pattern will be accepted.
/^\/app\/([\d\w\-\_]+)/
Instance Attribute Summary collapse
-
#session ⇒ Object
readonly
Returns the value of attribute session.
Instance Method Summary collapse
- #app_id ⇒ Object
-
#initialize(options = {}) ⇒ Connection
constructor
A new instance of Connection.
-
#onclose ⇒ Object
Closes current session.
-
#onerror(reason) ⇒ Object
Handler websocket’s of runtime errors.
-
#onmessage(message) ⇒ Object
Dispatches the received message.
-
#onopen ⇒ Object
Starts new session for application specified in request path.
-
#session? ⇒ Boolean
Returns true if session is open.
-
#subscribe!(data) ⇒ Object
Handles subscription event.
-
#trigger!(data) ⇒ Object
Publishes given message.
-
#unsubscribe!(data) ⇒ Object
Handles unsubscribe event.
Methods included from Helpers
Constructor Details
#initialize(options = {}) ⇒ Connection
Returns a new instance of Connection.
29 30 31 32 33 34 35 36 |
# File 'lib/rocket/server/connection.rb', line 29 def initialize(={}) super() @onopen = method(:onopen) @onclose = method(:onclose) @onmessage = method(:onmessage) @onerror = method(:onerror) @debug = Rocket::Server.debug end |
Instance Attribute Details
#session ⇒ Object (readonly)
Returns the value of attribute session.
27 28 29 |
# File 'lib/rocket/server/connection.rb', line 27 def session @session end |
Instance Method Details
#app_id ⇒ Object
131 132 133 |
# File 'lib/rocket/server/connection.rb', line 131 def app_id session? ? session.app_id : "unknown" end |
#onclose ⇒ Object
Closes current session.
66 67 68 69 |
# File 'lib/rocket/server/connection.rb', line 66 def onclose log_debug(:closing_connection, app_id, signature) @session.close and true if session? end |
#onerror(reason) ⇒ Object
Handler websocket’s of runtime errors.
72 73 74 |
# File 'lib/rocket/server/connection.rb', line 72 def onerror(reason) log_error(:web_socket_error, app_id, signature, reason) end |
#onmessage(message) ⇒ Object
Dispatches the received message.
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/rocket/server/connection.rb', line 77 def () if session? and data = JSON.parse() case data["event"] when "rocket:subscribe" then subscribe!(data['data'] || {}) when "rocket:unsubscribe" then unsubscribe!(data['data'] || {}) else trigger!(data) end end rescue JSON::ParserError log_error(:invalid_json_error, app_id, signature, .inspect) end |
#onopen ⇒ Object
Starts new session for application specified in request path.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rocket/server/connection.rb', line 39 def onopen ok = false request["Path"] =~ APP_PATH_PATTERN if @session = Session.new($1) log_debug(:opening_connection, app_id, signature) if secret = request["Query"]["secret"] if @session.authenticate!(secret) ok = true log_debug(:auth_success, app_id, signature) else log_debug(:auth_error, app_id, signature) end else ok = true end end send({:event => 'rocket:connected', :data => { :session_id => signature }}.to_json) if ok return ok rescue App::NotFoundError => ex log_error(:app_not_found_error, signature, ex.to_s) close_connection end |
#session? ⇒ Boolean
Returns true if session is open.
91 92 93 |
# File 'lib/rocket/server/connection.rb', line 91 def session? !!@session end |
#subscribe!(data) ⇒ Object
Handles subscription event.
96 97 98 99 100 101 102 103 104 |
# File 'lib/rocket/server/connection.rb', line 96 def subscribe!(data) if channel = data["channel"] log_debug(:subscribing_channel, app_id, signature, channel) @session.subscribe(channel, self) else log_error(:subscribe_error, app_id, signature, data.to_json) false end end |
#trigger!(data) ⇒ Object
Publishes given message.
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/rocket/server/connection.rb', line 118 def trigger!(data) if session? and session.authenticated? channel, event = data.values_at("channel", "event") if channel and event log_debug(:trigger_success, app_id, signature, event, channel, data.inspect) return Channel[session.app_id => channel].push(data.to_json) end log_error(:trigger_error, app_id, signature, data.inspect) else log_error(:access_denied_error, app_id, signature) end end |
#unsubscribe!(data) ⇒ Object
Handles unsubscribe event.
107 108 109 110 111 112 113 114 115 |
# File 'lib/rocket/server/connection.rb', line 107 def unsubscribe!(data) if channel = data["channel"] log_debug(:unsubscribing_channel, app_id, signature, channel) @session.unsubscribe(channel, self) else log_error(:unsubscribe_error, app_id, signature, data.to_json) false end end |