simple_pusher 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZTZiOWQ2ZjAxYzAyMjVlMTc3NDkyNTI0YzljOTU4MmYzOWYwNTc0Ng==
4
+ N2MyNzZjMjk3OTYwNGFjNWMxODQ4NmIxNDk2NDhiZGQzNDViNWU5ZA==
5
5
  data.tar.gz: !binary |-
6
- MDVjZWQ4OWQ4NjU0YjkzNzQ3OGJmNzc3OWJkNTI1OTZjZDU3MDA4MQ==
6
+ N2M1YzcyZTM0MzI5MmU1NTdmMTdhNzdmOTcyNGM0OWQyMzU2N2JhOA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- Y2IwZGUxNTI3MzY0YzgzZTNjMWZhZDk5NmUyOTlkMWQzM2RlZjdhNWY4ZWE1
10
- Mzc5MDZmNTM1MzA4NzJmMDYzM2U5ZjFlNDM0MzcwY2EzYzBmYTFkMTc2YjRi
11
- YmM5MWMyYzk1YWYwOWI5M2Y1MTVkNzBkN2IxYTM3MmQwMTkyNTQ=
9
+ MGZkM2ZjNmFkZGEyNjc3NGVkN2E5MzBhZjYxZjI0NDQ0OTY2NDk5NDgyNGM3
10
+ YjBjZjIwM2M2NmQwMWE2NGMwNmIwMmJlMDI3YTM4Nzk5NjZkNzA5OWI1MjJk
11
+ YTU4YmE4NWE5ZDgxMDFlNDFlYmJhZjZjN2NjNjZmZjllZmJjMGQ=
12
12
  data.tar.gz: !binary |-
13
- NDUwOTNlNzUxNzRiNTVkZDRiNGZkYTcxNWNmNjRmNjkwNTc4NzZhZWRkZWY3
14
- ZWUwYjgxMjgzMWFmYjdlNWFkNjIzZjQxMWQ1YTIwYzA5OGIyOTBkZGI2YWMy
15
- MjRmNThkODVmNjJmYzFkM2JhMzdhNjU5ODVhZDMxZDg3ZmM2MWM=
13
+ MTY4M2UzZjUyM2I2MTVjYTU2ZWNlMTkzMWU4NmM2ZDU3ZDZmYTIxZDI5MDM2
14
+ OGUzMGRjODI1MDRiNzBlYmQ0YzcyYjM5NjIxZTg2MWM4NGMyMTg3ODFmZGU5
15
+ NzgyMWM2MjhmMjU0ZTg2MzYzM2U0Y2JkYjAxYmU0ZTIzMzg2YWU=
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # SimplePusher
2
2
 
3
- SimplePusher is a HTML5 websocket powered realtime messaging tool.
3
+ SimplePusher is a HTML5 websocket powered realtime messaging tool used in Rails project.
4
4
 
5
5
  ## Installation
6
6
 
@@ -16,46 +16,60 @@ Or install it yourself as:
16
16
 
17
17
  $ gem install simple_pusher
18
18
 
19
- ## Usage
19
+ Then start simple pusher server when rails app boot:
20
20
 
21
- 1. Start simple pusher server in rails config/initializers.
21
+ # config/initializers/simple_pusher.rb
22
+ EventMachine.next_tick do
23
+ SimplePusher.setup do |config|
24
+ config.port = 8088
25
+ config.debug = false
26
+ end
27
+ SimplePusher.start
28
+ end
22
29
 
23
- ```
24
- # config/initializers/simple_pusher.rb
25
- EventMachine.next_tick do
26
- SimplePusher.setup do |config|
27
- config.port = 8088
28
- config.debug = false
29
- end
30
- SimplePusher.start
31
- end
32
- ```
30
+ ### Note
33
31
 
34
- 2. Setup client js code.
32
+ As initializer code `config/initializers/simple_pusher.rb` show, your rails runtime environment must be in EventMachine run loop. Otherwise you start EventMachine run loop in a new thread.
35
33
 
36
- ```
37
- var simple_pusher = new SimplePusher("ws://<%= request.host %>:8088/");
38
- simple_pusher.on("message", function(message){
39
- console.log("Receive:", message )
40
- });
34
+ ## Usage
35
+
36
+ Setup client js code.
41
37
 
42
- simple_pusher.on("online_count", function(message){
38
+ Add `//= require simple_pusher` to app/assets/javascripts/application.js
43
39
 
44
- });
40
+ Add code to app/views/layouts/application.html.erb
41
+ ```
42
+ <script type="text/javascript">
43
+ var simple_pusher = new SimplePusher("ws://<%= request.host %>:8088/");
44
+ simple_pusher.broadcast("Hello everybody.");
45
+ </script>
45
46
  ```
46
47
 
47
- 3. Broadcast message via server side.
48
+ Broadcast message via server side.
48
49
 
49
50
  ```
50
51
  SimplePusher.broadcast("Time now #{Time.now.to_s(:db}")
51
52
  ```
52
53
 
53
- 4. Broadcast message via client side.
54
+ Message callback at server side.
54
55
 
55
56
  ```
56
- simple_pusher.broadcast("Hello everybody.")
57
+ SimplePusher.on("ping") do
58
+ puts "I received ping request."
59
+ end
60
+
61
+ SimplePusher.on("ping") do
62
+ puts "I also received ping request."
63
+ end
57
64
  ```
58
65
 
66
+ ## Emulating WebSockets in older browsers
67
+ It is possible to emulate WebSockets in older browsers using flash emulation. For example take a look at the [web-socket-js](https://github.com/gimite/web-socket-js) project.
68
+
69
+ ## TODO
70
+
71
+ * Add `on` method message callback to client js.
72
+
59
73
  ## Contributing
60
74
 
61
75
  1. Fork it
@@ -7,6 +7,19 @@ module SimplePusher
7
7
  @@clients ||= {}
8
8
  end
9
9
 
10
+ def self.callbacks
11
+ @@callbacks ||= {}
12
+ end
13
+
14
+ def self.on(event, &callback)
15
+ callbacks[event] ||= []
16
+ callbacks[event] << callback
17
+ end
18
+
19
+ def self.trigger(event)
20
+ callbacks[event].each {|callback| callback.call }
21
+ end
22
+
10
23
  def self.add_client(socket)
11
24
  client = self.new(socket)
12
25
  clients[socket] = client
@@ -34,6 +34,9 @@ module SimplePusher
34
34
  when 'broadcast'
35
35
  message = message.join(":")
36
36
  Connection.broadcast(message)
37
+ when 'emit'
38
+ event = message[0]
39
+ Connection.trigger(event)
37
40
  else
38
41
  #TODO
39
42
  end
@@ -1,3 +1,3 @@
1
1
  module SimplePusher
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/simple_pusher.rb CHANGED
@@ -25,8 +25,13 @@ module SimplePusher
25
25
  Connection.broadcast(message)
26
26
  end
27
27
 
28
+ def on(event, &callback)
29
+ Connection.on(event, &callback)
30
+ end
31
+
28
32
  module_function :start
29
33
  module_function :broadcast
34
+ module_function :on
30
35
 
31
36
  end
32
37
 
@@ -0,0 +1,14 @@
1
+ require File.expand_path("../test_helper", __FILE__)
2
+
3
+ describe SimplePusher::Configuration do
4
+ it "must respond to debug method" do
5
+ configuration = SimplePusher::Configuration.new
6
+ configuration.debug.must_equal false #default to false
7
+ end
8
+
9
+ it "must respond to port method" do
10
+ configuration = SimplePusher::Configuration.new
11
+ configuration.port.must_equal 8088 #default to 8088
12
+ end
13
+
14
+ end
@@ -6,8 +6,14 @@ describe SimplePusher do
6
6
  SimplePusher::VERSION.wont_be_nil
7
7
  end
8
8
 
9
- it "must respond listen method" do
10
- #SimplePusher.repond_with
9
+ it "setup method work" do
10
+ # EventMachine.run do
11
+ #
12
+ # end
13
+ SimplePusher.setup do |c|
14
+ c.port = 8888
15
+ end
16
+ SimplePusher.configuration.port.must_equal 8888
11
17
  end
12
18
 
13
19
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_pusher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - qichunren
@@ -71,6 +71,7 @@ files:
71
71
  - lib/simple_pusher/server.rb
72
72
  - lib/simple_pusher/version.rb
73
73
  - simple_pusher.gemspec
74
+ - test/configuration_test.rb
74
75
  - test/simple_pusher_test.rb
75
76
  - test/test_helper.rb
76
77
  - vendor/assets/javascripts/simple_pusher.coffee
@@ -99,5 +100,6 @@ signing_key:
99
100
  specification_version: 4
100
101
  summary: Simple pusher based on websocket, build with Eventmachine
101
102
  test_files:
103
+ - test/configuration_test.rb
102
104
  - test/simple_pusher_test.rb
103
105
  - test/test_helper.rb