Sha256: 879359523f5b038e746f1854187eeaa5ab09621abbd5e607a8879b494cf1a7ac

Contents?: true

Size: 1.8 KB

Versions: 23

Compression:

Stored size: 1.8 KB

Contents

module WebSocket
  class Driver

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

      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

23 entries across 23 versions & 8 rubygems

Version Path
websocket-driver-0.7.7-java lib/websocket/driver/server.rb
websocket-driver-0.7.7 lib/websocket/driver/server.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/websocket-driver-0.7.6/lib/websocket/driver/server.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/websocket-driver-0.7.6/lib/websocket/driver/server.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/websocket-driver-0.7.6/lib/websocket/driver/server.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/websocket-driver-0.7.6/lib/websocket/driver/server.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/websocket-driver-0.7.6/lib/websocket/driver/server.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/websocket-driver-0.7.6/lib/websocket/driver/server.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/websocket-driver-0.7.5/lib/websocket/driver/server.rb
websocket-driver-0.7.6-java lib/websocket/driver/server.rb
websocket-driver-0.7.6 lib/websocket/driver/server.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/websocket-driver-0.7.5/lib/websocket/driver/server.rb
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/websocket-driver-0.7.5/lib/websocket/driver/server.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/websocket-driver-0.7.5/lib/websocket/driver/server.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/websocket-driver-0.7.5/lib/websocket/driver/server.rb
websocket-driver-0.7.5-java lib/websocket/driver/server.rb
websocket-driver-0.7.5 lib/websocket/driver/server.rb
websocket-driver-0.7.4-java lib/websocket/driver/server.rb
websocket-driver-0.7.4 lib/websocket/driver/server.rb
websocket-driver-0.7.3-java lib/websocket/driver/server.rb