diff --git a/lib/sessions.rb b/lib/sessions.rb index 64cdc5739..b0da69bcc 100644 --- a/lib/sessions.rb +++ b/lib/sessions.rb @@ -281,6 +281,33 @@ returns =begin +send message to recipient client + + Sessions.send_to(user_id, data) + +returns + + true|false + +=end + + def self.send_to( user_id, data ) + + # list all current clients + client_list = self.sessions + client_list.each {|client_id| + session = Sessions.get(client_id) + next if !session + next if !session[:user] + next if !session[:user][:id] + next if session[:user][:id].to_i != user_id.to_i + Sessions.send( client_id, data ) + } + true + end + +=begin + send message to all client Sessions.broadcast(data) diff --git a/test/unit/session_enhanced_test.rb b/test/unit/session_enhanced_test.rb index 50297ecf1..a40410e9c 100644 --- a/test/unit/session_enhanced_test.rb +++ b/test/unit/session_enhanced_test.rb @@ -130,6 +130,17 @@ class SessionEnhancedTest < ActiveSupport::TestCase assert_equal( messages.count, 1, 'messages count') assert_equal( 'ooo123123123123123123', messages[0]['msg'], 'messages broadcast 1') + # send dedicated message to user + Sessions.send_to( agent1.id, { :msg => 'ooo1231231231231231234'} ) + messages = Sessions.queue(client_id1) + assert_equal( messages.count, 1, 'messages count') + assert_equal( 'ooo1231231231231231234', messages[0]['msg'], 'messages send 1') + + messages = Sessions.queue(client_id2) + assert_equal( messages.count, 0, 'messages count') + + messages = Sessions.queue(client_id3) + assert_equal( messages.count, 0, 'messages count') # start jobs jobs = Thread.new {