def start!
@buffer = @env[Thin::Request::RACK_INPUT].read + @buffer
@env.delete Thin::Request::RACK_INPUT
@env.delete Thin::Request::ASYNC_CALLBACK
@env.delete Thin::Request::ASYNC_CLOSE
@version = @env['HTTP_SEC_WEBSOCKET_VERSION']
@version ||= "hixie-76" if @env.has_key?('HTTP_SEC_WEBSOCKET_KEY1') and @env.has_key?('HTTP_SEC_WEBSOCKET_KEY2')
@version ||= "hixie-75"
@origin ||= @env['HTTP_SEC_WEBSOCKET_ORIGIN'] || @env['HTTP_ORIGIN']
@location ||= "ws#{secure? ? 's' : ''}://#{@env['HTTP_HOST']}#{@env['REQUEST_PATH']}"
@protocol ||= @env['HTTP_SEC_WEBSOCKET_PROTOCOL'] || @env['HTTP_WEBSOCKET_PROTOCOL']
EM.next_tick { callback :on_start, self rescue error! "Error in start callback" }
EM.next_tick method :handshake!
@state = :started
self
rescue
error! "Error starting connection"
end