garufa 0.0.1.alpha.0 → 0.0.1.alpha.1
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 +4 -4
- data/README.md +51 -4
- data/bin/garufa +1 -1
- data/garufa.gemspec +1 -1
- data/lib/garufa/api/api.rb +36 -0
- data/lib/garufa/api/handler.rb +16 -0
- data/lib/garufa/garufa_app.rb +11 -8
- data/lib/garufa/subscriptions.rb +5 -1
- data/lib/garufa/version.rb +1 -1
- data/lib/garufa/websocket/websocket.rb +26 -0
- metadata +9 -12
- data/bin/garufa.log +0 -1
- data/bin/garufa.log_stdout.log +0 -2
- data/bin/garufa.pid +0 -1
- data/lib/garufa/api_server.rb +0 -35
- data/lib/garufa/faye_websocket_patch.rb +0 -16
- data/lib/garufa/ws_server.rb +0 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fd5414b2501df19636874c36d690a62cd7d75797
|
4
|
+
data.tar.gz: a2ad215fe5d4976b10dda84f2de45e3ab2fb3624
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fed179df6b9ce97ce73aefe8b2b9ab76996764ebf1a383fded17613501cc42754f884f7276e326258ed4393833cb021bc9101d661f57bf266105fef8c012c578
|
7
|
+
data.tar.gz: 148d19e5be686167b5a4391d0e66277397a93b8444c90051be36ea05a3a6bacbdc80cfa632af0e4778d05458cc15be9ed18e9d61a15b31fb8a484782a117412c
|
data/README.md
CHANGED
@@ -12,9 +12,7 @@ Intro
|
|
12
12
|
|
13
13
|
Garufa is a websocket server which implements the [Pusher][pusher] protocol. It
|
14
14
|
was built on top of [Goliath][goliath], a high performance non-blocking web
|
15
|
-
server.
|
16
|
-
|
17
|
-
It was based on [Slanger][slanger], another server compatible with Pusher.
|
15
|
+
server and based on [Slanger][slanger], another server compatible with Pusher.
|
18
16
|
|
19
17
|
[pusher]: http://pusher.com
|
20
18
|
[goliath]: https://github.com/postrank-labs/goliath/
|
@@ -29,6 +27,55 @@ Make sure you have a ruby version >= 1.9.2
|
|
29
27
|
$ gem install garufa --pre
|
30
28
|
|
31
29
|
$ garufa --help
|
30
|
+
```
|
31
|
+
|
32
|
+
Example
|
33
|
+
-------
|
34
|
+
|
35
|
+
Create an *index.html* file:
|
32
36
|
|
33
|
-
|
37
|
+
``` html
|
38
|
+
<html>
|
39
|
+
<script src="http://js.pusher.com/2.1/pusher.min.js"></script>
|
40
|
+
<script>
|
41
|
+
Pusher.log = function(message) { console.log(message) }
|
42
|
+
Pusher.host = 'localhost'
|
43
|
+
Pusher.ws_port = 8080
|
44
|
+
|
45
|
+
var appKey = 'my-application-key'
|
46
|
+
var pusher = new Pusher(appKey)
|
47
|
+
var channel = pusher.subscribe('my-channel')
|
48
|
+
|
49
|
+
channel.bind('my-event', function(data) { alert(data.message) })
|
50
|
+
</script>
|
51
|
+
</html>
|
34
52
|
```
|
53
|
+
|
54
|
+
For ruby, create a *server.rb* file:
|
55
|
+
``` ruby
|
56
|
+
# server.rb
|
57
|
+
require 'pusher'
|
58
|
+
|
59
|
+
Pusher.host = 'localhost'
|
60
|
+
Pusher.port = 8080
|
61
|
+
|
62
|
+
Pusher.app_id = 'my_app'
|
63
|
+
Pusher.key = 'my-appication-key'
|
64
|
+
Pusher.secret = 'my-application-secret'
|
65
|
+
|
66
|
+
Pusher.trigger('my-channel', 'my-event', { message: 'hello world' })
|
67
|
+
```
|
68
|
+
|
69
|
+
1. Start `garufa` server:
|
70
|
+
|
71
|
+
``` console
|
72
|
+
$ garufa -sv --app_key my-application-key --secret my-application-secret
|
73
|
+
```
|
74
|
+
|
75
|
+
2. Point your browser to the `index.html` you created.
|
76
|
+
|
77
|
+
3. Execute the `server.rb` script:
|
78
|
+
|
79
|
+
``` console
|
80
|
+
$ ruby server.rb
|
81
|
+
```
|
data/bin/garufa
CHANGED
data/garufa.gemspec
CHANGED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'cuba'
|
2
|
+
require 'garufa/cuba/authentication'
|
3
|
+
require 'garufa/api/handler'
|
4
|
+
|
5
|
+
module Garufa
|
6
|
+
module Api
|
7
|
+
|
8
|
+
Cuba.plugin Cuba::Authentication
|
9
|
+
|
10
|
+
Server = Cuba.new do
|
11
|
+
|
12
|
+
on "apps/:app_id" do |app_id|
|
13
|
+
|
14
|
+
authenticate
|
15
|
+
|
16
|
+
# Events
|
17
|
+
on post, "events" do
|
18
|
+
# Process requests dererred in order to response immediatly.
|
19
|
+
EM.defer proc { Handler.handle_events(req.body.read) }
|
20
|
+
res.write "{}"
|
21
|
+
end
|
22
|
+
|
23
|
+
# Channels
|
24
|
+
on get, "channels" do
|
25
|
+
end
|
26
|
+
|
27
|
+
on get, "channels/:channel" do
|
28
|
+
end
|
29
|
+
|
30
|
+
# Users
|
31
|
+
on get, "channels/:channel/users" do
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'garufa/subscriptions'
|
3
|
+
require 'garufa/message'
|
4
|
+
|
5
|
+
module Garufa
|
6
|
+
module Api
|
7
|
+
module Handler
|
8
|
+
def self.handle_events(body)
|
9
|
+
message = Garufa::Message.new(JSON.parse(body))
|
10
|
+
options = { data: message.data, socket_id: message.socket_id }
|
11
|
+
Garufa::Subscriptions.notify message.channels, message.name, options
|
12
|
+
rescue JSON::ParserError
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/garufa/garufa_app.rb
CHANGED
@@ -2,20 +2,23 @@ require 'goliath/api'
|
|
2
2
|
require 'goliath/connection'
|
3
3
|
require 'faye/websocket'
|
4
4
|
require 'garufa/config'
|
5
|
-
require 'garufa/
|
6
|
-
require 'garufa/
|
7
|
-
|
8
|
-
# Remove this require after next release of faye-websocket-ruby.
|
9
|
-
# See https://github.com/faye/faye-websocket-ruby/issues/38
|
10
|
-
require 'garufa/faye_websocket_patch'
|
5
|
+
require 'garufa/websocket/websocket'
|
6
|
+
require 'garufa/api/api'
|
11
7
|
|
12
8
|
module Garufa
|
9
|
+
|
10
|
+
# Default port for api and websocket servers
|
11
|
+
DEFAULT_PORT = 8080
|
12
|
+
|
13
13
|
Faye::WebSocket.load_adapter('goliath')
|
14
14
|
|
15
15
|
class GarufaApp < Goliath::API
|
16
16
|
|
17
17
|
# Extend goliath options with our own options.
|
18
18
|
def options_parser(opts, options)
|
19
|
+
|
20
|
+
options[:port] = DEFAULT_PORT
|
21
|
+
|
19
22
|
opts.separator ""
|
20
23
|
opts.separator "Pusher options:"
|
21
24
|
|
@@ -30,9 +33,9 @@ module Garufa
|
|
30
33
|
|
31
34
|
def response(env)
|
32
35
|
if Faye::WebSocket.websocket?(env)
|
33
|
-
|
36
|
+
WebSocket::Server.call(env)
|
34
37
|
else
|
35
|
-
|
38
|
+
Api::Server.call(env)
|
36
39
|
end
|
37
40
|
end
|
38
41
|
end
|
data/lib/garufa/subscriptions.rb
CHANGED
@@ -17,6 +17,8 @@ module Garufa
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def notify(channels, event, options = {})
|
20
|
+
return unless channels.is_a?(Array) && channels.any?
|
21
|
+
|
20
22
|
channels.each do |channel|
|
21
23
|
connections = subscriptions[channel].map { |s| s.connection }
|
22
24
|
next unless connections.any?
|
@@ -102,8 +104,10 @@ module Garufa
|
|
102
104
|
def invalid_signature?
|
103
105
|
return false if public_channel?
|
104
106
|
|
107
|
+
app_key, signature = @data["auth"].split(':')
|
105
108
|
string_to_sign = [@connection.socket_id, channel].compact.join(':')
|
106
|
-
|
109
|
+
|
110
|
+
app_key != Config[:app_key] || token(string_to_sign) != signature
|
107
111
|
end
|
108
112
|
|
109
113
|
def token(string_to_sign)
|
data/lib/garufa/version.rb
CHANGED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'garufa/connection'
|
2
|
+
|
3
|
+
module Garufa
|
4
|
+
module WebSocket
|
5
|
+
|
6
|
+
Server = proc do |env|
|
7
|
+
socket = Faye::WebSocket.new(env)
|
8
|
+
connection = Connection.new(socket, env.logger)
|
9
|
+
|
10
|
+
socket.on :open do |event|
|
11
|
+
connection.establish
|
12
|
+
end
|
13
|
+
|
14
|
+
socket.on :message do |event|
|
15
|
+
connection.handle_incomming_data(event.data)
|
16
|
+
end
|
17
|
+
|
18
|
+
socket.on :close do |event|
|
19
|
+
socket = nil
|
20
|
+
end
|
21
|
+
|
22
|
+
# Return async Rack response
|
23
|
+
socket.rack_response
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: garufa
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.1.alpha.
|
4
|
+
version: 0.0.1.alpha.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juan Manuel Cuello
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-01-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: goliath
|
@@ -28,16 +28,16 @@ dependencies:
|
|
28
28
|
name: faye-websocket
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ~>
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 0.7.2
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ~>
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 0.7.2
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: cuba
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -78,20 +78,17 @@ files:
|
|
78
78
|
- README.md
|
79
79
|
- Rakefile
|
80
80
|
- lib/garufa/version.rb
|
81
|
-
- lib/garufa/
|
81
|
+
- lib/garufa/api/api.rb
|
82
|
+
- lib/garufa/api/handler.rb
|
83
|
+
- lib/garufa/websocket/websocket.rb
|
82
84
|
- lib/garufa/message.rb
|
83
85
|
- lib/garufa/config.rb
|
84
86
|
- lib/garufa/cuba/authentication.rb
|
85
87
|
- lib/garufa/subscriptions.rb
|
86
88
|
- lib/garufa/garufa_app.rb
|
87
89
|
- lib/garufa/connection.rb
|
88
|
-
- lib/garufa/faye_websocket_patch.rb
|
89
|
-
- lib/garufa/api_server.rb
|
90
90
|
- lib/garufa.rb
|
91
|
-
- bin/garufa.pid
|
92
91
|
- bin/garufa
|
93
|
-
- bin/garufa.log_stdout.log
|
94
|
-
- bin/garufa.log
|
95
92
|
- garufa.gemspec
|
96
93
|
- test/helper.rb
|
97
94
|
- test/message.rb
|
data/bin/garufa.log
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
[4707:INFO] 2013-12-21 18:43:01 :: Starting server on 0.0.0.0:9000 in development mode. Watch out for stones.
|
data/bin/garufa.log_stdout.log
DELETED
data/bin/garufa.pid
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
4707
|
data/lib/garufa/api_server.rb
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
require 'cuba'
|
2
|
-
require 'garufa/cuba/authentication'
|
3
|
-
require 'garufa/message'
|
4
|
-
require 'garufa/subscriptions'
|
5
|
-
|
6
|
-
module Garufa
|
7
|
-
Cuba.plugin Cuba::Authentication
|
8
|
-
|
9
|
-
ApiServer = Cuba.new do
|
10
|
-
|
11
|
-
on "apps/:app_id" do |app_id|
|
12
|
-
|
13
|
-
authenticate
|
14
|
-
|
15
|
-
# Events
|
16
|
-
on post, "events" do
|
17
|
-
message = Message.new(JSON.parse(req.body.read))
|
18
|
-
options = { data: message.data, socket_id: message.socket_id }
|
19
|
-
Subscriptions.notify message.channels, message.name, options
|
20
|
-
res.write "{}"
|
21
|
-
end
|
22
|
-
|
23
|
-
# Channels
|
24
|
-
on get, "channels" do
|
25
|
-
end
|
26
|
-
|
27
|
-
on get, "channels/:channel" do
|
28
|
-
end
|
29
|
-
|
30
|
-
# Users
|
31
|
-
on get, "channels/:channel/users" do
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
@@ -1,16 +0,0 @@
|
|
1
|
-
# This is included only to provide a fix when the 'env' was not set.
|
2
|
-
# This fix was already included in 'master' of faye-websocket-ruby,
|
3
|
-
# but not released as a gem yet.
|
4
|
-
#
|
5
|
-
# See https://github.com/faye/faye-websocket-ruby/issues/38
|
6
|
-
#
|
7
|
-
module Faye
|
8
|
-
class WebSocket
|
9
|
-
module Adapter
|
10
|
-
def websocket?
|
11
|
-
e = defined?(@env) ? @env : env
|
12
|
-
e && WebSocket.websocket?(e)
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
data/lib/garufa/ws_server.rb
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
require 'garufa/connection'
|
2
|
-
|
3
|
-
module Garufa
|
4
|
-
WsServer = lambda do |env|
|
5
|
-
socket = Faye::WebSocket.new(env)
|
6
|
-
connection = Connection.new(socket, env.logger)
|
7
|
-
|
8
|
-
socket.on :open do |event|
|
9
|
-
connection.establish
|
10
|
-
end
|
11
|
-
|
12
|
-
socket.on :message do |event|
|
13
|
-
connection.handle_incomming_data(event.data)
|
14
|
-
end
|
15
|
-
|
16
|
-
socket.on :close do |event|
|
17
|
-
socket = nil
|
18
|
-
end
|
19
|
-
|
20
|
-
# Return async Rack response
|
21
|
-
socket.rack_response
|
22
|
-
end
|
23
|
-
end
|