我正在使用Rails5为我的应用程序创建api提供程序,并且,我需要使用websocket。
然后,我为测试创建了一个非常简单的代码:
routes.rb
Rails.application.routes.draw do
mount ActionCable.server => '/cable'
get '/testbroad', to: 'messages#testbroad'
end
messages_channel.rb
class MessagesChannel < ApplicationCable::Channe
根据DHHs Rails5 ActionCable聊天示例,我将创建另一个包含对话和许多消息的示例:
rails g model conversation
class Conversation < ApplicationRecord
has_many :messages
end
rails g model message content:text conversation:references
查看/会话/show.html.erb
<h1>Conversation</h1>
<div id="messages">
&l
我有一个Ruby on Rails应用程序,它使用ActionCable进行实时客户端通信。我有ScoreChannel,它负责流式用户分数更新:
class ScoreChannel < ApplicationCable::Channel
def self.broadcast_name(user:)
"score_#{user.guid}"
end
def broadcast_name
self.class.broadcast_name(user: current_user)
end
def subscribed
stre
我不想让actioncable.I想让用户订阅像这样的PostChannel,
class PostNotificationsChannel < ApplicationCable::Channel
def subscribed
stream_from params[:post_id]
end
def unsubscribed
stop_all_streams
end
end
那我就有js了
/javascripts/channels/post_Nofications.js
$(document).on('turbolinks:load'
我是Rails 5的初学者,我正在尝试在我的应用程序中实现web套接字。首先,我试图复制并运行一个测试应用程序,我发现它无法工作,因为它不能发送和接收数据。永远不会调用received: (data) ->。我还观察到它永远不会连接到我的redis服务器(我用brew安装的)。我正在粘贴下面的所有代码。提前谢谢。
//room.coffee
App.room = App.cable.subscriptions.create "RoomChannel",
connected: ->
# Called when the subscription is ready f
我在actioncable中使用Rails 5,当我尝试发送任何抛出actioncable的东西时,我得到的是,我该怎么办?我想把这个项目推给Heroku。
NoMethodError in MessagesController#create
undefined method `fetch' for nil:NilClass
Extracted source (around line #37):
#35 # Also makes sure proper dependencies are required.
#36 def pubsub_adapter
*37
我正在尝试在我的纯api rails应用程序中配置Action Cable。在一个非常基本的设置之后,我尝试从我的Ember应用程序连接到服务器。它无法连接,在Rails控制台上,我看到以下错误条目:
Started GET "/cable" for 172.17.0.1 at 2017-12-20 10:17:26 +0000
Started GET "/cable/" [WebSocket] for 172.17.0.1 at 2017-12-20 10:17:26 Request origin not allowed: http://localhost:4
我正在尝试使用Capistrano将一个启用Action电缆的应用程序部署到VPS中。我正在使用美洲豹,Nginx和Redis (电缆)。经过几个障碍之后,我能够让它在当地的开发环境中工作。我使用的是默认的进程内/cable URL。但是,当我尝试将它部署到VPS时,我一直在JS-日志中得到以下两个错误:
Establishing connection to host ws://{server-ip}/cable failed.
Connection to host ws://{server-ip}/cable was interrupted while loading the page.
在
我正在使用Action Cable,想知道您如何检测同一用户的多个选项卡或连接是否打开并在必要时将其删除。我有一个下载任务执行多次,我希望它只执行一次。
class ListsChannel < ApplicationCable::Channel
def subscribed
stream_from "channel"
end
# # TODO code to detect multiple streams and delete them if tabs.
# def unsubscribed
我已经在我们的Rails应用程序上设置了一个websocket,并且能够连接和接收来自同一服务器的数据。
我现在的方法是创建一个套接字,如下所示:
class UsersChannel < ApplicationCable::Channel
def subscribed
stream_from "users_1"
end
def unsubscribed
end
end
然后我使用javascript打开与
new WebSocket('wss://domain.com/cable/users_1');
然后,我从一个页面广播并