如何在rails/actioncable中向除发件人以外的所有客户端发送消息? [英] how to send message to all client except sender in rails/actioncable?

查看:28
本文介绍了如何在rails/actioncable中向除发件人以外的所有客户端发送消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

in socket.io, you can send message to all client except sender like:

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

但是在 rails/actioncable 中,如何做到这一点?

but in rails/actioncable, how to do that?

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

推荐答案

我一下午都在担心这个问题.一切都准备放弃了,只是躺在床上,脑子里闪过一个解决办法!!!登录分享

I have been worrying about this problem all afternoon. All ready to give up, just lying in bed, my mind flashed a solution it works! ! ! Sign in to share

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

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

Then, when you want to broadcast all users, you can:

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

当然,您也可以向特殊用户广播.你可以:

Of course, you can also broadcast to special users. you can:

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

完美!!!

这篇关于如何在rails/actioncable中向除发件人以外的所有客户端发送消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆