Skip to content
Browse files

Update client

  • Loading branch information...
1 parent 9d6f483 commit 0234380f62aa311289bd443ab80440e1ee94481f @glejeune glejeune committed Dec 23, 2009
View
2 History.rdoc
@@ -1,4 +1,4 @@
-=== 1.0.0 / 2009-11-23
+=== 0.0.1 / 2009-11-23
* 1 major enhancement
View
56 README.rdoc
@@ -7,21 +7,23 @@ Bayeux Protocol : http://svn.cometd.org/trunk/bayeux/bayeux.html
== FEATURES/PROBLEMS:
-* RComet does not work with Thin !
* It was tested only with Dojo
+* Does not work with Thin :(
== SYNOPSIS:
- require 'rcomet'
+=== Server
+
+ require 'rcomet/server'
# We create a new RComer server
server = RComet::Server.new( :host => '0.0.0.0', :port => 8990, :server => :mongrel, :mount => '/' ) {
# We create a new channel (/graph) with a callback to manipulate recieved data
- channel['/graph'].callback do |data|
+ channel['/graph'].callback do |message|
# Someone have send data on channel /graph
- # We push the data to the same channel
+ # We push the data message to the same channel
# We don't need to do this... But it's an example
- channel['/graph'].data( data )
+ channel['/graph'].data( message['data'] )
end
}
@@ -35,7 +37,49 @@ Bayeux Protocol : http://svn.cometd.org/trunk/bayeux/bayeux.html
sleep(5)
end
-See <tt>examples</tt>...
+=== Client
+
+ require 'rcomet/client'
+
+ # Initialisation
+ x = RComet::Client.new( 'http://localhost:8990/' )
+
+ # Handshake
+ x.handshake
+
+ # Publication
+ x.publish( '/login', "daemon" );
+
+ # Subscriptions
+ x.subscribe( "/from/greg" ) { |r|
+ puts "#{r["username"]} : #{r["message"]}"
+ }
+ x.subscribe( "/from/daemon" ) { |r|
+ puts "#{r["username"]} : #{r["message"]}"
+ }
+
+ # Connection
+ x.connect
+
+ # Mainloop
+ msg = ""
+ while msg != "quit"
+ msg = $stdin.readline.chomp
+ unless msg == "quit"
+ channel = "/from/daemon"
+ data = { "username" => "daemon", "message" => msg }
+ r = x.publish( channel, data )
+ unless r["successful"]
+ puts "=> Message not send !"
+ end
+ end
+ end
+
+ x.disconnect
+
+=== More...
+
+See {examples}[http://github.com/glejeune/rcomet/tree/master/examples/]...
== REQUIREMENTS:
View
2 examples/chart/chart-cometd.rb
@@ -1,5 +1,5 @@
$:.unshift( "../../lib" )
-require 'rcomet'
+require 'rcomet/server'
server = RComet::Server.new( :host => '0.0.0.0', :port => 8990, :server => :mongrel, :mount => '/' ) {
channel['/graph'] = [1,1,2,2,3,3,4,4]
View
2 examples/chat/chat-cometd.rb
@@ -1,5 +1,5 @@
$:.unshift("../../lib")
-require 'rcomet'
+require 'rcomet/server'
users = {}
View
36 examples/soapbox/client.rb
@@ -0,0 +1,36 @@
+$:.unshift( "../../lib" )
+require 'rcomet/client'
+
+x = RComet::Client.new( 'http://localhost:8990/' )
+
+puts "-- handshake"
+x.handshake
+
+puts "-- login as `daemon'"
+x.publish( '/login', "daemon" );
+
+puts "-- subscriptions"
+x.subscribe( "/from/greg" ) { |r|
+ puts "#{r["username"]} : #{r["message"]}"
+}
+x.subscribe( "/from/daemon" ) { |r|
+ puts "#{r["username"]} : #{r["message"]}"
+}
+
+puts "-- connect"
+x.connect
+
+msg = ""
+while msg != "quit"
+ msg = $stdin.readline.chomp
+ unless msg == "quit"
+ channel = "/from/daemon"
+ data = { "username" => "daemon", "message" => msg }
+ r = x.publish( channel, data )
+ unless r["successful"]
+ puts "=> Message not send !"
+ end
+ end
+end
+
+x.disconnect
View
4 examples/soapbox/soapbox-cometd.rb
@@ -1,7 +1,7 @@
$:.unshift( "../../lib" )
-require 'rcomet'
+require 'rcomet/server'
-RComet::Server.new( :host => '0.0.0.0', :port => 8990, :mount => '/', :server => :mongrel ) {
+RComet::Server.new( :host => '0.0.0.0', :port => 8990, :mount => '/', :server => :thin ) {
channel['/login'].callback do |message|
puts "someone send "
p message['data']
View
11 lib/rcomet.rb
@@ -1,10 +1,15 @@
-require 'rubygems'
-require 'rcomet/server'
-
module RComet
VERSION = '0.0.1'
BAYEUX_VERSION = '1.0'
JSONP_CALLBACK = 'jsonpcallback'
CONNECTION_TYPES = %w[long-polling callback-polling]
+
+ def self.random(size) #:nodoc:
+ id = ''
+ size.times do |i|
+ id << ?A+rand(50)
+ end
+ return id
+ end
end
View
114 lib/rcomet/client.rb
@@ -1,39 +1,17 @@
-# (The MIT License)
-#
-# Copyright (c) 2009 Grégoire Lejeune
-#
-# Permission is hereby granted, free of charge, to any person obtaining
-# a copy of this software and associated documentation files (the
-# 'Software'), to deal in the Software without restriction, including
-# without limitation the rights to use, copy, modify, merge, publish,
-# distribute, sublicense, and/or sell copies of the Software, and to
-# permit persons to whom the Software is furnished to do so, subject to
-# the following conditions:
-#
-# The above copyright notice and this permission notice shall be
-# included in all copies or substantial portions of the Software.
-#
-# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
-# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
-# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
-# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
-# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
-# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-
require 'net/http'
require 'uri'
require 'rubygems'
require 'json'
-require 'faye'
+require 'rcomet'
+require 'rcomet/channel'
-module Faye
+module RComet
class Client
def initialize( uri_or_string )
@uri = uri_or_string
@uri = URI.parse(@uri) if @uri.class == String
@clientId = nil
- @interval = nil
+ # @interval = nil
@connection = nil
@subscriptions = {}
end
@@ -52,9 +30,9 @@ def connect
@connection = Thread.new {
faild = false
while true
- id = Faye.random(32)
+ id = RComet.random(32)
message = {
- "channel" => Faye::Channel::CONNECT,
+ "channel" => RComet::Channel::CONNECT,
"clientId" => @clientId,
"connectionType" => "long-polling",
"id" => id
@@ -87,9 +65,9 @@ def disconnect
unless @connection.nil?
@connection.kill
message = {
- "channel" => Faye::Channel::DISCONNECT,
+ "channel" => RComet::Channel::DISCONNECT,
"clientId" => @clientId,
- "id" => Faye.random(32)
+ "id" => RComet.random(32)
}
r = send( message )
## TODO : Check response
@@ -116,18 +94,18 @@ def disconnect
# * id * id
# * authSuccessful
def handshake
- id = Faye.random(32)
+ id = RComet.random(32)
message = {
- "channel" => Faye::Channel::HANDSHAKE,
- "version" => Faye::BAYEUX_VERSION,
+ "channel" => RComet::Channel::HANDSHAKE,
+ "version" => RComet::BAYEUX_VERSION,
"supportedConnectionTypes" => [ "long-polling", "callback-polling" ],
"id" => id
}
response = send( message )[0]
if response["successful"] and response["id"] == id
@clientId = response["clientId"]
- @interval = response["advice"]["interval"]
+ # @interval = response["advice"]["interval"]
else
raise
end
@@ -140,14 +118,12 @@ def handshake
# * id * error
# * ext * ext
def publish( channel, data )
- message = [
- {
- "channel" => channel,
- "data" => data,
- "clientId" => @clientId,
- "id" => Faye.random(32)
- }
- ]
+ message = {
+ "channel" => channel,
+ "data" => data,
+ "clientId" => @clientId,
+ "id" => RComet.random(32)
+ }
r = send(message)[0]
## TODO : Check response
end
@@ -162,18 +138,14 @@ def publish( channel, data )
# * ext
# * id
# * timestamp
- def subscribe( channels, &block )
- channels = [channels] unless channels.class == Array
- if block
- channels.each do |c|
- @subscriptions[c] = block
- end
- end
+ def subscribe( channel, &block )
+ @subscriptions[channel] = block if block_given?
+
message = {
- "channel" => Faye::Channel::SUBSCRIBE,
+ "channel" => RComet::Channel::SUBSCRIBE,
"clientId" => @clientId,
- "subscription" => channels,
- "id" => Faye.random(32)
+ "subscription" => channel,
+ "id" => RComet.random(32)
}
r = send(message)
@@ -196,10 +168,10 @@ def unsubscribe( channels )
@subscriptions.delete(c)
end
message = {
- "channel" => Faye::Channel::UNSUBSCRIBE,
+ "channel" => RComet::Channel::UNSUBSCRIBE,
"clientId" => @clientId,
"subscription" => channels,
- "id" => Faye.random(32)
+ "id" => RComet.random(32)
}
r = send(message)
@@ -208,41 +180,9 @@ def unsubscribe( channels )
private
def send( message )
- res = Net::HTTP.post_form( @uri, { "message" => message.to_json } )
+ res = Net::HTTP.post_form( @uri, { "message" => [message].to_json } )
return JSON.parse( res.body )
end
end
end
-
-if $0 == __FILE__
-x = Faye::Client.new( 'http://localhost:3000/comet' )
-
-puts "-- handshake"
-x.handshake
-
-puts "-- subscriptions"
-x.subscribe( "/mentioning/daemon" )
-
-x.subscribe( "/from/greg" ) { |r|
- puts "#{r["user"]} : #{r["message"]}"
-}
-
-puts "-- connect"
-x.connect
-
-msg = ""
-while msg != "quit"
- msg = $stdin.readline.chomp
- unless msg == "quit"
- channel = "/from/daemon"
- data = { "user" => "daemon", "message" => msg }
- r = x.publish( channel, data )
- unless r["successful"]
- puts "=> Message not send !"
- end
- end
-end
-
-x.disconnect
-end
View
2 lib/rcomet/core_ext.rb
@@ -1,4 +1,4 @@
-class Hash
+class Hash #:nodoc:
def <<( h )
self.merge!( h )
end
View
5 lib/rcomet/rack_adapter.rb
@@ -11,6 +11,7 @@
require 'rack'
require 'json'
+require 'rcomet'
require 'rcomet/core_ext'
require 'rcomet/server'
require 'rcomet/channel'
@@ -38,7 +39,7 @@ def call(env)
messages = JSON.parse(request.params['message'])
jsonp = request.params['jsonp'] || JSONP_CALLBACK
get = request.get?
-
+
process( jsonp, messages, get )
end
end
@@ -270,7 +271,7 @@ def handle( message )
'successful' => true
}
end
-
+
Thread.new do
unless c.nil?
if c.handler.nil?
View
1 lib/rcomet/server.rb
@@ -1,3 +1,4 @@
+require 'rubygems'
require 'rcomet/rack_adapter'
require 'logger'
View
5 lib/rcomet/user.rb
@@ -6,10 +6,7 @@ class User
# Create a new Comet user
def initialize(adapter)
@connected = false
- @id = ''
- 16.times do |i|
- @id << ?A+rand(50)
- end
+ @id = RComet.random(64)
@channels = Hash.new
@event_mutex = Mutex.new
View
89 rcomet.gemspec
@@ -0,0 +1,89 @@
+# Generated by jeweler
+# DO NOT EDIT THIS FILE DIRECTLY
+# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{rcomet}
+ s.version = "0.0.1"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Guillaume Dorchies", "Gr\303\251goire Lejeune"]
+ s.date = %q{2009-12-23}
+ s.default_executable = %q{rcomet}
+ s.description = %q{RComet implement the Bayeux protocole to allow you to create client and/or Comet's server.}
+ s.email = %q{guillaume.dorchies@gmail.com}
+ s.executables = ["rcomet"]
+ s.extra_rdoc_files = [
+ "README.rdoc"
+ ]
+ s.files = [
+ ".gitignore",
+ "History.rdoc",
+ "LICENSE",
+ "README.rdoc",
+ "Rakefile",
+ "VERSION",
+ "bin/rcomet",
+ "examples/chart/README.rdoc",
+ "examples/chart/chart-cometd.rb",
+ "examples/chart/chart.rb",
+ "examples/chart/static/chart.rhtml",
+ "examples/chat/README.rdoc",
+ "examples/chat/chat-cometd.rb",
+ "examples/chat/chat.rb",
+ "examples/chat/static/chat.rhtml",
+ "examples/chat/static/login.rhtml",
+ "examples/chat/static/style.css",
+ "examples/soapbox/README.rdoc",
+ "examples/soapbox/client.rb",
+ "examples/soapbox/soapbox-cometd.rb",
+ "examples/soapbox/soapbox.rb",
+ "examples/soapbox/static/soapbox.rhtml",
+ "examples/soapbox/static/style.css",
+ "lib/rcomet.rb",
+ "lib/rcomet/channel.rb",
+ "lib/rcomet/client.rb",
+ "lib/rcomet/core_ext.rb",
+ "lib/rcomet/rack_adapter.rb",
+ "lib/rcomet/server.rb",
+ "lib/rcomet/user.rb",
+ "spec/rcomet_spec.rb",
+ "spec/spec.opts",
+ "spec/spec_helper.rb"
+ ]
+ s.homepage = %q{http://github.com/glejeune/rcomet}
+ s.rdoc_options = ["--title", "RComet", "--main", "README.rdoc", "--line-numbers"]
+ s.require_paths = ["lib"]
+ s.rubyforge_project = %q{rcomet}
+ s.rubygems_version = %q{1.3.5}
+ s.summary = %q{RComet is an implementation of the Bayeux protocol in Ruby.}
+ s.test_files = [
+ "spec/rcomet_spec.rb",
+ "spec/spec_helper.rb",
+ "examples/chart/chart-cometd.rb",
+ "examples/chart/chart.rb",
+ "examples/chat/chat-cometd.rb",
+ "examples/chat/chat.rb",
+ "examples/soapbox/client.rb",
+ "examples/soapbox/soapbox-cometd.rb",
+ "examples/soapbox/soapbox.rb"
+ ]
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
+ s.add_runtime_dependency(%q<rack>, [">= 0"])
+ else
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
+ s.add_dependency(%q<rack>, [">= 0"])
+ end
+ else
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
+ s.add_dependency(%q<rack>, [">= 0"])
+ end
+end
+

0 comments on commit 0234380

Please sign in to comment.
Something went wrong with that request. Please try again.