ruby-on-rails – 将ActionCable连接到不同的主机
我正在运行rails 5应用程序作为后端服务器,以及用于前端应用程序的ember应用程序.它们是托管在两个不同域上的两个独立应用程序 – 例如backend.dev和frontend.dev
rails应用程序在app / channels / application_cable / connection.rb中有一个简单的连接类,如下所示: module ApplicationCable class Connection < ActionCable::Connection::Base def connect Rails.logger.debug("env: #{env.inspect}") Rails.logger.info("cookies.signed: #{cookies.signed.inspect}") end end end 我在app / channels / application_cable / channel.rb上有一个简单的基本通道类,其中包含以下内容: module ApplicationCable class Channel < ActionCable::Channel::Base end end 在app / channels / events_channel.rb上该类的单个实现: class EventsChannel < ApplicationCable::Channel def subscribed Rails.logger.debug("env: #{env.inspect}") Rails.logger.info("cookies.signed: #{cookies.signed.inspect}") stream_from 'events' end end 在余烬方面,我正在使用ember-cable软件包.我通过扩展控制器类使用以下内容在我的前端设置我的消费者: cableService: Ember.inject.service('cable'),setupConsumer: Ember.on('init',function() { let service = this.get('cableService'); let consumer = service.createConsumer(`ws://backend.dev`); let channel = 'EventsChannel'; consumer.subscriptions.create(channel,{ disconnected() { Ember.debug(`${channel}#disconnected`); },connected() { Ember.debug(`${channel}#connected`); }, 我很确定我的消费者设置正确,因为当我将以下输出发送到我的js控制台时,我看到了一些调试输出: DEBUG: EventsChannel#disconnected 但是,我也在控制台中看到一个奇怪的错误: WebSocket connection to 'ws://backend.dev/' failed: Error during WebSocket handshake: Unexpected response code: 200 我不知道这里的响应代码错误是什么,并且我的rails应用程序中没有记录任何内容.我还需要设置哪些其他功能才能跨域运行actioncable吗?知道200响应代码在这里意味着什么吗? 解决方法
试试这个:
# routes.rb Rails.application.routes.draw do # your code mount ActionCable.server => '/cable' end 然后在你的应用中: let consumer = service.createConsumer(`ws://backend.dev/cable`); 如果您遇到握手问题,则解决方案很少: >检查您的前端应用程序是否与协议07或更新版本兼容. config.action_cable.disable_request_forgery_protection = true (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |