Class: RComet::Channel

Inherits:
Object
  • Object
show all
Defined in:
lib/rcomet/channel.rb

Constant Summary collapse

HANDSHAKE =
'/meta/handshake'
CONNECT =
'/meta/connect'
SUBSCRIBE =
'/meta/subscribe'
UNSUBSCRIBE =
'/meta/unsubscribe'
DISCONNECT =
'/meta/disconnect'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, data = nil) ⇒ Channel

Create a new channel



38
39
40
41
42
43
# File 'lib/rcomet/channel.rb', line 38

def initialize( path, data = nil )
  @path = path
  @users = Hash.new
  @data = data
  @handler = nil
end

Instance Attribute Details

#handlerObject (readonly)

Returns the value of attribute handler.



35
36
37
# File 'lib/rcomet/channel.rb', line 35

def handler
  @handler
end

#pathObject (readonly)

Returns the value of attribute path.



35
36
37
# File 'lib/rcomet/channel.rb', line 35

def path
  @path
end

Instance Method Details

#add_user(user) ⇒ Object

:nodoc:



72
73
74
# File 'lib/rcomet/channel.rb', line 72

def add_user( user ) #:nodoc:
  @users[user.id] = user
end

#callback(&block) ⇒ Object

Define the channel publishion callback



68
69
70
# File 'lib/rcomet/channel.rb', line 68

def callback( &block )
  @handler = block
end

#data(data = nil, &block) ⇒ Object

Send data to the channel or, if no parameter is given, get available data for the channel



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rcomet/channel.rb', line 46

def data( data = nil, &block )
  unless data or block_given?
    return @data
  end
  
  if block_given?
    @data = yield( data )
  else
    @data = data
  end

  @users.each do |id, user|
    message = {
      'channel' => @path,
      'data' => @data,
      'clientId' => id
    }
    user.send( message )
  end
end