2017-04-19 4 views
0

Ich bin ein Anfänger in Rails 5 und ich versuche, Web-Sockets in meiner App zu implementieren. Ich versuche zunächst, eine Test-App zu kopieren und auszuführen, und ich stelle fest, dass es nicht funktioniert, da es die Daten nicht sendet und empfängt. received: (data) -> wird nie aufgerufen. Ich habe auch beobachtet, dass es auch nie mit meinem Redis-Server verbunden ist (den ich mit brew installiert habe). Ich füge den ganzen folgenden Code ein. Danke im Voraus.Aktion Kabel funktioniert nicht mit Schienen 5

//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

# 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

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

# 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

# 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() 

Antwort

0

Sie könnten versuchen, Action-Kabel ohne Redis zu verwenden (da Redis nicht streng Aktions Kabel erforderlich):

development: 
    adapter: async 

test: 
    adapter: async 

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

Aber das ist wahrscheinlich nicht der Ursprung des Problems. Ihrem room.coffee fehlt etwas speak -calling code. In den room.coffee etwas wie folgt aus:

$ -> 
    $('#send_form').submit (event)-> 
    App.room.speak event.currentTarget['message'].value 
    event.currentTarget['message'].value = "" 
    event.preventDefault() 
    false 

Hier ist eine Vorlage:

+0

Ich habe das versucht. Es hat nicht funktioniert. –

+0

Versuchen Sie, 'gem' pg'' anstelle von 'gem' sqlite3'' (und Bündel installieren) Siehe auch: https://www.pluralsight.com/guides/ruby-ruby-on-rails/ creating-a-chat-using-rails-action-cable – prograils

+0

Ich werde das versuchen, aber was ist der Grund der Einstellung 'pg' anstelle von 'sqlite3' –

Verwandte Themen