socker 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/socker.rb +19 -14
- data/lib/socker/sinatra.rb +35 -0
- metadata +3 -2
data/lib/socker.rb
CHANGED
@@ -13,22 +13,11 @@ module Socker
|
|
13
13
|
|
14
14
|
WEBSOCKET_STANDARD_EVENTS = [ :open, :close, :message, :error ]
|
15
15
|
|
16
|
-
|
16
|
+
attr_accessor :events
|
17
|
+
attr_reader :callbacks
|
17
18
|
|
18
19
|
def initialize(callbacks={})
|
19
20
|
@callbacks = callbacks
|
20
|
-
@application = lambda do |env|
|
21
|
-
return [501, {}, ['Sorry, but I am websocket app.']] unless is_websocket?(env)
|
22
|
-
connection(env) do |c|
|
23
|
-
WEBSOCKET_STANDARD_EVENTS.each(®ister_handler(c, env))
|
24
|
-
end
|
25
|
-
end
|
26
|
-
# This is needed for Puma so we can log the requests to WebSockets
|
27
|
-
@application.class.instance_eval {
|
28
|
-
define_method(:log) { |message| Socker::App.log(message) }
|
29
|
-
}
|
30
|
-
@application.freeze
|
31
|
-
@application
|
32
21
|
end
|
33
22
|
|
34
23
|
# Helper method your application can use to register new handlers
|
@@ -42,8 +31,11 @@ module Socker
|
|
42
31
|
# Handler could be anything that implements the .call method, like
|
43
32
|
# lambda, Proc or Method...
|
44
33
|
#
|
45
|
-
def on(event, handler)
|
34
|
+
def on(event, handler=nil, &block)
|
46
35
|
@events ||= {}
|
36
|
+
handler = block unless handler
|
37
|
+
return @callbacks[:when_active] = handler if event == :active
|
38
|
+
return @callbacks[:when_idle] = handler if event == :idle
|
47
39
|
raise "Unable register handler for unsupported event: #{event}" unless event_supported?(event)
|
48
40
|
@events[event] ||= lambda { |socket, ev| handler.call(socket, ev) }
|
49
41
|
end
|
@@ -53,6 +45,19 @@ module Socker
|
|
53
45
|
# run Rack::URLMap.new('/' => MyServer.new.to_app)
|
54
46
|
#
|
55
47
|
def to_app
|
48
|
+
@application = lambda do |env|
|
49
|
+
return [501, {}, ['Sorry, but I am websocket app.']] unless is_websocket?(env)
|
50
|
+
connection(env) do |c|
|
51
|
+
WEBSOCKET_STANDARD_EVENTS.each(®ister_handler(c, env))
|
52
|
+
end
|
53
|
+
end
|
54
|
+
# This is needed for Puma so we can log the requests to WebSockets
|
55
|
+
@application.class.instance_eval {
|
56
|
+
define_method(:log) { |message| Socker::App.log(message) }
|
57
|
+
}
|
58
|
+
# Prevent any modification of events or application itself at this point.
|
59
|
+
@events.freeze
|
60
|
+
@application.freeze
|
56
61
|
@application
|
57
62
|
end
|
58
63
|
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'socker'
|
2
|
+
require 'sinatra/base'
|
3
|
+
|
4
|
+
module Sinatra
|
5
|
+
|
6
|
+
module Socker
|
7
|
+
|
8
|
+
class RackAdapter
|
9
|
+
|
10
|
+
def initialize(app, options = {})
|
11
|
+
@socker_app = options[:app]
|
12
|
+
@mount_point = options[:at]
|
13
|
+
@app = app
|
14
|
+
end
|
15
|
+
|
16
|
+
def call(env)
|
17
|
+
if Rack::Request.new(env).path == @mount_point
|
18
|
+
@socker_app.call(env)
|
19
|
+
else
|
20
|
+
@app.call(env)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
def websocket(mount_point, &block)
|
27
|
+
socker_app = ::Socker::App.new
|
28
|
+
socker_app.instance_eval(&block)
|
29
|
+
use Sinatra::Socker::RackAdapter, :app => socker_app.to_app, :at => mount_point
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
register Sinatra::Socker
|
35
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: socker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-10-
|
12
|
+
date: 2013-10-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: faye-websocket
|
@@ -34,6 +34,7 @@ extensions: []
|
|
34
34
|
extra_rdoc_files: []
|
35
35
|
files:
|
36
36
|
- lib/socker/helper.rb
|
37
|
+
- lib/socker/sinatra.rb
|
37
38
|
- lib/socker/event_handlers.rb
|
38
39
|
- lib/socker.rb
|
39
40
|
homepage: https://github.com/mfojtik/socker
|