Class: Volt::WebsocketHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/volt/server/websocket/websocket_handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ WebsocketHandler

Returns a new instance of WebsocketHandler.



15
16
17
# File 'lib/volt/server/websocket/websocket_handler.rb', line 15

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/volt/server/websocket/websocket_handler.rb', line 19

def call(env)
  if Faye::WebSocket.websocket?(env)
    ws = Faye::WebSocket.new(env, nil, WEBSOCKET_OPTIONS)

    socket_connection_handler = SocketConnectionHandler.new(ws)

    ws.on :message do |event|
      socket_connection_handler.process_message(event.data)
    end

    ws.on :close do |event|
      socket_connection_handler.closed

      ws = nil
    end

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