Added broadcast feature to long polling.
This commit is contained in:
parent
108cff3ca1
commit
c768f02f57
2 changed files with 37 additions and 5 deletions
|
@ -29,6 +29,28 @@ class LongPollingController < ApplicationController
|
||||||
user = User.user_data_full( user_id )
|
user = User.user_data_full( user_id )
|
||||||
end
|
end
|
||||||
Session.create( client_id, user, { :type => 'ajax' } )
|
Session.create( client_id, user, { :type => 'ajax' } )
|
||||||
|
|
||||||
|
# broadcast
|
||||||
|
elsif params['data']['action'] == 'broadcast'
|
||||||
|
|
||||||
|
# list all current clients
|
||||||
|
client_list = Session.list
|
||||||
|
client_list.each {|local_client_id, local_client|
|
||||||
|
if local_client_id != client_id
|
||||||
|
|
||||||
|
# broadcast to recipient list
|
||||||
|
if params['data']['recipient'] && params['data']['recipient']['user_id']
|
||||||
|
params['data']['recipient']['user_id'].each { |user_id|
|
||||||
|
if local_client[:user][:id] == user_id
|
||||||
|
Session.send( local_client_id, params['data'] )
|
||||||
|
end
|
||||||
|
}
|
||||||
|
# broadcast every client
|
||||||
|
else
|
||||||
|
Session.send( local_client_id, params['data'] )
|
||||||
|
end
|
||||||
|
end
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
if new_connection
|
if new_connection
|
||||||
|
|
|
@ -140,21 +140,31 @@ EventMachine.run {
|
||||||
elsif data['action'] == 'broadcast'
|
elsif data['action'] == 'broadcast'
|
||||||
|
|
||||||
# list all current clients
|
# list all current clients
|
||||||
@clients.each { |local_client_id, local_client|
|
client_list = Session.list
|
||||||
|
client_list.each {|local_client_id, local_client|
|
||||||
if local_client_id != client_id
|
if local_client_id != client_id
|
||||||
|
|
||||||
# broadcast to recipient list
|
# broadcast to recipient list
|
||||||
if data['recipient'] && data['recipient']['user_id']
|
if data['recipient'] && data['recipient']['user_id']
|
||||||
data['recipient']['user_id'].each { |user_id|
|
data['recipient']['user_id'].each { |user_id|
|
||||||
if local_client[:session]['id'] == user_id
|
if local_client[:user][:id] == user_id
|
||||||
log 'notice', "send broadcast to (user_id=#{user_id})", local_client_id
|
log 'notice', "send broadcast to (user_id=#{user_id})", local_client_id
|
||||||
local_client[:websocket].send( "[#{msg}]" )
|
if local_client[:meta][:type] == 'websocket' && @clients[ local_client_id ]
|
||||||
|
@clients[ local_client_id ][:websocket].send( "[#{msg}]" )
|
||||||
|
else
|
||||||
|
Session.send( local_client_id, data )
|
||||||
|
end
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
# broadcast every client
|
# broadcast every client
|
||||||
else
|
else
|
||||||
log 'notice', "send broadcast", local_client_id
|
log 'notice', "send broadcast", local_client_id
|
||||||
local_client[:websocket].send( "[#{msg}]" )
|
if local_client[:meta][:type] == 'websocket' && @clients[ local_client_id ]
|
||||||
|
@clients[ local_client_id ][:websocket].send( "[#{msg}]" )
|
||||||
|
else
|
||||||
|
Session.send( local_client_id, data )
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
@ -162,7 +172,7 @@ EventMachine.run {
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
# check open unused connections, kick all connection without activitie in the last 5 minutes
|
# check open unused connections, kick all connection without activitie in the last 2 minutes
|
||||||
EventMachine.add_periodic_timer(120) {
|
EventMachine.add_periodic_timer(120) {
|
||||||
log 'notice', "check unused idle connections..."
|
log 'notice', "check unused idle connections..."
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue