Class: SockJS::ConsumingTransport

Inherits:
Transport show all
Defined in:
lib/sockjs/transport.rb

Instance Attribute Summary

Attributes inherited from Endpoint

#connection, #options

Attributes included from Endpoint::ClassMethods

#method, #prefix

Instance Method Summary collapse

Methods inherited from Transport

#exception_response, #handle_request, #handle_session_unavailable, #request_data, #response_beginning, #server_key, #session_key

Methods inherited from SessionEndpoint

routing_prefix

Methods inherited from Endpoint

#build_error_response, #build_response, #call, #empty_string, #error_content_type, #handle, #handle_http_error, #handle_request, #initialize, #inspect, #response_class, #setup_response

Methods included from Endpoint::ClassMethods

#add_route, #add_routes, #endpoints, #register, #route_conditions, #routing_prefix

Constructor Details

This class inherits a constructor from SockJS::Endpoint

Instance Method Details

#closing_frame(response, status, message) ⇒ Object



285
286
287
288
# File 'lib/sockjs/transport.rb', line 285

def closing_frame(response, status, message)
  send_data(response, format_frame(response, Protocol::ClosingFrame.new(status, message)))
  finish_response(response)
end

#finish_response(response) ⇒ Object



269
270
271
# File 'lib/sockjs/transport.rb', line 269

def finish_response(response)
  response.finish
end

#format_frame(response, frame) ⇒ Object



296
297
298
# File 'lib/sockjs/transport.rb', line 296

def format_frame(response, frame)
  frame.to_s + "\n"
end

#get_session(response) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/sockjs/transport.rb', line 300

def get_session(response)
  begin
    session = connection.get_session(session_key(response))
    response_beginning(response)
    return session
  rescue KeyError
    SockJS::debug("Missing session for #{session_key(response)} - creating new")
    session = connection.create_session(session_key(response))
    response_beginning(response)
    opening_frame(response)
    return session
  end
end

#heartbeat_frame(response) ⇒ Object



277
278
279
# File 'lib/sockjs/transport.rb', line 277

def heartbeat_frame(response)
  send_data(response, format_frame(response, Protocol::HeartbeatFrame.instance))
end

#messages_frame(response, messages) ⇒ Object



281
282
283
# File 'lib/sockjs/transport.rb', line 281

def messages_frame(response, messages)
  send_data(response, format_frame(response, Protocol::ArrayFrame.new(messages)))
end

#opening_frame(response) ⇒ Object



273
274
275
# File 'lib/sockjs/transport.rb', line 273

def opening_frame(response)
  send_data(response, format_frame(response, Protocol::OpeningFrame.instance))
end

#process_session(session, response) ⇒ Object



254
255
256
257
258
259
260
261
262
263
# File 'lib/sockjs/transport.rb', line 254

def process_session(session, response)
  session.attach_consumer(response, self)
  response.request.on_close do
    begin
      request_closed(session)
    rescue Object => ex
      SockJS::debug "Exception when closing request: #{ex.inspect}"
    end
  end
end

#request_closed(session) ⇒ Object



265
266
267
# File 'lib/sockjs/transport.rb', line 265

def request_closed(session)
  session.detach_consumer
end

#send_data(response, data) ⇒ Object

TODO: Consider absorbing format_frame into send_data



291
292
293
294
# File 'lib/sockjs/transport.rb', line 291

def send_data(response, data)
  response.write(data)
  return data.length
end