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

Goliath::WebSocket can't handle POST requests? #199

Closed
boutros opened this issue Aug 15, 2012 · 5 comments

Comments

@boutros
Copy link

@boutros boutros commented Aug 15, 2012

I can't get Goliath to handle POST or PUT requests with parameters when using the WebSocket class. Complains undefined method 'handler' in Goliath::Env

POST-requests without parameters appears to be working.

This code produces the error:

class App < Goliath::WebSocket
  use Goliath::Rack::Params

  # websocket handling:
  def on_open(env)
    env.logger.info("WS OPEN")
  end

  def on_message(env, msg)
    env.logger.info("WS MESSAGE: #{msg}")
  end

  def on_close(env)
    env.logger.info("WS CLOSED")
  end

  def on_error(env, error)
    env.logger.error error
  end

  # response routing and dispatch:
  def response(env)
    if env['PATH_INFO'] == '/ws'
      super(env)
    else
      case env['REQUEST_METHOD']
      when 'GET'
        [200, {}, "It's a GET!"]
      when 'POST'
        [200, {}, "IT's a POST from #{env.params['name']}"]
      end
    end
  end
end

I get the following error when doing a post request:

curl localhost:9000 -X POST -d name=bob
undefined method `handler' for #<Goliath::Env:0x0000000229e350>

If i sublacss from Goliath::API instead, it works fine.

@jpfuentes2

This comment has been minimized.

Copy link

@jpfuentes2 jpfuentes2 commented Aug 15, 2012

Websockets != HTTP. It does not support POST/PUT. You may want to do some research into what Websockets are and do.

Here is some good reading material:

http://en.wikipedia.org/wiki/WebSocket
http://www.websocket.org/aboutwebsocket.html

Specifically for your reference:

The WebSocket protocol was designed to work well with the existing Web infrastructure. As part of this design principle, the protocol specification defines that the WebSocket connection starts its life as an HTTP connection, guaranteeing full backwards compatibility with the pre-WebSocket world. The protocol switch from HTTP to WebSocket is referred to as a the WebSocket handshake.

If the server understands the WebSocket protocol, it agrees to the protocol switch through the Upgrade header.

At this point the HTTP connection breaks down and is replaced by the WebSocket connection over the same underlying TCP/IP connection. The WebSocket connection uses the same ports as HTTP (80) and HTTPS (443), by default.

@boutros

This comment has been minimized.

Copy link
Author

@boutros boutros commented Aug 15, 2012

Ah, thanks for clearing that up!

I though Goliath could handle both HTTP and Websockets at the same time.
So I guess I must make two different apps then, and connect them somehow..

@boutros boutros closed this Aug 15, 2012
@cedricfung

This comment has been minimized.

Copy link

@cedricfung cedricfung commented Sep 12, 2012

Just an alternative to use POST in Goliath::WebSocket http://vec.io/posts/use-warden-to-authenticate-in-ruby-goliath#toc-troubleshooting

@boutros

This comment has been minimized.

Copy link
Author

@boutros boutros commented Sep 18, 2012

Thanks vecio, I'll look into your solution.

I solved it by having two goliath processes - one to handle websockets, and another to handle http requests..

@denro

This comment has been minimized.

Copy link

@denro denro commented Nov 24, 2012

Thanks @Vecio, I solved this by implementing the following in my app, dunno actually what consequences this has but it seems to work 🌈

def on_body(env, data)
  if env.respond_to? :handler
    super env, data
  else
    env['rack.input'] = StringIO.new data
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.