Class: Citrus::Common::Service::ChannelService::Channel
- Inherits:
-
Object
- Object
- Citrus::Common::Service::ChannelService::Channel
- Includes:
- Util
- Defined in:
- lib/citrus/common/service/channel_service.rb
Overview
Channel
Instance Attribute Summary collapse
-
#groups ⇒ Object
readonly
Returns the value of attribute groups.
-
#user_amount ⇒ Object
readonly
Returns the value of attribute user_amount.
Instance Method Summary collapse
-
#add(uid, sid) ⇒ Object
Add user to channel.
-
#destroy ⇒ Object
Destroy channel.
-
#get_member(uid) ⇒ Object
Get member info.
-
#get_members ⇒ Object
Get channel members.
-
#initialize(name, service) ⇒ Channel
constructor
Create a new channel.
-
#leave(uid, sid) ⇒ Object
Remove user from channel.
-
#push_message(route, msg, args, &block) ⇒ Object
Push message to all the members in the channel.
Constructor Details
#initialize(name, service) ⇒ Channel
Create a new channel
403 404 405 406 407 408 409 410 |
# File 'lib/citrus/common/service/channel_service.rb', line 403 def initialize name, service @name = name @groups = {} @records = {} @channel_service = service @state = :state_inited @user_amount = 0 end |
Instance Attribute Details
#groups ⇒ Object (readonly)
Returns the value of attribute groups.
397 398 399 |
# File 'lib/citrus/common/service/channel_service.rb', line 397 def groups @groups end |
#user_amount ⇒ Object (readonly)
Returns the value of attribute user_amount.
397 398 399 |
# File 'lib/citrus/common/service/channel_service.rb', line 397 def user_amount @user_amount end |
Instance Method Details
#add(uid, sid) ⇒ Object
Add user to channel
416 417 418 419 420 421 422 423 424 425 426 427 |
# File 'lib/citrus/common/service/channel_service.rb', line 416 def add uid, sid return false unless @state == :state_inited res = add uid, sid, @groups if res @records[uid] = { :sid => sid, :uid => uid } @user_amount += 1 end add_to_store @channel_service, gen_key(@channel_service, @name), gen_value(sid, uid) res end |
#destroy ⇒ Object
Destroy channel
464 465 466 467 |
# File 'lib/citrus/common/service/channel_service.rb', line 464 def destroy @state = :state_destroyed @channel_service.destroy_channel @name end |
#get_member(uid) ⇒ Object
Get member info
459 460 461 |
# File 'lib/citrus/common/service/channel_service.rb', line 459 def get_member uid @records[uid] end |
#get_members ⇒ Object
Get channel members
450 451 452 453 454 |
# File 'lib/citrus/common/service/channel_service.rb', line 450 def get_members res = [] @groups.each { |group| group.each { |e| res << e } } res end |
#leave(uid, sid) ⇒ Object
Remove user from channel
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
# File 'lib/citrus/common/service/channel_service.rb', line 433 def leave uid, sid return unless uid && sid @records.delete uid @user_amount -= 1 @user_amout = 0 if @user_amount < 0 remove_from_store @channel_service, gen_key(@channel_service, @name), gen_value(sid, uid) res = delete_from uid, sid, @groups[sid] if @groups[sid] && @groups[sid].length == 0 @groups.delete sid end res end |
#push_message(route, msg, args, &block) ⇒ Object
Push message to all the members in the channel
474 475 476 477 478 479 480 |
# File 'lib/citrus/common/service/channel_service.rb', line 474 def route, msg, args, &block unless @state == :state_inited block_given? and yield Exception.new 'channel is not running now' return end @channel_service, route, msg, @groups, args, &block end |