2015-12-09 13:09:37 +00:00
|
|
|
class Sessions::Event::Broadcast < Sessions::Event::Base
|
|
|
|
|
2018-11-02 17:42:57 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
Event module to broadcast messages to all client connections.
|
|
|
|
|
|
|
|
To execute this manually, just paste the following into the browser console
|
|
|
|
|
|
|
|
App.WebSocket.send({event:'broadcast', recipient: { user_id: [1,2,3]}, data: {some: 'key'}})
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-12-09 13:09:37 +00:00
|
|
|
def run
|
|
|
|
|
|
|
|
# list all current clients
|
|
|
|
client_list = Sessions.list
|
2017-10-01 12:25:52 +00:00
|
|
|
client_list.each do |local_client_id, local_client|
|
2015-12-09 13:09:37 +00:00
|
|
|
if local_client_id == @client_id
|
2020-11-27 09:49:36 +00:00
|
|
|
log 'info', 'do not send broadcast to it self'
|
2015-12-09 13:09:37 +00:00
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
# broadcast to recipient list
|
|
|
|
if @payload['recipient']
|
2017-05-30 14:06:17 +00:00
|
|
|
if @payload['recipient'].class != Hash && @payload['recipient'].class != ActiveSupport::HashWithIndifferentAccess && @payload['recipient'].class != ActionController::Parameters
|
2017-04-17 11:03:16 +00:00
|
|
|
log 'error', "recipient attribute isn't a hash (#{@payload['recipient'].class}) '#{@payload['recipient'].inspect}'"
|
2016-01-15 17:22:57 +00:00
|
|
|
elsif !@payload['recipient'].key?('user_id')
|
|
|
|
log 'error', "need recipient.user_id attribute '#{@payload['recipient'].inspect}'"
|
|
|
|
elsif @payload['recipient']['user_id'].class != Array
|
|
|
|
log 'error', "recipient.user_id attribute isn't an array '#{@payload['recipient']['user_id'].inspect}'"
|
2015-12-09 13:09:37 +00:00
|
|
|
else
|
2017-10-01 12:25:52 +00:00
|
|
|
@payload['recipient']['user_id'].each do |user_id|
|
2016-01-15 17:22:57 +00:00
|
|
|
|
|
|
|
next if local_client[:user]['id'].to_i != user_id.to_i
|
|
|
|
|
2020-11-27 09:49:36 +00:00
|
|
|
log 'info', "send broadcast from (#{@client_id}) to (user_id=#{user_id})", local_client_id
|
2016-01-15 17:22:57 +00:00
|
|
|
websocket_send(local_client_id, @payload['data'])
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-12-09 13:09:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# broadcast every client
|
|
|
|
else
|
2020-11-27 09:49:36 +00:00
|
|
|
log 'info', "send broadcast from (#{@client_id})", local_client_id
|
2015-12-09 13:09:37 +00:00
|
|
|
websocket_send(local_client_id, @payload['data'])
|
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-12-09 13:09:37 +00:00
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|