Class: RComet::RackAdapter
- Inherits:
-
Object
- Object
- RComet::RackAdapter
- Defined in:
- lib/rcomet/rack_adapter.rb
Overview
:nodoc:
Instance Method Summary collapse
- #call(env) ⇒ Object
- #channel ⇒ Object
- #connect(message) ⇒ Object
- #disconnect(message) ⇒ Object
- #handle(message) ⇒ Object
- #handshake(message) ⇒ Object
-
#initialize(app = nil, options = nil, &block) ⇒ RackAdapter
constructor
A new instance of RackAdapter.
- #process(jsonp, messages, get) ⇒ Object
- #subscribe(message) ⇒ Object
- #timeout(&block) ⇒ Object
- #unsubscribe(message) ⇒ Object
Constructor Details
#initialize(app = nil, options = nil, &block) ⇒ RackAdapter
Returns a new instance of RackAdapter.
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/rcomet/rack_adapter.rb', line 22 def initialize(app = nil, = nil, &block) @app = app if app.respond_to?(:call) = [app, ].grep(Hash).first @channels = RComet::ChannelSet.new @users = Hash.new @timeout = nil instance_eval(&block) if block return @app end |
Instance Method Details
#call(env) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rcomet/rack_adapter.rb', line 33 def call(env) request = Rack::Request.new(env) if request.params.empty? [404, {'Content-Type' => 'text/html'}, ""] else = JSON.parse(request.params['message']) jsonp = request.params['jsonp'] || JSONP_CALLBACK get = request.get? process( jsonp, , get ) end end |
#channel ⇒ Object
47 48 49 |
# File 'lib/rcomet/rack_adapter.rb', line 47 def channel @channels end |
#connect(message) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/rcomet/rack_adapter.rb', line 110 def connect( ) # Initialize response response = { 'channel' => RComet::Channel::CONNECT, 'clientId' => ['clientId'] } response << { 'id' => ['id'] } if .has_key?('id') # Get user for clientId user = @users[['clientId']] if user # Ok, connect user user.connected = true time = Time.new response << { 'successful' => true, 'timestamp' =>"#{time.hour}:#{time.min}:#{time.sec} #{time.year}" } if user.has_channel? response = user.wait( response ) end user.connected = false else # User does not exist! response << { 'successful' => false, 'error' => "402:#{message['clientId']}:Unknown Client ID" } end return response end |
#disconnect(message) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/rcomet/rack_adapter.rb', line 144 def disconnect( ) # Initialize response message response = { 'channel' => RComet::Channel::DISCONNECT, 'clientId' => ['clientId'] } response << { 'id' => ['id'] } if .has_key?('id') # Get user for clientId user = @users[['clientId']] if user # Ok, disconnect user user.connected = false @users.delete( ['clientId'] ) # Complete reponse response << { 'successful' => true } else # User does nit exist! response << { 'successful' => false, 'error' => "402:#{message['clientId']}:Unknown Client ID" } end return response end |
#handle(message) ⇒ Object
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/rcomet/rack_adapter.rb', line 256 def handle( ) # Initialize response response = { 'channel' => ['channel'] } response << { 'id' => ['id'] } if .has_key?('id') c = channel[['channel']] if c.nil? response << { 'successful' => false, 'error' => "404:#{message['channel']}:Unknown Channel" } else response << { 'successful' => true } end Thread.new do unless c.nil? if c.handler.nil? c.data( ['data'] ) else c.handler.call( ) end end return end return response end |
#handshake(message) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/rcomet/rack_adapter.rb', line 91 def handshake( ) begin user = User.new(self) end while @users.has_key?(user.id) @users[user.id] = user response = { 'channel' => RComet::Channel::HANDSHAKE, 'version' => RComet::BAYEUX_VERSION, 'minimumVersion' => RComet::BAYEUX_VERSION, 'supportedConnectionTypes' => ['long-polling','callback-polling'], 'clientId' => user.id, 'successful' => true } response << { 'id' => ['id'] } if .has_key?('id') return response end |
#process(jsonp, messages, get) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/rcomet/rack_adapter.rb', line 56 def process( jsonp, , get ) replies = [] .each do || reply = nil case ['channel'] when RComet::Channel::HANDSHAKE reply = handshake( ) when RComet::Channel::CONNECT reply = connect( ) when RComet::Channel::DISCONNECT reply = disconnect( ) when RComet::Channel::SUBSCRIBE reply = subscribe( ) when RComet::Channel::UNSUBSCRIBE reply = unsubscribe( ) else reply = handle( ) end if reply.class == Array replies.concat( reply ) else replies << reply end end response = JSON.generate(replies) type = {'Content-Type' => 'text/json'} if get response = "#{jsonp}(#{response});" type = {'Content-Type' => 'text/javascript'} end [200, type, [response]] end |
#subscribe(message) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/rcomet/rack_adapter.rb', line 174 def subscribe( ) response = { 'channel' => RComet::Channel::SUBSCRIBE, 'clientId' => ['clientId'] } response << { 'id' => ['id'] } if .has_key?('id') # Get user for clientId user = @users[['clientId']] if user # Get channel channel = @channels[['subscription']] if channel user.subscribe( channel ) response << { 'successful' => true, 'subscription' => ['subscription'] } unless channel.data.nil? response = [response] response << { 'channel' => ['subscription'], 'id' => (['id'].to_i+1).to_s, 'data' => channel.data } end else #Channel doesn't exist response << { 'successful' => false, 'subscription' => ['subscription'], 'error' => "404:#{message['subscription']}:Unknown Channel" } end else response << { 'successful' => false, 'error' => "402:#{message['clientId']}:Unknown Client ID" } end return response end |
#timeout(&block) ⇒ Object
51 52 53 54 |
# File 'lib/rcomet/rack_adapter.rb', line 51 def timeout( &block ) @handler = block if block_given? @handler end |
#unsubscribe(message) ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/rcomet/rack_adapter.rb', line 219 def unsubscribe( ) # Initialize response response = { 'channel' => RComet::Channel::UNSUBSCRIBE, 'clientId' => ['clientId'] } response << { 'id' => ['id'] } if .has_key?('id') # Get user for clientId user = @users[['clientId']] if user # Get channel channel = @channels[['subscription']] if channel user.unsubscribe( channel ) response << { 'successful' => true, 'subscription' => ['subscription'] } else #Channel doesn't exist response << { 'successful' => false, 'subscription' => ['subscription'], 'error' => "404:#{message['subscription']}:Unknown Channel" } end else response << { 'successful' => false, 'error' => "402:#{message['clientId']}:Unknown Client ID" } end return response end |