socker 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,80 @@
1
+ require_relative './socker/event_handlers.rb'
2
+ require_relative './socker/helper.rb'
3
+
4
+ require 'faye/websocket'
5
+ require 'logger'
6
+
7
+ module Socker
8
+
9
+ class App
10
+
11
+ include Socker::EventHandlers
12
+ include Socker::Helper
13
+
14
+ WEBSOCKET_STANDARD_EVENTS = [ :open, :close, :message, :error ]
15
+
16
+ attr_reader :events, :callbacks
17
+
18
+ def initialize(callbacks={})
19
+ @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(&register_handler(c))
24
+ end
25
+ end
26
+ @application.freeze
27
+ @application
28
+ end
29
+
30
+ def connection(env, opts={}, &block)
31
+ conn = Faye::WebSocket.new(env)
32
+ yield conn if block_given?
33
+ conn.rack_response
34
+ end
35
+
36
+ def socket(socket=nil)
37
+ @socket = socket
38
+ end
39
+
40
+ def on(event, handler)
41
+ @events ||= {}
42
+ @events[event] ||= lambda { |socket, ev| handler.call(socket, ev) }
43
+ end
44
+
45
+ def to_app
46
+ @application
47
+ end
48
+
49
+ def self.log(message, level=:info)
50
+ @log ||= Logger.new($stdout)
51
+ @log.send(level, message)
52
+ end
53
+
54
+ def log(message); self.class.log(message); end
55
+
56
+ private
57
+
58
+ def is_websocket?(env)
59
+ Faye::WebSocket.websocket?(env)
60
+ end
61
+
62
+ def register_handler(connection)
63
+ lambda { |event| connection.on(event, &handle_with(event, socket: connection)) }
64
+ end
65
+
66
+ def handle_with(event, opts={})
67
+ if events[event]
68
+ lambda { |ev| handle(event, opts[:socket]); events[event].call(opts[:socket], ev) }
69
+ else
70
+ method(:handle_undefined)
71
+ end
72
+ end
73
+
74
+ def handle_undefined(event)
75
+ self.class.log("WARNING: The '#{event.type}' event is not defined.", :error)
76
+ end
77
+
78
+ end
79
+
80
+ end
@@ -0,0 +1,28 @@
1
+ module Socker
2
+
3
+ module EventHandlers
4
+
5
+ def connections
6
+ @connections ||= []
7
+ end
8
+
9
+ def on_open(connection)
10
+ @callbacks[:when_active].call if connections.empty? and @callbacks[:when_active]
11
+ connections << connection
12
+ end
13
+
14
+ def on_close(connection)
15
+ connections.delete(connection)
16
+ @callbacks[:when_idle].call if connections.empty? and @callbacks[:when_active]
17
+ end
18
+
19
+ def handle(event, connection)
20
+ case event
21
+ when :open then on_open(connection)
22
+ when :close then on_close(connection)
23
+ end
24
+ end
25
+
26
+ end
27
+
28
+ end
@@ -0,0 +1,15 @@
1
+ module Socker
2
+
3
+ module Helper
4
+
5
+ def broadcast(message)
6
+ connections.each { |c| c.send(message) }
7
+ end
8
+
9
+ def pbroadcast(message, callback)
10
+ connections.each { |c| c.ping(message, &callback) }
11
+ end
12
+
13
+ end
14
+
15
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: socker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michal Fojtik
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-10-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: faye-websocket
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: A simple lib that helps you build awesome websockets apps
31
+ email: mi@mifo.sk
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - lib/socker/helper.rb
37
+ - lib/socker/event_handlers.rb
38
+ - lib/socker.rb
39
+ homepage: https://github.com/mfojtik/socker
40
+ licenses: []
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 1.8.23
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Framework for building WebSocket applications
63
+ test_files: []