Class: Cramp::Action

Inherits:
Abstract show all
Includes:
KeepConnectionAlive, PeriodicTimer
Defined in:
lib/cramp/action.rb

Direct Known Subclasses

LongPolling, SSE, Websocket

Instance Method Summary collapse

Methods included from KeepConnectionAlive

#keep_connection_alive

Methods inherited from Abstract

call, #process

Methods included from Callbacks

#before_start, #callback_wrapper, #on_finish, #on_start

Constructor Details

#initialize(env) ⇒ Action

Returns a new instance of Action.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cramp/action.rb', line 6

def initialize(env)
  super
  
  case
  when Faye::EventSource.eventsource?(env)
    # request has Accept: text/event-stream
    # faye server adapter intercepts headers - need to send them in send_initial_response or use faye's implementation
    @eventsource_detected = true
    unless transport == :sse
      err = "WARNING: Cramp got request with EventSource header on action with transport #{transport} (not sse)! Response may not contain valid http headers!"
      Cramp.logger ? Cramp.logger.error(err) : $stderr.puts(err)
    end
  when Faye::WebSocket.websocket?(env)
    @web_socket = Faye::WebSocket.new(env)
    @web_socket.onmessage = lambda do |event|
      message = event.data
      _invoke_data_callbacks(message) if message.is_a?(String)
    end
  end
end