websocket-rails, websocket handshake error

2019-08-31 01:31发布

问题:

I followed the docs on the websocket-rails github wiki page, but couldn't overcome this difficulty, I get this error with chrome:

WebSocket connection to 'ws://0.0.0.0:3000/websocket' failed: Error during WebSocket handshake: Unexpected response code: 301

I installed the websocket-rails gem, generated the install, I set successfully config.middleware.delete Rack::Lock in development.rb; I'm using Rails 4.

I got in the view:

<script type="text/javascript">
  var dispatcher = new WebSocketRails('0.0.0.0:3000/websocket');
</script>

I got in my chat_controller.rb:

class ChatController < WebsocketRails::BaseController
   def initialize_session
      puts "Session Initialized"
   end

   def user_connected
     puts 'user connected'
   end
end

in my events.rb:

WebsocketRails::EventMap.describe do
  subscribe :client_connected, :to => ChatController, :with_method => :user_connected
end

As you can see, my goal is to display "Session Initialized", and "user connected", in my thin server console each time that someone goes on a page. Between, I run the server with bundle exec thin start, but I got a javascript error instead (websocket connection failed error 301).

回答1:

Thanks to the members of the Websocket Rails IRC, I found out that in my route, I was appending the locale before every path, so it coudln't find it.

Check your routes.rb if someone hit that issue too !