如何向每个连接发送消息

时间:2016-10-01 07:21:28

标签: ruby-on-rails ruby websocket ruby-on-rails-5 actioncable

我知道在ActionCable中ping是这样的:

def beat
  transmit type: ActionCable::INTERNAL[:message_types][:ping], message: Time.now.to_i
end

但是当我尝试在连接上传输时,只有一个连接用transmit来获取我的消息?

def connect
  self.uuid = SecureRandom.uuid
  transmit({'title': 'players_online', 'message': 'A new user has been subscribed'})
end

1 个答案:

答案 0 :(得分:1)

实际上很容易:

ActionCable.server.connections.each do |connection|
  connection.transmit({'title': 'players_online', 'message': ActionCable.server.connections.size})
end