- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
在我的 Ruby on Rails 项目中,我有一个 Tickets MVC。用于编辑工单(例如 ID 为 5 的工单)的典型页面是 /tickets/5/edit
。
在另一个页面上,我有一个简单的表格,我想实时显示有多少用户正在查看每张票(即显示有多少用户在 /tickets/1/edit
, tickets/2/edit
....
每当用户登录并前往 /tickets/:id/edit
查看票证时,我都有一个 coffeescript ticket_notifications.coffee
看起来像:
$ ->
if $('body').attr('data-controller') == 'tickets' && $('body').attr('data-action') == 'edit'
ticketId = document.getElementById('ticket_id').value
App.ticket_notifications = App.cable.subscriptions.create {channel: "TicketNotificationsChannel", ticket_id: ticketId},
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) ->
我的 app/channels/ticket_notifications_channel.rb
看起来像:
class TicketNotificationsChannel < ApplicationCable::Channel
def subscribed
# stream_from "some_channel"
if current_user&.account
stream_from "ticket_notifications_channel_#{current_user.account_id}"
if params[:ticket_id]
if Ticket.find(params[:ticket_id]).account == current_user.account
ActionCable.server.broadcast "ticket_notifications_channel_#{current_user.account_id}",
{stuff: "Agent #{current_user.email} is viewing ticket #{params[:ticket_id]}"}
end
end
end
end
def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
end
前端表看起来像(我用的是Slim,但是跟erb差不多):
table.table.table-hover
thead
tr
th
| Ticket #
th
| Short Code
th
| Mobile Number
th
| Number of Tickets
th
| Updated At
th
| Viewers
tbody
- unless @tickets.empty?
- current_time = Time.now
- @tickets.each do |tkt|
- tkt_updated_at = tkt.updated_at
tr.m-unread.m-tr-clickable data-href=edit_ticket_path(id: tkt.id)
td
b #{tkt.id}
td
b #{tkt.short_code}
td
b #{tkt.mobile_number}
td
- if last_message = tkt.messages&.last&.body
b #{last_message[0..60]}
td
- if (current_time-tkt_updated_at) <= time_period
b #{time_ago_in_words(tkt_updated_at)} ago
- else
b #{tkt_updated_at.in_time_zone.strftime('%b %e %H:%M:%S %Y')}
td
b Count how many subscriptions to tickets_notifications channel with params tkt.id here.
谢谢。
最佳答案
您可以查看以下问题的答案:
How do I find out who is connected to ActionCable?
基本上你必须做。
Redis.new.pubsub("channels", "action_cable/*")
这将为您提供所有活跃的 pubsub channel 。
您还可以在创建新订阅时提供 userId 以及 ticketId,例如:
App.cable.subscriptions.create {channel: "TicketNotificationsChannel", ticket_id: ticketId, user_id: userId}
现在 Redis.new.pubsub("channels", "action_cable/*")
将按照以下方式为您提供所有事件订阅
[“action_cable/Z2lkOi8vYXBpL1VzZXIvMTUwMjIz”,“action_cable/Z2lkOi8vYXBpL1VzZXIvMTUwNTc0”]
对上面提到的字符串进行 Base64.decode
将以下列方式输出 "gid://api/User/150223/Ticket/1"
。然后,您可以在此输出上构建一些逻辑,为您提供特定工单 ID 的所有用户计数。
关于ruby-on-rails - Actioncable 计数订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52807289/
我的应用程序中有两个设计身份验证模型,并且想要在它们之间创建聊天。有人可以帮我为用户编写连接吗?以下是我所拥有的。我想检查是否可以让两个连接根据不同用户的个人登录拒绝其连接。如有任何帮助,我们将不胜感
根据 Sitepoint's tutorial,我在我的 RoR 应用程序中使用 ActionCable,旨在用作 Angular 色扮演的聊天室。 .我已经设置好了,并且消息如预期的那样在创建消息时
我看过ActionCable.server.open_connections_statistics、ActionCable.server.connections.length、ActionCable.
我正在尝试使用我的 rails 5 应用程序设置 ActionCables,我已经运行了 rails g channel Posts,现在我的服务器每隔几秒就会重置一次,并出现此错误: Started
这在 Rails 中没有记录,但 ActionCable 有一个名为 PeriodicTimers 的模块 https://github.com/rails/rails/blob/master/act
我一直在尝试 ActionCable 并想知道它将尝试打开多少个数据库连接。是每个实例一个还是每个消费者连接一个?让 100k 消费者为每个消费者打开一个数据库连接会破坏数据库。 最佳答案 这取决于工
我无法手动关闭我的套接字。 只有当我关闭浏览器中的选项卡时它才会退出。 Finished "/cable/" [WebSocket] for 127.0.0.1 at 2016-10-29 16:55
我正在关注迈克尔·哈特尔 tutorial关于 Rails 5 ActionCable,建立一个简单的聊天,我的 create 内部出现问题行动在 MessageController : def
在我的项目中,我有一个模型categories,它具有一个详细 View (显示)。该 View 包含应使用actioncable进行实时更新的数据。 View 的内容/数据对于理解问题并不重要。 一
我使用 ActionCable 创建了以下内容,但无法接收任何正在广播的数据。 评论 channel : class CommentsChannel < ApplicationCable::Chann
我正在尝试让 ActionCable 在子域上工作。 问题是,一旦我更改以下行 config.action_cable.mount_path = '/' 该应用程序不再运行。但 ActionCable
我正在尝试使用Action Cable创建一个简单的类似于聊天的应用程序(计划扑克应用程序)。我对术语,文件层次结构以及回调如何工作感到有些困惑。 这是创建用户 session 的操作: class
我无法使用 docker 在我的 ubuntu 服务器上正确运行 actioncable 这似乎是一种设备验证错误,这是我的终端日志 There was an exception - NoMeth
我最近一直在尝试为 Squash 比赛创建一个实时计分系统。我已经设法将 ActionCable 与 Rails 5 一起使用来自动更新页面上的分数,但我想知道是否可以告诉 Rails 在满足特定条件
我正在使用 Ruby on Rails 5 作为后端框架,实际上是用于 websocket 连接的电缆和前端 Android。我按照 https://github.com/rails/actionca
有什么方法可以通过 ActionCable 传递文件吗?我正在尝试使用 Paperclip 进行上传。 我的 JavaScript 是 return $('#new_messag
我正在创建一个 ruby 应用程序,我正在尝试实现操作电缆,当用户发布帖子时,我希望为所有用户更新帖子索引。在我的 channels/posts.js 中我有: App.posts = App.c
我正在 rails 5 中实现 action cable,它在本地运行良好,但在暂存和生产中,在将消息发送到 action cable rails logger 之后。 [ActionCable] B
我有一个 Rails 5 应用程序,它使用 Action Cable 来实现 websocket 功能。 在我的开发环境中,一切都按预期进行,浏览器客户端成功连接到 Action Cable chan
在我的 Ruby on Rails 项目中,我有一个 Tickets MVC。用于编辑工单(例如 ID 为 5 的工单)的典型页面是 /tickets/5/edit。 在另一个页面上,我有一个简单的表
我是一名优秀的程序员,十分优秀!