首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Action Cable无法与rails 5配合使用

Action Cable无法与rails 5配合使用
EN

Stack Overflow用户
提问于 2017-04-19 21:47:44
回答 1查看 3.1K关注 0票数 0

我是Rails 5的初学者,我正在尝试在我的应用程序中实现web套接字。首先,我试图复制并运行一个测试应用程序,我发现它无法工作,因为它不能发送和接收数据。永远不会调用received: (data) ->。我还观察到它永远不会连接到我的redis服务器(我用brew安装的)。我正在粘贴下面的所有代码。提前谢谢。

代码语言:javascript
复制
//room.coffee

App.room = App.cable.subscriptions.create "RoomChannel",
connected: -> 
# Called when the subscription is ready for use on the server 
disconnected: -> 
# Called when the subscription has been terminated by the server 

received: (data) -> 
alert(data['message'])
speak: (message) -> 
@perform 'speak', message: message

room_chanel.rb

代码语言:javascript
复制
# Be sure to restart your server when you modify this file. Action 
Cable runs in a loop that does not support auto reloading.
class RoomChannel < ApplicationCable::Channel
def subscribed
 stream_from "room_channel"
end

def unsubscribed
  # Any cleanup needed when channel is unsubscribed
end

def speak(data)
  ActionCable.server.broadcast "room_channel", message: data['message']
end
end

Gemfile

代码语言:javascript
复制
source 'https://rubygems.org'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
  "https://github.com/#{repo_name}.git"
end
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.2'

# Use postgresql as the database for Active Record
gem 'sqlite3'
# Use Puma as the app server
gem 'puma'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

gem 'redis'

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5.x'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platform: :mri
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '~> 3.0.5'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

cable.yml

代码语言:javascript
复制
# Action Cable uses Redis by default to administer connections, channels, and sending/receiving messages over the WebSocket.
production:
  adapter: redis
  url: redis://localhost:6379/1

development:
  adapter: redis
  url: redis://localhost:6379/1

test:
  adapter: redis
  url: redis://localhost:6379/1

cable.coffee

代码语言:javascript
复制
# Action Cable provides the framework to deal with WebSockets in Rails.
    # You can generate new channels where WebSocket features live using the rails generate channel command.
    #
    # Turn on the cable connection by removing the comments after the require statements (and ensure it's also on in config/routes.rb).
    #
    #= require action_cable
    #= require_self
    #= require_tree ./channels
    #
    @App ||= {}
    App.cable = ActionCable.createConsumer()
EN

回答 1

Stack Overflow用户

发布于 2017-04-21 16:19:59

您可以尝试在不使用Redis的情况下使用Action Cable (因为Action Cable不一定需要Redis ):

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

test:
  adapter: async

production:
  adapter: redis
  url: redis://localhost:6379/1

但这可能不是问题的根源。您的room.coffee缺少一些speak-calling代码。将类似以下内容添加到room.coffee中:

代码语言:javascript
复制
$ ->
  $('#send_form').submit (event)->
    App.room.speak event.currentTarget['message'].value
    event.currentTarget['message'].value = ""
    event.preventDefault()
    false

这是一个模板:

代码语言:javascript
复制
<div class="jumbotron">
  <form id="send_form">
    <label>Message:</label>
    <input id="message" type="text" data-behavior="room_speaker">
    <br> 
    <input type="submit" data-behavior="room_speaker">
  </form>
</div>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43497644

复制
相关文章

相似问题

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