Sha256: c1bed2ca567fa56f54c028d38c87e99e0e6b5fe098c4d6331516a8b079460c9a

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 KB

Contents

module Rack
  module WebSocket
    class Application

      DEFAULT_OPTIONS = {}
      attr_accessor :options

      def on_open; end # Fired when a client is connected.
      def on_message(msg); end # Fired when a message from a client is received.
      def on_close; end # Fired when a client is disconnected.
      def on_error(error); end # Fired when error occurs.

      def initialize(*args)
        app, options = args[0], args[1]
        app, options = nil, app if app.is_a?(Hash)
        @options = DEFAULT_OPTIONS.merge(options || {})
        @app = app
      end

      def call(env)
        if(env['HTTP_CONNECTION'].to_s.downcase == 'upgrade' && env['HTTP_UPGRADE'].to_s.downcase == 'websocket')
          @env = env
          socket = env['async.connection']
          @conn = Connection.new(self, socket, :debug => !!@options[:websocket_debug])
          @conn.dispatch(env) ? async_response : failure_response
        elsif @app
          @app.call(env)
        else
          not_found_response
        end
      end

      def close_websocket
        if @conn
          @conn.close_websocket
        else
          raise WebSocketError, "WebSocket not opened"
        end
      end

      def send_data(data)
        if @conn
          @conn.send data
        else
          raise WebSocketError, "WebSocket not opened"
        end
      end

      protected

      def async_response
        [-1, {}, []]
      end

      def failure_response
        [ 400, { "Content-Type" => "text/plain" }, [ 'invalid data' ] ]
      end

      def not_found_response
        [ 404, { "Content-Type" => "text/plain" }, [ 'not found' ] ]
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
websocket-rack-0.1.2 lib/rack/websocket/application.rb