sinatra-websocketio 0.0.6 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ === 0.1.0 2013-03-16
2
+
3
+ * support modular style application
4
+
1
5
  === 0.0.6 2013-03-10
2
6
 
3
7
  * add tests
data/README.md CHANGED
@@ -37,8 +37,10 @@ set :websocketio, :port => 8080
37
37
  run Sinatra::Application
38
38
  ```
39
39
  ```ruby
40
- WebSocketIO.push :temperature, 35 # to all clients
41
- WebSocketIO.push :light, {:value => 150}, {:to => session_id} # to specific client
40
+ io = Sinatra::WebSocketIO
41
+
42
+ io.push :temperature, 35 # to all clients
43
+ io.push :light, {:value => 150}, {:to => session_id} # to specific client
42
44
  ```
43
45
 
44
46
  Client Side
@@ -69,7 +71,7 @@ io.push("chat", {name: "shokai", message: "hello"}); // client -> server
69
71
  Server Side
70
72
 
71
73
  ```ruby
72
- WebSocketIO.on :chat do |data, session|
74
+ io.on :chat do |data, session|
73
75
  puts "#{data['name']} : #{data['message']} <#{session}>"
74
76
  end
75
77
  ## => "shokai : hello <12abcde345f6g7h8ijk>"
@@ -92,11 +94,11 @@ io.on("disconnect", function(){
92
94
  Server Side
93
95
 
94
96
  ```ruby
95
- WebSocketIO.on :connect do |session|
97
+ io.on :connect do |session|
96
98
  puts "new client <#{session}>"
97
99
  end
98
100
 
99
- WebSocketIO.on :disconnect do |session|
101
+ io.on :disconnect do |session|
100
102
  puts "client disconnected <#{session}>"
101
103
  end
102
104
  ```
@@ -113,7 +115,7 @@ io.on("error", function(err){
113
115
 
114
116
  Server Side
115
117
  ```ruby
116
- WebSocketIO.on :error do |e|
118
+ io.on :error do |e|
117
119
  STDERR.puts e
118
120
  end
119
121
  ```
@@ -123,16 +125,16 @@ end
123
125
  Server Side
124
126
 
125
127
  ```ruby
126
- event_id = WebSocketIO.on :chat do |data, from|
128
+ event_id = io.on :chat do |data, from|
127
129
  puts "#{data} - from#{from}"
128
130
  end
129
- WebSocketIO.removeListener event_id
131
+ io.removeListener event_id
130
132
  ```
131
133
 
132
134
  or
133
135
 
134
136
  ```ruby
135
- WebSocketIO.removeListener :chat # remove all "chat" listener
137
+ io.removeListener :chat # remove all "chat" listener
136
138
  ```
137
139
 
138
140
 
@@ -5,9 +5,14 @@ require 'digest/md5'
5
5
  require 'event_emitter'
6
6
  require 'sinatra/streaming'
7
7
  require File.expand_path '../sinatra-websocketio/version', File.dirname(__FILE__)
8
+ require File.expand_path '../sinatra-websocketio/helpers', File.dirname(__FILE__)
8
9
  require File.expand_path '../sinatra-websocketio/options', File.dirname(__FILE__)
9
10
  require File.expand_path '../sinatra-websocketio/websocketio', File.dirname(__FILE__)
10
11
  require File.expand_path '../sinatra-websocketio/application', File.dirname(__FILE__)
11
12
 
12
- Sinatra.register Sinatra::WebSocketIO
13
- WebSocketIO.start
13
+ module Sinatra
14
+ module WebSocketIO
15
+ end
16
+ register WebSocketIO
17
+ end
18
+ Sinatra::WebSocketIO.start
@@ -1,35 +1,30 @@
1
+ module Sinatra
2
+ module WebSocketIO
1
3
 
2
- module Sinatra::WebSocketIO
3
-
4
- def websocketio=(options)
5
- WebSocketIO.options = options
6
- end
7
-
8
- def websocketio
9
- WebSocketIO.options
10
- end
4
+ def websocketio=(options)
5
+ WebSocketIO.options = options
6
+ end
11
7
 
12
- helpers do
13
- def websocketio_js
14
- "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}#{env['SCRIPT_NAME']}/websocketio/websocketio.js"
8
+ def websocketio
9
+ WebSocketIO.options
15
10
  end
16
11
 
17
- def websocketio_url
18
- "ws://#{env['SERVER_NAME']}:#{WebSocketIO.options[:port]}"
12
+ def self.registered(app)
13
+ app.helpers Sinatra::WebSocketIO::Helpers
14
+
15
+ app.get '/websocketio/websocketio.js' do
16
+ content_type 'application/javascript'
17
+ @js ||= (
18
+ js = ''
19
+ Dir.glob(File.expand_path '../js/*.js', File.dirname(__FILE__)).each do |i|
20
+ File.open(i) do |f|
21
+ js += f.read
22
+ end
23
+ end
24
+ ERB.new(js).result(binding)
25
+ )
26
+ end
19
27
  end
20
- end
21
28
 
22
- get '/websocketio/websocketio.js' do
23
- content_type 'application/javascript'
24
- @js ||= (
25
- js = ''
26
- Dir.glob(File.expand_path '../js/*.js', File.dirname(__FILE__)).each do |i|
27
- File.open(i) do |f|
28
- js += f.read
29
- end
30
- end
31
- ERB.new(js).result(binding)
32
- )
33
29
  end
34
-
35
30
  end
@@ -0,0 +1,15 @@
1
+ module Sinatra
2
+ module WebSocketIO
3
+ module Helpers
4
+
5
+ def websocketio_js
6
+ "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}#{env['SCRIPT_NAME']}/websocketio/websocketio.js"
7
+ end
8
+
9
+ def websocketio_url
10
+ "ws://#{env['SERVER_NAME']}:#{WebSocketIO.options[:port]}"
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -1,35 +1,37 @@
1
- class WebSocketIO
1
+ module Sinatra
2
+ module WebSocketIO
2
3
 
3
- def self.default_options
4
- {
5
- :port => [8080, lambda{|v| v.kind_of? Fixnum }]
6
- }
7
- end
4
+ def self.default_options
5
+ {
6
+ :port => [8080, lambda{|v| v.kind_of? Fixnum and v > 0 }]
7
+ }
8
+ end
8
9
 
9
- def self.options
10
- @@options ||= (
11
- opts = {}
12
- default_options.each do |k,v|
13
- opts[k] = v[0]
14
- end
15
- opts
16
- )
17
- end
10
+ def self.options
11
+ @@options ||= (
12
+ opts = {}
13
+ default_options.each do |k,v|
14
+ opts[k] = v[0]
15
+ end
16
+ opts
17
+ )
18
+ end
18
19
 
19
- def self.options=(opts)
20
- @@options = {}
21
- opts.each do |k,v|
22
- k = k.to_sym
23
- if default_options.include? k
24
- @@options[k] = default_options[k][1].call(v) ? v : default_options[k][0]
25
- else
26
- @@options[k] = v
20
+ def self.options=(opts)
21
+ @@options = {}
22
+ opts.each do |k,v|
23
+ k = k.to_sym
24
+ if default_options.include? k
25
+ @@options[k] = default_options[k][1].call(v) ? v : default_options[k][0]
26
+ else
27
+ @@options[k] = v
28
+ end
27
29
  end
30
+ default_options.each do |k, v|
31
+ @@options[k] = v unless @@options.include? k
32
+ end
33
+ @@options
28
34
  end
29
- default_options.each do |k, v|
30
- @@options[k] = v unless @@options.include? k
31
- end
32
- @@options
33
- end
34
35
 
36
+ end
35
37
  end
@@ -1,5 +1,5 @@
1
1
  module Sinatra
2
2
  module WebSocketIO
3
- VERSION = "0.0.6"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
@@ -1,80 +1,83 @@
1
- class WebSocketIO
1
+ module Sinatra
2
+ module WebSocketIO
2
3
 
3
- def self.start
4
- EM::defer do
5
- while !EM::reactor_running? do
6
- sleep 1
7
- end
8
- puts "WebSocketIO.start port:#{options[:port]}"
9
- EM::WebSocket.run :host => "0.0.0.0", :port => options[:port] do |ws|
10
- ws.onopen do |handshake|
11
- params = parse_handshake_params handshake.path
12
- session_id = params[:session] || (create_session Socket.unpack_sockaddr_in ws.get_peername)
4
+ def self.start
5
+ EM::defer do
6
+ while !EM::reactor_running? do
7
+ sleep 1
8
+ end
9
+ puts "Sinatra::WebSocketIO.start port:#{options[:port]}"
10
+ EM::WebSocket.run :host => "0.0.0.0", :port => options[:port] do |ws|
11
+ ws.onopen do |handshake|
12
+ params = parse_handshake_params handshake.path
13
+ session_id = params[:session] || (create_session Socket.unpack_sockaddr_in ws.get_peername)
13
14
 
14
- if self.sessions.include? session_id
15
- ws.send({:type => :error, :data => "invalid session_id (#{session_id})"}.to_json)
16
- ws.close
17
- else
18
- self.sessions[session_id] = {
19
- :websocket => ws
20
- }
21
- ws.onclose do
22
- self.sessions.delete session_id
23
- self.emit :disconnect, session_id
24
- end
25
- ws.onmessage do |msg|
26
- begin
27
- data = JSON.parse msg
28
- rescue => e
29
- self.emit :error, e
15
+ if self.sessions.include? session_id
16
+ ws.send({:type => :error, :data => "invalid session_id (#{session_id})"}.to_json)
17
+ ws.close
18
+ else
19
+ self.sessions[session_id] = {
20
+ :websocket => ws
21
+ }
22
+ ws.onclose do
23
+ self.sessions.delete session_id
24
+ self.emit :disconnect, session_id
30
25
  end
31
- self.emit data['type'], data['data'], session_id if data
26
+ ws.onmessage do |msg|
27
+ begin
28
+ data = ::JSON.parse msg
29
+ rescue => e
30
+ self.emit :error, e
31
+ end
32
+ self.emit data['type'], data['data'], session_id if data
33
+ end
34
+ ws.send({:type => :__session_id, :data => session_id}.to_json)
35
+ self.emit :connect, session_id
32
36
  end
33
- ws.send({:type => :__session_id, :data => session_id}.to_json)
34
- self.emit :connect, session_id
35
- end
36
37
 
38
+ end
37
39
  end
38
40
  end
39
41
  end
40
- end
41
42
 
42
- def self.push(type, data, opt={})
43
- session_ids = opt[:to].to_s.empty? ? self.sessions.keys : [opt[:to]]
44
- session_ids.each do |id|
45
- s = sessions[id]
46
- begin
47
- s[:websocket].send({:type => type, :data => data}.to_json)
48
- rescue
49
- next
43
+ def self.push(type, data, opt={})
44
+ session_ids = opt[:to].to_s.empty? ? self.sessions.keys : [opt[:to]]
45
+ session_ids.each do |id|
46
+ s = sessions[id]
47
+ begin
48
+ s[:websocket].send({:type => type, :data => data}.to_json)
49
+ rescue
50
+ next
51
+ end
50
52
  end
51
53
  end
52
- end
53
54
 
54
- def self.sessions
55
- @@sessions ||= Hash.new{|h,session_id|
56
- h[session_id] = {
57
- :websocket => nil
55
+ def self.sessions
56
+ @@sessions ||= Hash.new{|h,session_id|
57
+ h[session_id] = {
58
+ :websocket => nil
59
+ }
58
60
  }
59
- }
60
- end
61
+ end
61
62
 
62
- def self.create_session(ip_addr)
63
- Digest::MD5.hexdigest "#{Time.now.to_i}_#{Time.now.usec}_#{ip_addr}"
64
- end
63
+ def self.create_session(ip_addr)
64
+ Digest::MD5.hexdigest "#{Time.now.to_i}_#{Time.now.usec}_#{ip_addr}"
65
+ end
65
66
 
66
- def self.parse_handshake_params(path)
67
- tmp = path.gsub(/^\//,'').split(/\=+/)
68
- params = {}
69
- while !tmp.empty?
70
- params[tmp.shift.to_sym] = tmp.shift
67
+ def self.parse_handshake_params(path)
68
+ tmp = path.gsub(/^\//,'').split(/\=+/)
69
+ params = {}
70
+ while !tmp.empty?
71
+ params[tmp.shift.to_sym] = tmp.shift
72
+ end
73
+ params
71
74
  end
72
- params
73
- end
74
75
 
76
+ end
75
77
  end
76
- EventEmitter.apply WebSocketIO
77
- WebSocketIO.on :__session_id do |data, from|
78
- WebSocketIO.push :__session_id, from, :to => from
78
+
79
+ EventEmitter.apply Sinatra::WebSocketIO
80
+ Sinatra::WebSocketIO.on :__session_id do |data, from|
81
+ push :__session_id, from, :to => from
79
82
  end
80
83
 
data/sample/Gemfile.lock CHANGED
@@ -11,8 +11,8 @@ GEM
11
11
  eventmachine
12
12
  libwebsocket
13
13
  event_emitter (0.2.3)
14
- eventmachine (1.0.1)
15
- foreman (0.61.0)
14
+ eventmachine (1.0.3)
15
+ foreman (0.62.0)
16
16
  thor (>= 0.13.6)
17
17
  haml (4.0.0)
18
18
  tilt
@@ -22,12 +22,12 @@ GEM
22
22
  addressable
23
23
  websocket
24
24
  rack (1.5.2)
25
- rack-protection (1.4.0)
25
+ rack-protection (1.5.0)
26
26
  rack
27
27
  rack-test (0.6.2)
28
28
  rack (>= 1.0)
29
- sass (3.2.6)
30
- sinatra (1.3.5)
29
+ sass (3.2.7)
30
+ sinatra (1.3.6)
31
31
  rack (~> 1.4)
32
32
  rack-protection (~> 1.3)
33
33
  tilt (~> 1.3, >= 1.3.3)
@@ -43,7 +43,7 @@ GEM
43
43
  eventmachine (>= 0.12.6)
44
44
  rack (>= 1.0.0)
45
45
  thor (0.17.0)
46
- tilt (1.3.4)
46
+ tilt (1.3.5)
47
47
  websocket (1.0.7)
48
48
 
49
49
  PLATFORMS
data/sample/config.ru CHANGED
@@ -1,12 +1,12 @@
1
1
  require 'rubygems'
2
2
  require 'bundler/setup'
3
3
  Bundler.require
4
+ require 'sinatra/base'
4
5
  if development?
5
6
  $stdout.sync = true
6
7
  require 'sinatra/reloader'
7
8
  $:.unshift File.expand_path '../lib', File.dirname(__FILE__)
8
9
  end
9
- require 'eventmachine'
10
10
  require 'sinatra/websocketio'
11
11
  require File.dirname(__FILE__)+'/main'
12
12
 
@@ -19,4 +19,4 @@ when /bsd/i then EM.kqueue
19
19
  end
20
20
  EM.set_descriptor_table_size 10000
21
21
 
22
- run Sinatra::Application
22
+ run ChatApp
data/sample/main.rb CHANGED
@@ -1,27 +1,32 @@
1
- WebSocketIO.on :connect do |session|
2
- puts "new client <#{session}>"
3
- WebSocketIO.push :chat, {:name => "system", :message => "new client <#{session}> / #{WebSocketIO.sessions.size} clients connecting"}
4
- WebSocketIO.push :chat, {:name => "system", :message => "welcome <#{session}>"}, {:to => session}
5
- end
1
+ class ChatApp < Sinatra::Base
2
+ register Sinatra::WebSocketIO
3
+ io = Sinatra::WebSocketIO
6
4
 
7
- WebSocketIO.on :disconnect do |session|
8
- puts "disconnect client <#{session}>"
9
- WebSocketIO.push :chat, {:name => "system", :message => "bye <#{session}>"}
10
- end
5
+ io.on :connect do |session|
6
+ puts "new client <#{session}>"
7
+ push :chat, {:name => "system", :message => "new client <#{session}> / #{io.sessions.size} clients connecting"}
8
+ push :chat, {:name => "system", :message => "welcome <#{session}>"}, {:to => session}
9
+ end
11
10
 
12
- WebSocketIO.on :chat do |data, from|
13
- puts "#{data['name']} : #{data['message']} (from:#{from})"
14
- WebSocketIO.push :chat, data
15
- end
11
+ io.on :disconnect do |session|
12
+ puts "disconnect client <#{session}>"
13
+ push :chat, {:name => "system", :message => "bye <#{session}>"}
14
+ end
16
15
 
17
- WebSocketIO.on :error do |err|
18
- STDERR.puts "error!! #{err}"
19
- end
16
+ io.on :chat do |data, from|
17
+ puts "#{data['name']} : #{data['message']} (from:#{from})"
18
+ push :chat, data
19
+ end
20
20
 
21
- get '/' do
22
- haml :index
23
- end
21
+ io.on :error do |err|
22
+ STDERR.puts "error!! #{err}"
23
+ end
24
+
25
+ get '/' do
26
+ haml :index
27
+ end
24
28
 
25
- get '/:source.css' do
26
- scss params[:source].to_sym
29
+ get '/:source.css' do
30
+ scss params[:source].to_sym
31
+ end
27
32
  end
@@ -16,10 +16,10 @@ Gem::Specification.new do |gem|
16
16
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
17
  gem.require_paths = ["lib"]
18
18
  gem.add_dependency 'rack'
19
- gem.add_dependency 'sinatra', '>= 1.3.3'
20
- gem.add_dependency 'eventmachine', '>= 1.0.0'
21
- gem.add_dependency 'sinatra-contrib', '>= 1.3.2'
22
- gem.add_dependency 'json', '>= 1.7.0'
19
+ gem.add_dependency 'sinatra'
20
+ gem.add_dependency 'eventmachine'
21
+ gem.add_dependency 'sinatra-contrib'
22
+ gem.add_dependency 'json'
23
23
  gem.add_dependency 'event_emitter', '>= 0.2.0'
24
24
  gem.add_dependency 'em-websocket'
25
25
  gem.add_dependency 'em-websocket-client'
data/test/app/config.ru CHANGED
@@ -1,9 +1,8 @@
1
1
  require 'rubygems'
2
- require 'bundler'
3
- Bundler.require
2
+ require 'sinatra'
3
+ require 'sinatra/base'
4
4
  $stdout.sync = true
5
5
  $:.unshift File.expand_path '../../lib', File.dirname(__FILE__)
6
- require 'sinatra'
7
6
  require 'sinatra/websocketio'
8
7
  require File.dirname(__FILE__)+'/main'
9
8
 
data/test/app/main.rb CHANGED
@@ -3,27 +3,29 @@ File.open(pid_file, "w+") do |f|
3
3
  f.write Process.pid.to_s
4
4
  end
5
5
 
6
- class TestApp < Sinatra::Application
6
+ class TestApp < Sinatra::Base
7
+ register Sinatra::WebSocketIO
8
+ io = Sinatra::WebSocketIO
7
9
 
8
10
  get '/' do
9
11
  "sinatra-websocketio v#{Sinatra::WebSocketIO::VERSION}"
10
12
  end
11
13
 
12
- WebSocketIO.on :connect do |session|
14
+ io.on :connect do |session|
13
15
  puts "new client <#{session}>"
14
16
  end
15
17
 
16
- WebSocketIO.on :disconnect do |session|
18
+ io.on :disconnect do |session|
17
19
  puts "disconnect client <#{session}>"
18
20
  end
19
21
 
20
- WebSocketIO.on :broadcast do |data, from|
22
+ io.on :broadcast do |data, from|
21
23
  puts from
22
24
  puts "broadcast <#{from}> - #{data.to_json}"
23
25
  push :broadcast, data
24
26
  end
25
27
 
26
- WebSocketIO.on :message do |data, from|
28
+ io.on :message do |data, from|
27
29
  puts "message <#{from}> - #{data.to_json}"
28
30
  push :message, data, :to => data['to']
29
31
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-websocketio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.1.0
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-03-10 00:00:00.000000000 Z
12
+ date: 2013-03-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
@@ -34,7 +34,7 @@ dependencies:
34
34
  requirements:
35
35
  - - ! '>='
36
36
  - !ruby/object:Gem::Version
37
- version: 1.3.3
37
+ version: '0'
38
38
  type: :runtime
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
@@ -42,7 +42,7 @@ dependencies:
42
42
  requirements:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
- version: 1.3.3
45
+ version: '0'
46
46
  - !ruby/object:Gem::Dependency
47
47
  name: eventmachine
48
48
  requirement: !ruby/object:Gem::Requirement
@@ -50,7 +50,7 @@ dependencies:
50
50
  requirements:
51
51
  - - ! '>='
52
52
  - !ruby/object:Gem::Version
53
- version: 1.0.0
53
+ version: '0'
54
54
  type: :runtime
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
@@ -58,7 +58,7 @@ dependencies:
58
58
  requirements:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
- version: 1.0.0
61
+ version: '0'
62
62
  - !ruby/object:Gem::Dependency
63
63
  name: sinatra-contrib
64
64
  requirement: !ruby/object:Gem::Requirement
@@ -66,7 +66,7 @@ dependencies:
66
66
  requirements:
67
67
  - - ! '>='
68
68
  - !ruby/object:Gem::Version
69
- version: 1.3.2
69
+ version: '0'
70
70
  type: :runtime
71
71
  prerelease: false
72
72
  version_requirements: !ruby/object:Gem::Requirement
@@ -74,7 +74,7 @@ dependencies:
74
74
  requirements:
75
75
  - - ! '>='
76
76
  - !ruby/object:Gem::Version
77
- version: 1.3.2
77
+ version: '0'
78
78
  - !ruby/object:Gem::Dependency
79
79
  name: json
80
80
  requirement: !ruby/object:Gem::Requirement
@@ -82,7 +82,7 @@ dependencies:
82
82
  requirements:
83
83
  - - ! '>='
84
84
  - !ruby/object:Gem::Version
85
- version: 1.7.0
85
+ version: '0'
86
86
  type: :runtime
87
87
  prerelease: false
88
88
  version_requirements: !ruby/object:Gem::Requirement
@@ -90,7 +90,7 @@ dependencies:
90
90
  requirements:
91
91
  - - ! '>='
92
92
  - !ruby/object:Gem::Version
93
- version: 1.7.0
93
+ version: '0'
94
94
  - !ruby/object:Gem::Dependency
95
95
  name: event_emitter
96
96
  requirement: !ruby/object:Gem::Requirement
@@ -158,6 +158,7 @@ files:
158
158
  - lib/js/websocketio.js
159
159
  - lib/sinatra-websocketio.rb
160
160
  - lib/sinatra-websocketio/application.rb
161
+ - lib/sinatra-websocketio/helpers.rb
161
162
  - lib/sinatra-websocketio/options.rb
162
163
  - lib/sinatra-websocketio/version.rb
163
164
  - lib/sinatra-websocketio/websocketio.rb