我们正在尝试将DHH的简单Rails 5聊天示例部署到AWS上的单个自包含EC2实例.代码可在此处获取:https://github.com/HectorPerez/chat-in-rails5
我们使用Elastic Beanstalk来启动单个实例:
eb create dev-env -p “64bit Amazon Linux 2015.09 v2.0.4 running Ruby
2.2 (Puma)” –single -i t2.micro --envvars
SECRET_KEY_BASE=g5dh9cg614a37d4bdece9126b42d50d0ab8b2fc785daa1e0dac0383d6387f36b
Run Code Online (Sandbox Code Playgroud)
这是一个最小的安装,因此没有Elasticache,也没有负载均衡器.要在EC2实例上安装redis,我们添加了一个.ebextensions配置文件,如下所示:https://gist.github.com/KeithP/08b38189372b7fd241e5#file-ebextensions-redis-config ; Git提交和部署.
但是websocket不起作用:检查浏览器控制台,我们看到这个错误一遍又一遍地重复:
application-a57354de3399cd895ca366df9bd7316ab69e81d266b63be7d7be563ebc78ab9d.js:27
WebSocket connection to ‘ws://dev-env-y2e5dcrxqk.elasticbeanstalk.com/cable’ failed:
Error during WebSocket handshake: Unexpected response code: 404
Run Code Online (Sandbox Code Playgroud)
服务器production.log为每个"Finished/cable"呼叫显示2"Started GET/cable".ActiveCable没有DEBUG消息:
/var/app/containerfiles/logs/production.log
-------------------------------------
INFO -- : Processing by RoomsController#show as HTML
DEBUG -- : [1m[36mMessage Load (0.1ms)[0m [1m[34mSELECT "messages".* FROM "messages"[0m INFO -- : Rendered collection (0.0ms)
INFO -- : Rendered rooms/show.html.erb within layouts/application …
Run Code Online (Sandbox Code Playgroud) deployment amazon-web-services redis ruby-on-rails-5 actioncable
我正在尝试使用Rails 5 ActionCable构建一个消息传递应用程序,但我在JS控制台中遇到了上述错误.
ws://localhost:3002/cable' failed: Error during WebSocket handshake: Unexpected response code: 404
我正在使用Redis和Puma宝石.
我有一台Redis服务器在运行 7937:M 24 Jul 18:20:53.379 * The server is now ready to accept connections on port 6379
我用Puma启动Rails服务器:( rails s Puma -p 3002
这是否需要?)
config/cable.yml
看起来像这样......
development:
adapter: redis
url: redis://localhost:6379
test:
adapter: async
production:
adapter: redis
url: redis://localhost:6379/1
Run Code Online (Sandbox Code Playgroud)
当我进入rails c
并执行广播时:
2.2.4 :003 > ActionCable.server.broadcast("messages:1", text: "test")
[ActionCable] Broadcasting to messages:1: {:text=>"test"}
=> 0
Run Code Online (Sandbox Code Playgroud)
...你可以看到它返回0,所以它失败了.并且控制台错误消息看起来像......
WebSocket connection to 'ws://localhost:3002/cable' failed: WebSocket is closed before …
我在我的产品环境中连接 ActionCable 时遇到问题,并且相关问题还没有有效的解决方案。我在 Ubuntu 20.04 上使用 nginx+puma 设置和 Rails 6.1.3.2。我已经确认它redis-server
正在 port 上运行6379
,并且 Rails 正在作为生产运行。
这是我在日志中得到的内容:
I, [2021-05-25T22:47:25.335711 #72559] INFO -- : [5d1a85f7-0102-4d25-bd4e-d81355b846ee] Started GET "/cable" for 74.111.15.223 at 2021-05-25 22:47:25 +0000
I, [2021-05-25T22:47:25.336283 #72559] INFO -- : [5d1a85f7-0102-4d25-bd4e-d81355b846ee] Started GET "/cable/"[non-WebSocket] for 74.111.15.223 at 2021-05-25 22:47:25 +0000
E, [2021-05-25T22:47:25.336344 #72559] ERROR -- : [5d1a85f7-0102-4d25-bd4e-d81355b846ee] Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: close, HTTP_UPGRADE: )
I, [2021-05-25T22:47:25.336377 #72559] INFO -- : [5d1a85f7-0102-4d25-bd4e-d81355b846ee] Finished "/cable/"[non-WebSocket] for 74.111.15.223 …
Run Code Online (Sandbox Code Playgroud)