Permalink
Browse files

Totally rewritten

  • Loading branch information...
1 parent 3f57e0a commit a6341fe16297a200806a723d2e7b17a6672e8b03 @glejeune glejeune committed Dec 8, 2009
@@ -1,17 +1,17 @@
$:.unshift( "../../lib" )
require 'rcomet'
-server = RComet::Server.new( :host => '0.0.0.0', :port => 8990 )
-graph_channel = server.add_channel( '/graph', [1,1,2,2,3,3,4,4] )
-graph_channel.callback do |data|
+server = RComet::Server.new( :host => '0.0.0.0', :port => 8990, :server => :mongrel, :mount => '/' )
+server.channel['/graph'] = [1,1,2,2,3,3,4,4]
+server.channel['/graph'].callback do |data|
puts "someone send "
p data
puts 'on channel /graph'
- graph_channel.data = data
+ server.channel['/graph'].data( data )
end
server.start
while true
- graph_channel.data = [rand(10),rand(10),rand(10),rand(10),rand(10),rand(10),rand(10),rand(10)]
+ server.channel['/graph'].data( [rand(10),rand(10),rand(10),rand(10),rand(10),rand(10),rand(10),rand(10)] )
sleep(5)
end
@@ -1,13 +1,12 @@
$:.unshift( "../../lib" )
require 'rcomet'
-server = RComet::Server.new( :server => '0.0.0.0', :port => 8990 )
-login = server.add_channel( '/login' )
-login.callback do |data|
+server = RComet::Server.new( :host => '0.0.0.0', :port => 8990, :mount => '/' )
+server.channel['/login'].callback do |data|
puts "someone send "
p data
puts 'on channel /login'
- server.add_channel( "/from/#{data}" )
+ server.channel["/from/#{data}"]
end
server.start
View
@@ -1,14 +1,10 @@
-##Comet server with bayeux protocol for Manag
-
-##TODO Restreindre certain canaux à des utilisateurs authentifiés
-##TODO Faire les Spec
-
require 'rubygems'
-require 'json'
-
-require 'rcomet/core_ext'
-require 'rcomet/constants'
-require 'rcomet/error'
-require 'rcomet/channel'
-require 'rcomet/user'
require 'rcomet/server'
+
+module RComet
+ VERSION = '0.0.1'
+
+ BAYEUX_VERSION = '1.0'
+ JSONP_CALLBACK = 'jsonpcallback'
+ CONNECTION_TYPES = %w[long-polling callback-polling]
+end
View
@@ -1,7 +1,38 @@
module RComet
+ class ChannelSet #:nodoc:
+ def initialize
+ @channels = Hash.new
+ end
+
+ def [](channel_or_path)
+ channel = nil
+ if( channel_or_path.class == RComet::Channel )
+ return @channels[channel_or_path.path] if @channels.has_key?(channel_or_path.path)
+ channel = channel_or_path
+ else
+ return @channels[channel_or_path] if @channels.has_key?(channel_or_path)
+ channel = RComet::Channel.new( channel_or_path )
+ end
+
+ @channels[channel.path] = channel
+ return channel
+ end
+
+ def []=(channel_or_path, data)
+ channel = self[channel_or_path]
+ channel.data( data )
+ return channel
+ end
+ end
+
class Channel
- attr_reader :path, :data, :handler
- attr_accessor :server
+ HANDSHAKE = '/meta/handshake'
+ CONNECT = '/meta/connect'
+ SUBSCRIBE = '/meta/subscribe'
+ UNSUBSCRIBE = '/meta/unsubscribe'
+ DISCONNECT = '/meta/disconnect'
+
+ attr_reader :path, :handler
# Create a new channel
def initialize( path, data = nil )
@@ -10,48 +41,36 @@ def initialize( path, data = nil )
@data = data
@handler = nil
end
-
- # Update data
- #
- # channel.update_data( [1,2,3,4] )
- #
- # or
- #
- # channel.update_data( data = nil ) { |data| ... }
- #
- # or
- #
- # channel.data = ... # YES, data not update_data !
- def update_data( data = nil, &block )
+
+ # Send data to the channel or, if no parameter is given, get available data for the channel
+ 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|
- response = {
+ message = {
'channel' => @path,
'data' => @data,
'clientId' => id
}
- user.send_data( response )
+ user.send( message )
end
end
- def data=( data ) #:nodoc:
- update_data( data )
- end
- def callback( &b )
- @handler = b
+ # Define the channel publishion callback
+ def callback( &block )
+ @handler = block
end
def add_user( user ) #:nodoc:
@users[user.id] = user
end
-
- def delete_user( user ) #:nodoc:
- @users.delete(user.id)
- end
end
end
View
@@ -1,13 +0,0 @@
-module RComet
- VERSION = '0.0.1'
-
- class Channel
- HANDSHAKE = '/meta/handshake'
- CONNECT = '/meta/connect'
- SUBSCRIBE = '/meta/subscribe'
- UNSUBSCRIBE = '/meta/unsubscribe'
- DISCONNECT = '/meta/disconnect'
- end
-
- BAYEUX_VERSION = '1.0'
-end
View
@@ -1,16 +0,0 @@
-module RComet
- class RCometAddrInUse < Exception
- end
-
- class RCometInvalidRequest < Exception
- end
-
- class RCometInvalidChannel < Exception
- end
-
- class RCometNotImplemented < Exception
- end
-
- class RCometInvalidServer < Exception
- end
-end
Oops, something went wrong.

0 comments on commit a6341fe

Please sign in to comment.