Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using with already parsed data #96

Open
etm opened this issue Jan 31, 2013 · 1 comment
Open

using with already parsed data #96

etm opened this issue Jan 31, 2013 · 1 comment

Comments

@etm
Copy link

etm commented Jan 31, 2013

Currently Connection#dispatch relies on raw http input. When using this in an environment where the raw http input is already parsed (and thus not easily available) (think vanilla rack/thin app), things become complicated.

My solution:

  • i created a class WebSocketParserData, which allows for the #match used in Connection#dispatch
  • I monkey patched Handshake to include a #receive_data that does no parsing
  • I wrote my own Connection class (which inherits from your connection) and call the standard #dispatch with the already parsed data.
class WebSocketParserData
  attr_accessor :headers, :request_path, :query_string, :http_method, :body, :request_url
  def match(what)
    @body =~ what
  end
  def upgrade?
    true
  end
end

module EventMachine
  module WebSocket
    class Handshake
      def receive_data(data)
        @parser = data
        @headers = data.headers
        process(@headers, data.body)
      end
    end
  end
end

Is there a simpler way? If no, consider adding functionality to Connection#dispatch that detects difference between raw data, and pre-parsed data (as i did through the introduction of WebSocketParserData) and add a Handshake#receive_parsed_data in this case.

@mloughran
Copy link
Collaborator

I agree it would be nice if the code was abstracted such that running behind e.g. rack was easy and a documented API. I may give this a go at some point, but meanwhile if you can see a clean way of doing this I'm very open to a pull request :)

Goliath implemented on top of the previous implementation and it would be worth working out how that should be updated to work with 0.4 at the same time. See https://github.com/postrank-labs/goliath/blob/master/lib/goliath/websocket.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants