Class: DRb::WebSocket::RackApp

Inherits:
Object
  • Object
show all
Defined in:
lib/drb/websocket/rack_app.rb

Defined Under Namespace

Classes: Config

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RackApp

Returns a new instance of RackApp.



4
5
6
7
# File 'lib/drb/websocket/rack_app.rb', line 4

def initialize(app)
  @app = app
  RackApp.config.use_rack = true
end

Class Method Details

.close(key) ⇒ Object



15
16
17
18
19
# File 'lib/drb/websocket/rack_app.rb', line 15

def self.close(key)
  if @handlers.has_key?(key)
    @handlers.delete(key)
  end
end

.config {|@config| ... } ⇒ Object

Yields:



52
53
54
55
56
# File 'lib/drb/websocket/rack_app.rb', line 52

def self.config
  @config ||= Config.new
  yield @config if block_given?
  @config
end

.handler(key) ⇒ Object



11
12
13
# File 'lib/drb/websocket/rack_app.rb', line 11

def self.handler(key)
  @handlers[key]
end

.register(key, handler) ⇒ Object



21
22
23
# File 'lib/drb/websocket/rack_app.rb', line 21

def self.register(key, handler)
  @handlers[key] = handler
end

Instance Method Details

#call(env) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/drb/websocket/rack_app.rb', line 25

def call(env)
  if Faye::WebSocket.websocket?(env)
    ws = Faye::WebSocket.new(env)
    req = Rack::Request.new(env)
    uri = "ws://#{req.host}:#{req.port}#{req.path == '/' ? nil : req.path}"

    handler = req.path.start_with?('/callback') ? RackApp.register(uri, CallbackHandler.new(uri)) : RackApp.handler(uri)
    handler.on_session_start(ws)

    ws.on :message do |event|
      handler.on_message(event.data)
    end

    ws.on :close do |event|
      if CallbackHandler === handler
        RackApp.close(uri)
      end
      ws = nil
    end

    # Return async Rack response
    ws.rack_response
  else
    @app.call(env)
  end
end