Class: Volt::WebsocketHandler

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

Overview

Setup the dispatcher for the socket connection handler. SocketConnectionHandler.dispatcher = Dispatcher.new

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ WebsocketHandler

Returns a new instance of WebsocketHandler.



11
12
13
14
15
16
# File 'lib/volt/server/websocket/websocket_handler.rb', line 11

def initialize(app)
  # Setup the rack server and adaptor
  RackServerAdaptor.load

  @app = app
end

Instance Method Details

#call(env) ⇒ Object



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

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

    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