ruby-on-rails - 如何在 rails/actioncable 中向除发件人以外的所有客户端发送消息?

标签 ruby-on-rails ruby websocket actioncable

在 socket.io 中,你可以向除发送者之外的所有客户端发送消息,例如:

socket.broadcast.emit('user connected');

但是在 rails/actioncable 中,该怎么做呢?

class BoardChannel < ApplicationCable::Channel
   def subscribed
     stream_from "board:#{params[:board]}"
   end

   def speak
     # client will call @perform('speak')
     result = do_something()
     # how to send 'result' to all client except sender?
   end
 end

最佳答案

我整个下午都在为这个问题烦恼。都准备放弃了,刚躺在床上,脑子里灵光一闪,灵机一动! ! !登录分享

 class BoardChannel < ApplicationCable::Channel
   def subscribed
     stream_from "board:#{params[:board]}"
     stream_from "global_stream"
   end
 end

然后,当你想广播所有用户时,你可以:

ActionCable.server.broadcast "global_stream", "some messages"

当然也可以给特殊用户广播。你可以:

ActionCable.server.broadcast "board:#{params[:board]}", "some messages"

完美!!!

关于ruby-on-rails - 如何在 rails/actioncable 中向除发件人以外的所有客户端发送消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38892367/

相关文章:

php - 使用 AJAX/PHP 将其他网站表格中的内容插入到我的表单中?

java - 从字符串模式中提取城市名称的正则表达式

Ruby:有没有 "knows"英文不规则动词的词干分析器?

Python heroku 为socket.io 聊天应用程序配置procfile Gunicorn + gevent |运行时错误: You need to use the gevent-websocket server

ruby - 如何测试文件操作

ruby-on-rails - 更改 faker gem 电话号码格式

c++ - WebSocket,解码数据帧(c++)

ios - Rails 6提示在手机上下载

ruby-on-rails - 如何限制用户在未登录的情况下通过电子邮件链接进行操作?

mysql - 基于联接和自联接设置列值 MySql Rails