Websockets with Rails(Puma) - WebSocket 握手期间出错:意外响应代码:200 [英] Websockets with Rails(Puma) - Error during WebSocket handshake: Unexpected response code: 200
问题描述
我正在尝试在 Rails4.1 应用程序中使用 websocket
I am trying to use websocket in my Rails4.1 application
以下是一些相关的代码片段:
Here are some relevant code snippets:
宝石文件:
gem 'websocket-rails'
gem 'puma'
development.rb
development.rb
config.middleware.delete Rack::Lock
我在本地启动服务器:
bundle exec puma -p 3000
在 chrome 控制台中,我看到一个连接错误:
In the chrome console I see a connection error:
new WebSocketRails('localhost:3000/post/hello', true);
WebSocket connection to 'ws://localhost:3000/post/hello' failed: Error during WebSocket handshake: Unexpected response code: 200
任何人都可以帮助我在 Rails 中本地使用 Web 套接字需要做什么吗?
Can anyone help with what I need to do to use web sockets locally in Rails?
更新 1
我尝试按照 当我在 Unicorn 服务器上运行时,Websockets 在我的 Rails 应用程序中不起作用,但在瘦服务器上运行但它没有帮助
I tried adding following as per Websockets not working in my Rails app when I run on Unicorn server, but works on a Thin server but it did not help
初始化程序/eventmachine.rb
initializers/eventmachine.rb
Thread.new { EventMachine.run } unless EventMachine.reactor_running? && EventMachine.reactor_thread.alive
推荐答案
在您的控制台中试试这个:
Try this in your console:
window.dispatcher = new WebSocketRails window.document.location.host + '/websocket'
您可以在 config/events.rb 文件以及用于处理事件的任何控制器中配置您想要执行的其余操作
You configure the rest of what you want to do in the config/events.rb file and whatever controllers you use to handle the events
这篇关于Websockets with Rails(Puma) - WebSocket 握手期间出错:意外响应代码:200的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!