Class: Thin::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/thin-em-websocket.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ws_bufferObject (readonly)

Returns the value of attribute ws_buffer.



246
247
248
# File 'lib/thin-em-websocket.rb', line 246

def ws_buffer
  @ws_buffer
end

Instance Method Details

#processObject



249
250
251
252
253
254
255
256
# File 'lib/thin-em-websocket.rb', line 249

def process
  if websocket? && !@request.env['em.connection']
    @socket_connection = ThinEM::Websocket::Connection.new(self)
    @request.env['em.connection'] = @socket_connection
    @response.persistent!
  end
  thin_process
end

#receive_data(data) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/thin-em-websocket.rb', line 258

def receive_data(data)
  if @socket_connection && @socket_connection.upgraded?
    @socket_connection.receive_data(data)
  else
    @ws_buffer ||= ""
    @ws_buffer << data unless @ws_buffer == false
    @ws_buffer = false if @ws_buffer.length > 10000 # some sane cutoff so we dont have too much data in memory
    @socket_connection.upgrade_websocket if @socket_connection && @socket_connection.pending_upgrade?
    thin_receive_data(data)
  end

end

#thin_processObject



243
# File 'lib/thin-em-websocket.rb', line 243

alias :thin_process      :process

#thin_receive_dataObject



244
# File 'lib/thin-em-websocket.rb', line 244

alias :thin_receive_data :receive_data

#websocket?Boolean



271
272
273
274
275
276
277
278
279
# File 'lib/thin-em-websocket.rb', line 271

def websocket?
  return @websocket unless @websocket == nil
  env = @request.env
  @websocket =
    env['REQUEST_METHOD'] == 'GET' and
    env['HTTP_CONNECTION'] and
    env['HTTP_CONNECTION'].split(/\s*,\s*/).include?('Upgrade') and
    env['HTTP_UPGRADE'].downcase == 'websocket'
end