simple_pusher 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/README.md +38 -24
- data/lib/simple_pusher/connection.rb +13 -0
- data/lib/simple_pusher/server.rb +3 -0
- data/lib/simple_pusher/version.rb +1 -1
- data/lib/simple_pusher.rb +5 -0
- data/test/configuration_test.rb +14 -0
- data/test/simple_pusher_test.rb +8 -2
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
N2MyNzZjMjk3OTYwNGFjNWMxODQ4NmIxNDk2NDhiZGQzNDViNWU5ZA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
N2M1YzcyZTM0MzI5MmU1NTdmMTdhNzdmOTcyNGM0OWQyMzU2N2JhOA==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MGZkM2ZjNmFkZGEyNjc3NGVkN2E5MzBhZjYxZjI0NDQ0OTY2NDk5NDgyNGM3
|
10
|
+
YjBjZjIwM2M2NmQwMWE2NGMwNmIwMmJlMDI3YTM4Nzk5NjZkNzA5OWI1MjJk
|
11
|
+
YTU4YmE4NWE5ZDgxMDFlNDFlYmJhZjZjN2NjNjZmZjllZmJjMGQ=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
-
|
19
|
+
Then start simple pusher server when rails app boot:
|
20
20
|
|
21
|
-
|
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
|
-
|
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
|
-
|
38
|
-
|
39
|
-
console.log("Receive:", message )
|
40
|
-
});
|
34
|
+
## Usage
|
35
|
+
|
36
|
+
Setup client js code.
|
41
37
|
|
42
|
-
|
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
|
-
|
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
|
-
|
54
|
+
Message callback at server side.
|
54
55
|
|
55
56
|
```
|
56
|
-
|
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
|
data/lib/simple_pusher/server.rb
CHANGED
data/lib/simple_pusher.rb
CHANGED
@@ -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
|
data/test/simple_pusher_test.rb
CHANGED
@@ -6,8 +6,14 @@ describe SimplePusher do
|
|
6
6
|
SimplePusher::VERSION.wont_be_nil
|
7
7
|
end
|
8
8
|
|
9
|
-
it "
|
10
|
-
#
|
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.
|
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
|