Sha256: 3c9eccd3ef4a9d7181f3fe46ed1522083f62d1118f92dc63acb39986b2a3f3ae

Contents?: true

Size: 1.78 KB

Versions: 80

Compression:

Stored size: 1.78 KB

Contents

module WebSocket
  class Driver

    class Server < Driver
      EVENTS = %w[open message error close]

      def initialize(socket, options = {})
        super
        @http = HTTP::Request.new
        @delegate = nil
      end

      def env
        @http.complete? ? @http.env : nil
      end

      def url
        return nil unless e = env

        url  = "ws://#{e['HTTP_HOST']}"
        url << e['PATH_INFO']
        url << "?#{e['QUERY_STRING']}" unless e['QUERY_STRING'] == ''
        url
      end

      %w[add_extension set_header start frame text binary ping close].each do |method|
        define_method(method) do |*args, &block|
          if @delegate
            @delegate.__send__(method, *args, &block)
          else
            @queue << [method, args, block]
            true
          end
        end
      end

      %w[protocol version].each do |method|
        define_method(method) do
          @delegate && @delegate.__send__(method)
        end
      end

      def parse(chunk)
        return @delegate.parse(chunk) if @delegate

        @http.parse(chunk)
        return fail_request('Invalid HTTP request') if @http.error?
        return unless @http.complete?

        @delegate = Driver.rack(self, @options)
        open

        EVENTS.each do |event|
          @delegate.on(event) { |e| emit(event, e) }
        end

        emit(:connect, ConnectEvent.new)
      end

      def write(buffer)
        @socket.write(buffer)
      end

    private

      def fail_request(message)
        emit(:error, ProtocolError.new(message))
        emit(:close, CloseEvent.new(Hybi::ERRORS[:protocol_error], message))
      end

      def open
        @queue.each do |method, args, block|
          @delegate.__send__(method, *args, &block)
        end
        @queue = []
      end
    end

  end
end

Version data entries

80 entries across 52 versions & 10 rubygems

Version Path
kinetic_sdk-5.0.9 gems/websocket-driver-0.6.5-java/lib/websocket/driver/server.rb
kinetic_sdk-5.0.9 gems/websocket-driver-0.6.5/lib/websocket/driver/server.rb
kinetic_sdk-5.0.8 gems/websocket-driver-0.6.5/lib/websocket/driver/server.rb
kinetic_sdk-5.0.8 gems/websocket-driver-0.6.5-java/lib/websocket/driver/server.rb
kinetic_sdk-5.0.7 gems/websocket-driver-0.6.5-java/lib/websocket/driver/server.rb
kinetic_sdk-5.0.7 gems/websocket-driver-0.6.5/lib/websocket/driver/server.rb
kinetic_sdk-5.0.6 gems/websocket-driver-0.6.5-java/lib/websocket/driver/server.rb
kinetic_sdk-5.0.6 gems/websocket-driver-0.6.5/lib/websocket/driver/server.rb
kinetic_sdk-5.0.5 gems/websocket-driver-0.6.5/lib/websocket/driver/server.rb
kinetic_sdk-5.0.5 gems/websocket-driver-0.6.5-java/lib/websocket/driver/server.rb
argon-1.3.1 vendor/bundle/ruby/2.7.0/gems/websocket-driver-0.7.1/lib/websocket/driver/server.rb
symbolic_enum-1.1.5 vendor/bundle/ruby/2.7.0/gems/websocket-driver-0.7.1/lib/websocket/driver/server.rb
kinetic_sdk-5.0.4 gems/websocket-driver-0.6.5/lib/websocket/driver/server.rb
kinetic_sdk-5.0.4 gems/websocket-driver-0.6.5-java/lib/websocket/driver/server.rb
kinetic_sdk-5.0.3 gems/websocket-driver-0.6.5/lib/websocket/driver/server.rb
kinetic_sdk-5.0.3 gems/websocket-driver-0.6.5-java/lib/websocket/driver/server.rb
kinetic_sdk-5.0.2 gems/websocket-driver-0.6.5/lib/websocket/driver/server.rb
kinetic_sdk-5.0.2 gems/websocket-driver-0.6.5-java/lib/websocket/driver/server.rb
kinetic_sdk-5.0.2.beta.4 gems/websocket-driver-0.6.5-java/lib/websocket/driver/server.rb
kinetic_sdk-5.0.2.beta.4 gems/websocket-driver-0.6.5/lib/websocket/driver/server.rb