websocket-rails 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,18 @@
1
1
  # WebsocketRails Change Log
2
2
 
3
+ ## Version 0.2.1
4
+
5
+ * Fix default redis driver issue that was causing problems when using
6
+ redis while event machine was not running.
7
+ * Fix undefined data store value issue. Thanks to @burninggramma.
8
+
9
+ ## Version 0.2.0
10
+
11
+ November 25 2012
12
+
13
+ * Add standalone server mode to support non event machine
14
+ based web servers.
15
+
3
16
  ## Version 0.1.9
4
17
 
5
18
  November 19 2012
data/README.md CHANGED
@@ -8,6 +8,9 @@ If you haven't done so yet, check out the [Project Page](http://danknox.github.c
8
8
 
9
9
  Check out the [CHANGELOG](https://github.com/DanKnox/websocket-rails/blob/master/CHANGELOG.md) to find out what's new.
10
10
 
11
+ As of version 0.2.0, non event machine based web servers such as Phusion
12
+ Passenger are supported through the use of the [Standalone Server Mode](https://github.com/DanKnox/websocket-rails/wiki/Standalone-Server-Mode).
13
+
11
14
  ## Overview
12
15
 
13
16
  Start treating client side events as first class citizens inside your
@@ -34,6 +37,7 @@ Check out the [Example Application](https://github.com/DanKnox/websocket-rails-E
34
37
  DataStore](https://github.com/DanKnox/websocket-rails/wiki/Using-the-DataStore)
35
38
  * [Reloading Controllers In Development](https://github.com/DanKnox/websocket-rails/wiki/Reloading-Controllers-In-Development)
36
39
  * [Multiple Servers and Background Jobs](https://github.com/DanKnox/websocket-rails/wiki/Multiple-Servers-and-Background-Jobs)
40
+ * [Standalone Server Mode](https://github.com/DanKnox/websocket-rails/wiki/Standalone-Server-Mode)
37
41
 
38
42
  ## Handle Events With Class
39
43
 
@@ -79,7 +83,7 @@ end
79
83
  Receive the response in the client.
80
84
 
81
85
  ````javascript
82
- dispatcher.bind('tasks.create_successful', function(task) {
86
+ dispatcher.bind('tasks.create_success', function(task) {
83
87
  console.log('successfully created ' + task.name);
84
88
  });
85
89
  ````
@@ -93,7 +97,7 @@ var failure = function(task) {
93
97
  console.log("Failed to create Product: " + product.name)
94
98
  }
95
99
 
96
- dispatcher.trigger('products.create', success, failure);
100
+ dispatcher.trigger('products.create', task, success, failure);
97
101
  ````
98
102
 
99
103
  Then trigger them in your controller:
@@ -40,7 +40,7 @@ module WebsocketRails
40
40
  end
41
41
 
42
42
  def self.redis_defaults
43
- {:host => '127.0.0.1', :port => 6379}
43
+ {:host => '127.0.0.1', :port => 6379, :driver => :synchrony}
44
44
  end
45
45
 
46
46
  attr_accessor :standalone
@@ -46,7 +46,7 @@ module WebsocketRails
46
46
  end
47
47
 
48
48
  def [](k)
49
- @data[cid][k] = Hash.new unless @data[cid]
49
+ @data[cid] = Hash.new unless @data[cid]
50
50
  @data[cid][k]
51
51
  end
52
52
 
@@ -1,33 +1,58 @@
1
- require "redis"
2
1
  require "redis/connection/synchrony"
2
+ require "redis"
3
+ require "redis/connection/ruby"
3
4
 
4
5
  module WebsocketRails
5
6
  class Synchronization
6
7
 
8
+ def self.publish(event)
9
+ singleton.publish event
10
+ end
11
+
12
+ def self.synchronize!
13
+ singleton.synchronize!
14
+ end
15
+
16
+ def self.shutdown!
17
+ singleton.shutdown!
18
+ end
19
+
20
+ def self.singleton
21
+ @singleton ||= new
22
+ end
23
+
7
24
  include Logging
8
25
 
9
- def self.redis
26
+ def redis
10
27
  @redis ||= Redis.new(WebsocketRails.redis_options)
11
28
  end
12
29
 
13
- def self.publish(event)
30
+ def ruby_redis
31
+ @ruby_redis ||= begin
32
+ redis_options = WebsocketRails.redis_options.merge(:driver => :ruby)
33
+ Redis.new(redis_options)
34
+ end
35
+ end
36
+
37
+ def publish(event)
14
38
  Fiber.new do
39
+ redis_client = EM.reactor_running? ? redis : ruby_redis
15
40
  event.server_token = server_token
16
- redis.publish "websocket_rails.events", event.serialize
41
+ redis_client.publish "websocket_rails.events", event.serialize
17
42
  end.resume
18
43
  end
19
44
 
20
- def self.server_token
45
+ def server_token
21
46
  @server_token
22
47
  end
23
48
 
24
- def self.synchronize!
49
+ def synchronize!
25
50
  unless @synchronizing
26
51
  @server_token = generate_unique_token
27
52
  register_server(@server_token)
28
53
 
29
54
  synchro = Fiber.new do
30
- EM::Synchrony.sleep(0.1)
55
+ #EM::Synchrony.sleep(0.1)
31
56
 
32
57
  fiber_redis = Redis.connect(WebsocketRails.redis_options)
33
58
  fiber_redis.subscribe "websocket_rails.events" do |on|
@@ -59,11 +84,11 @@ module WebsocketRails
59
84
  end
60
85
  end
61
86
 
62
- def self.shutdown!
87
+ def shutdown!
63
88
  remove_server(server_token)
64
89
  end
65
90
 
66
- def self.generate_unique_token
91
+ def generate_unique_token
67
92
  begin
68
93
  token = SecureRandom.urlsafe_base64
69
94
  end while redis.sismember("websocket_rails.active_servers", token)
@@ -71,14 +96,14 @@ module WebsocketRails
71
96
  token
72
97
  end
73
98
 
74
- def self.register_server(token)
99
+ def register_server(token)
75
100
  Fiber.new do
76
101
  redis.sadd "websocket_rails.active_servers", token
77
102
  log "Server Registered: #{token}"
78
103
  end.resume
79
104
  end
80
105
 
81
- def self.remove_server(token)
106
+ def remove_server(token)
82
107
  Fiber.new do
83
108
  redis.srem "websocket_rails.active_servers", token
84
109
  log "Server Removed: #{token}"
@@ -1,3 +1,3 @@
1
1
  module WebsocketRails
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -725,3 +725,41 @@ Connecting to database specified by database.yml
725
725
  Connecting to database specified by database.yml
726
726
  Connecting to database specified by database.yml
727
727
  Connecting to database specified by database.yml
728
+ Connecting to database specified by database.yml
729
+ Connecting to database specified by database.yml
730
+ Connecting to database specified by database.yml
731
+ Connecting to database specified by database.yml
732
+ Connecting to database specified by database.yml
733
+ Connecting to database specified by database.yml
734
+ Connecting to database specified by database.yml
735
+ Connecting to database specified by database.yml
736
+ Connecting to database specified by database.yml
737
+ Connecting to database specified by database.yml
738
+ Connecting to database specified by database.yml
739
+ Connecting to database specified by database.yml
740
+ Connecting to database specified by database.yml
741
+ Connecting to database specified by database.yml
742
+ Connecting to database specified by database.yml
743
+ Connecting to database specified by database.yml
744
+ Connecting to database specified by database.yml
745
+ Connecting to database specified by database.yml
746
+ Connecting to database specified by database.yml
747
+ Connecting to database specified by database.yml
748
+ Connecting to database specified by database.yml
749
+ Connecting to database specified by database.yml
750
+ Connecting to database specified by database.yml
751
+ Connecting to database specified by database.yml
752
+ Connecting to database specified by database.yml
753
+ Connecting to database specified by database.yml
754
+ Connecting to database specified by database.yml
755
+ Connecting to database specified by database.yml
756
+ Connecting to database specified by database.yml
757
+ Connecting to database specified by database.yml
758
+ Connecting to database specified by database.yml
759
+ Connecting to database specified by database.yml
760
+ Connecting to database specified by database.yml
761
+ Connecting to database specified by database.yml
762
+ Connecting to database specified by database.yml
763
+ Connecting to database specified by database.yml
764
+ Connecting to database specified by database.yml
765
+ Connecting to database specified by database.yml
@@ -2,14 +2,38 @@ require 'spec_helper'
2
2
 
3
3
  module WebsocketRails
4
4
  describe DataStore do
5
+ let(:attribute) {"example_attribute"}
6
+ let(:value) {1}
7
+
5
8
  before(:each) do
6
9
  @base = double('base_controller')
7
10
  @base.stub(:client_id).and_return(1)
11
+ @data_store = DataStore.new(@base)
8
12
  end
9
13
 
10
14
  it "loads up" do
11
- data_store = DataStore.new(@base)
12
- data_store.present?.should be_true
15
+ @data_store.present?.should be_true
16
+ end
17
+
18
+ describe "#[]" do
19
+ context "with an undefined attribute" do
20
+ it "returns nil" do
21
+ @data_store[attribute].should be_nil
22
+ end
23
+ end
24
+
25
+ context "with a defined attribute" do
26
+ it "returns its value" do
27
+ @data_store[attribute] = value
28
+ @data_store[attribute].should == value
29
+ end
30
+ end
31
+ end
32
+
33
+ describe "#[]=" do
34
+ it "returns the value" do
35
+ (@data_store[attribute]=value).should == value
36
+ end
13
37
  end
14
38
  end
15
39
  end
@@ -18,7 +18,7 @@ module WebsocketRails
18
18
  EM.stop
19
19
  end
20
20
 
21
- let(:subject) { Synchronization }
21
+ let(:subject) { Synchronization.singleton }
22
22
 
23
23
  describe "#publish" do
24
24
  it "should add the serialized event to the websocket_rails.events channel" do
@@ -29,6 +29,26 @@ module WebsocketRails
29
29
  end
30
30
  end
31
31
 
32
+ describe "#synchronize!" do
33
+ #before do
34
+ # #@synchro = Synchronization.new
35
+ #end
36
+
37
+ #it "should receive remote channel events" do
38
+ # event = Event.new(:channel_event, :channel => :channel_one, :data => 'hello channel one')
39
+
40
+ # @redis.should_receive(:subscribe)
41
+ # Redis.should_receive(:connect).with(WebsocketRails.redis_options).and_return(@redis)
42
+
43
+ # Synchronization.new.synchronize!
44
+
45
+ # EM::Synchrony.sleep(0.5)
46
+
47
+ # redis = @redis
48
+ # EM.next_tick { redis.publish "websocket_rails.events", event.serialize }
49
+ #end
50
+ end
51
+
32
52
  describe "#generate_unique_token" do
33
53
  before do
34
54
  SecureRandom.stub(:urlsafe_base64).and_return(1, 2, 3)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: websocket-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2012-11-26 00:00:00.000000000Z
14
+ date: 2013-01-29 00:00:00.000000000Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rails
@@ -285,7 +285,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
285
285
  version: '0'
286
286
  segments:
287
287
  - 0
288
- hash: -1199514115316704579
288
+ hash: -982119143149111185
289
289
  required_rubygems_version: !ruby/object:Gem::Requirement
290
290
  none: false
291
291
  requirements:
@@ -294,7 +294,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
294
294
  version: '0'
295
295
  segments:
296
296
  - 0
297
- hash: -1199514115316704579
297
+ hash: -982119143149111185
298
298
  requirements: []
299
299
  rubyforge_project: websocket-rails
300
300
  rubygems_version: 1.8.19