Class: BrowserChannel

Inherits:
Object
  • Object
show all
Includes:
EventMachine::Deferrable
Defined in:
lib/browser_channel.rb

Defined Under Namespace

Classes: Handler, Server, Session

Constant Summary collapse

LATEST_CHANNEL_VERSION =
8
NOOP =
['noop'.freeze].freeze

Instance Method Summary collapse

Constructor Details

#initialize(options, env) ⇒ BrowserChannel

Returns a new instance of BrowserChannel.



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
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/browser_channel.rb', line 186

def initialize options, env
  @options = options
  request = Rack::Request.new env
  request_GET = request.GET
  request_GET_SID = request_GET['SID']
  @session = Session.new @options, request_GET_SID, request_GET['AID']
  unless @session
    request.env['async.callback'].call [400, {}, self]
    succeed
    return
  end
  if @session.array_id == -1
    @session << ['c', @session.id, @options[:host_prefix], LATEST_CHANNEL_VERSION]
  end
  @chunked = request_GET['CI'] == '0'
  @html_data_type = request_GET['TYPE'] == 'html'
  headers = {'Cache-Control' => 'no-cache'}
  headers['Content-Type'] = @html_data_type ? 'text/html' : 'application/javascript'
  env['async.callback'].call [200, headers, self]
  if @html_data_type
    @body_callback.call "<html><body>\n"
    if domain = request_GET['DOMAIN']
      @body_callback.call "<script>try{document.domain=#{domain.dump};}catch(e){}</script>\n"
    end
  end
  if request_GET['TYPE'] == 'terminate'
    @session.terminate
    succeed
    return
  end
  if request.post? and request_GET_SID
    send_data @session.call request.POST
    return
  elsif request.get?
    # Thin calls errback when connection closes, even after success
    if @chunked
      @last_message = Time.now - ( @options[:keep_alive_interval] - @options[:keep_alive_first] )
      keep_alive 
      errback { @timer.cancel }
    end
    @session.bind self 
    errback { @session.unbind self }
  end
  send_data @session.messages unless @session.messages.empty?
end

Instance Method Details

#each(&block) ⇒ Object



241
242
243
# File 'lib/browser_channel.rb', line 241

def each &block
  @body_callback = block
end

#keep_aliveObject



232
233
234
235
236
237
238
239
# File 'lib/browser_channel.rb', line 232

def keep_alive
  idle = Time.now - @last_message
  if idle > @options[:keep_alive_interval]
    @session << NOOP
    idle = 0
  end
  @timer = EventMachine::Timer.new @options[:keep_alive_interval] - idle, &method(:keep_alive)
end

#send_data(messages) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/browser_channel.rb', line 245

def send_data messages
  json = messages.to_json
  if @html_data_type
    @body_callback.call "<script>try{parent.m(#{json.dump});}catch(e){}</script>\n"
  else
    @body_callback.call "#{json.bytesize+1}\n#{json}\n"
  end
  @last_message = Time.now
  unless @chunked
    if @html_data_type
      @body_callback.call "<script>try{parent.d();}catch(e){}</script>\n</body></html>\n"
    end
    succeed 
  end
end