首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Rails 7 ActionCable无法连接

Rails 7 ActionCable无法连接
EN

Stack Overflow用户
提问于 2022-03-04 23:50:12
回答 2查看 1.9K关注 0票数 1

我最近从Rails 6.1.4.6升级到7.0.2.2。通过这次升级,我从webpacker切换到导入带有链轮的地图。我的回购不包括涡轮增压或刺激,我现在也不想加入它们。因此,我重新添加了UJS,除了动作电缆功能测试之外,我的大多数测试都通过了.好像我连不上行动电缆。

任何帮助都将不胜感激!

Gemfile

代码语言:javascript
运行
复制
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby "3.0.2"

# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem "rails", "~> 7.0", ">= 7.0.2.2"
# The original asset pipeline for Rails [https://github.com/rails/sprockets-rails]
gem "sprockets-rails", "~> 3.4.2"
# Use postgresql as the database for Active Record
gem "pg", "~> 1.1"
# Use the Puma web server [https://github.com/puma/puma]
gem "puma", "~> 5.0"
# Use JavaScript with ESM import maps [https://github.com/rails/importmap-rails]
gem "importmap-rails", "~> 1.0"
# Build JSON APIs with ease [https://github.com/rails/jbuilder]
gem "jbuilder", "~> 2.0"
# Use Redis adapter to run Action Cable in production
gem "redis", "~> 4.0"
# Use Active Model has_secure_password [https://guides.rubyonrails.org/active_model_basics.html#securepassword]
gem "bcrypt", "~> 3.1.7"
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ]
# Reduces boot times through caching; required in config/boot.rb
gem "bootsnap", require: false
# Use SCSS for stylesheets
gem "sassc-rails", "~> 2.1.2"
# FontAwesome icons for sprockets and SASS
gem "font-awesome-sass", "~> 5.15.1"
# Payment management system
gem "stripe", "~> 5.45.0"
# Email management system
gem "sendgrid-ruby", "~> 6.6.1"
# Communications (voice/sms) system
gem "twilio-ruby", "~> 5.65.0"
# HTTP request gem
gem "faraday", "~> 1.10.0"
# Enable cross-origin AJAX
gem "rack-cors", "~> 1.1.1"
# Authentication framework
gem "devise", "~> 4.8.0"
# Efficient SQL importer
gem "activerecord-import", "~> 1.3.0"
# Session store backed by an Active Record class
gem "activerecord-session_store", "~> 2.0.0"
# Production background worker
gem "sidekiq", "~> 6.4.0"
# Extention for sidekiq cron jobs
gem "sidekiq-scheduler", "~> 3.1.1"
# Segment Analytics Gem
gem "analytics-ruby", "~> 2.4.0"

group :development, :test do
  # Debugging tool
  gem "pry", "~> 0.14.1"
  # Testing framework
  gem "rspec-rails", "~> 5.0.0"
  # Broswer webdriver for testing
  gem "selenium-webdriver", "~> 3.142", ">= 3.142.7"
end

group :development do
  # Add speed badges [https://github.com/MiniProfiler/rack-mini-profiler]
  gem "rack-mini-profiler", "~> 2.0"
  # Use console on exceptions pages [https://github.com/rails/web-console]
  gem "web-console", "~> 4.2.0"
  # Linter
  gem "rubocop-rails", require: false
end

group :test do
  # Testing frontend integration
  gem "capybara", "~> 3.36.0"
  # Testing coverage
  gem "simplecov", "~> 0.21.2", require: false
  # Disallow API calls during testing
  gem "webmock", "~> 3.14.0"
  # Make initial API call and save as fixtures
  gem "vcr", "~> 6.0.0"
  # Generate fixtures for testing
  gem "factory_bot_rails", "~> 6.2.0"
  gem "faker", "~> 2.19.0"
end

config/cable.yml

代码语言:javascript
运行
复制
development:
  adapter: async

test:
  adapter: test

production:
  adapter: redis
  url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>
  channel_prefix: my_app_name

config/scripes.rb

代码语言:javascript
运行
复制
mount ActionCable.server => "/cable"

config/config maps.rb

代码语言:javascript
运行
复制
pin "application", preload: true
pin "@rails/actioncable", to: "actioncable.esm.js"
pin_all_from "app/javascript/channels", under: "channels"

app/assets/config/plom.js

代码语言:javascript
运行
复制
//= link_tree ../images
//= link_directory ../stylesheets .scss
//= link_tree ../javascript .js
//= link_tree ../../../vendor/javascript .js
//= link application.css
//= link channels/consumer.js
//= link channels/index.js
//= link channels/messages_channel.js

app/javascripts/application.js

代码语言:javascript
运行
复制
import "@rails/actioncable"
import "channels"

app/javascripts/channels/index.js

代码语言:javascript
运行
复制
import "channels/market_channel"
import "channels/messages_channel"

app/javascripts/channels/app.app

代码语言:javascript
运行
复制
import { createConsumer } from "@rails/actioncable"

export default createConsumer()

app/javascripts/channels/messages_channels el.js

代码语言:javascript
运行
复制
import consumer from "channels/consumer"

consumer.subscriptions.create("MessagesChannel", {
  connected() {
    console.log('connected');
  },

  disconnected() {
    // Called when the subscription has been terminated by the server
  },

  received(data) {
    ... js code ...
  }
});

显示与/cable连接的日志

代码语言:javascript
运行
复制
Started GET "/cable" for ::1 at 2022-03-09 08:58:22 -0700
Started GET "/cable/" [WebSocket] for ::1 at 2022-03-09 08:58:22 -0700
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
  User Load (0.4ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 2], ["LIMIT", 1]]
  ↳ app/channels/application_cable/connection.rb:14:in `find_verified_user'
Registered connection (Z2lkOi8vY2FsZW5kYXJpemUvVXNlci8y)
MessagesChannel is transmitting the subscription confirmation
MessagesChannel is streaming from messages:2
MarketChannel is transmitting the subscription confirmation
MarketChannel is streaming from market_channel

开发控制台中WS选项卡中没有/cable连接

头中的Importmap脚本

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-03-09 22:08:35

找出问题是因为我有两个applications.js文件。一个在app/assets/javascripts/,另一个在app/javascript。由于我的清单指向那里,链轮正在为我的资产版本application.js服务。我调整了清单,删除了辅助application.js,一切都正常。

票数 1
EN

Stack Overflow用户

发布于 2022-10-20 11:09:48

我犯了个小错误!我的错误是:

代码语言:javascript
运行
复制
GET http://localhost:3000/assets/step_channel.js net::ERR_ABORTED 404 (Not Found) 

是在app/javascript/channels/index.js引起的

解决方案非常简单,它在app/javascript/channels/index.js中为导入添加了通道。

代码语言:javascript
运行
复制
import "./channels/step_channel.js"

我的manifest.js看起来像:

代码语言:javascript
运行
复制
//= link_tree ../images
//= link_directory ../stylesheets .css
//= link_tree ../../javascript .js
//= link tailwind.css
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71358357

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档