我正在尝试在本地配置 Rails 7.1.3/Turbo 2.0.5 应用程序,以尝试
broadcasts_refreshes
建立一个简单的模型。在尝试 turbo_stream_from
此模型的视图模板上,我收到 websocket 错误,表明它正在尝试连接。
以下是我遇到的错误:
# in the browser, with or without the above config.action_cable.url
WebSocket connection to 'ws://localhost:3000/cable' failed: There was a bad response from the server.
# in the terminal window, while rails server is running
Started GET "/cable" for ::1 at 2024-04-18 01:14:20 -0700
Started GET "/cable" [WebSocket] for ::1 at 2024-04-18 01:14:20 -0700
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
WebSocket error occurred: undefined method `write_nonblock' for nil
::1 - - [18/Apr/2024:01:14:20 PDT] "GET /cable HTTP/1.1" -1 0
WebSocket error occurred: undefined method `write_nonblock' for nil
出现以上错误信息是否说明认证未通过?
如果 Turbo 创建自己的通道,它是否也会创建自己的 websocket 连接并单独处理自己的身份验证逻辑?或者它是否使用
app/channels/application_cable/connection.rb
文件中我的应用程序的 ActionCable 连接身份验证逻辑? (我的 connection.rb
文件当前为空,因为我们不将 ActionCable 用于其他任何用途。)
Rails 是否会通过
rails s
自动启动 ActionCable 的 redis 服务器?
routes.rb
:mount ActionCable.server => "/cable"
config/environments/development.rb
:config.action_cable.url = "ws://localhost:3000/cable"
config.action_cable.allowed_request_origins = [/http:\/\/*/,/https:\/\/*/]
turbo-rails
版本2.0.5redis
已安装。我已经跑了./bin/rails turbo:install:redis
cable.yml
为开发中的redis配置,感谢这个答案。development:
adapter: redis
url: redis://localhost:6379/1
broadcasts_refreshes
class FieldSlip < ApplicationRecord
broadcasts_refreshes
turbo_stream_from
:<%= turbo_stream_from(:field_slip, dom_id(field_slip)) %>
<%= render(partial: "field_slips/row",
locals: { field_slip: field_slip }) %>
模板端的所有内容似乎都正常工作,Turbo 正在生成
turbo-cable-stream-source
标签,但是 我注意到没有 connected
属性:
<turbo-cable-stream-source channel="Turbo::StreamsChannel" signed-stream-name="ImZpZWxkX3NsaXBfam9iX3RyYWNrZXI6ZmllbGRfc2xpcF9qb2JfdHJhY2tlcl8xIg==--bcf536df9f893f646adb2ce946d4d8ffa166eb297cca1a5c7fa093c77b0da878"></turbo-cable-stream-source>
显然,websocket 不起作用。我错过了什么?
我也有同样的问题。我注意到它不仅影响了流媒体,还影响了所有 Hotwire 功能。我也尝试了你概述的所有事情。
为我解决这个问题的方法是将以下标签添加到我的
application.html.erb
的头部:
<%= turbo_include_tags %>