faye-websocket 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of faye-websocket might be problematic. Click here for more details.
- data/CHANGELOG.txt +6 -0
- data/README.rdoc +209 -24
- data/examples/app.rb +50 -0
- data/examples/config.ru +17 -0
- data/examples/server.rb +33 -32
- data/examples/sse.html +39 -0
- data/examples/{index.html → ws.html} +12 -8
- data/lib/faye/adapters/goliath.rb +47 -0
- data/lib/faye/adapters/rainbows.rb +32 -0
- data/lib/faye/adapters/rainbows_client.rb +70 -0
- data/lib/faye/{thin_extensions.rb → adapters/thin.rb} +25 -38
- data/lib/faye/eventsource.rb +118 -0
- data/lib/faye/websocket.rb +64 -30
- data/lib/faye/websocket/adapter.rb +21 -0
- data/lib/faye/websocket/api.rb +13 -62
- data/lib/faye/websocket/api/event.rb +33 -0
- data/lib/faye/websocket/api/event_target.rb +34 -0
- data/spec/faye/websocket/client_spec.rb +16 -6
- data/spec/rainbows.conf +3 -0
- data/spec/spec_helper.rb +23 -9
- metadata +45 -23
@@ -0,0 +1,21 @@
|
|
1
|
+
module Faye
|
2
|
+
class WebSocket
|
3
|
+
|
4
|
+
module Adapter
|
5
|
+
def websocket?
|
6
|
+
e = defined?(@env) ? @env : env
|
7
|
+
WebSocket.websocket?(e)
|
8
|
+
end
|
9
|
+
|
10
|
+
def eventsource?
|
11
|
+
e = defined?(@env) ? @env : env
|
12
|
+
EventSource.eventsource?(e)
|
13
|
+
end
|
14
|
+
|
15
|
+
def async_connection?
|
16
|
+
websocket? or eventsource?
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
data/lib/faye/websocket/api.rb
CHANGED
@@ -1,14 +1,20 @@
|
|
1
1
|
module Faye
|
2
2
|
class WebSocket
|
3
3
|
|
4
|
-
CONNECTING = 0
|
5
|
-
OPEN = 1
|
6
|
-
CLOSING = 2
|
7
|
-
CLOSED = 3
|
8
|
-
|
9
4
|
module API
|
10
|
-
|
11
|
-
|
5
|
+
module ReadyStates
|
6
|
+
CONNECTING = 0
|
7
|
+
OPEN = 1
|
8
|
+
CLOSING = 2
|
9
|
+
CLOSED = 3
|
10
|
+
end
|
11
|
+
|
12
|
+
require File.expand_path('../api/event_target', __FILE__)
|
13
|
+
require File.expand_path('../api/event', __FILE__)
|
14
|
+
include EventTarget
|
15
|
+
include ReadyStates
|
16
|
+
|
17
|
+
attr_reader :url, :ready_state, :buffered_amount
|
12
18
|
|
13
19
|
def receive(data)
|
14
20
|
return false unless ready_state == OPEN
|
@@ -49,61 +55,6 @@ module Faye
|
|
49
55
|
close.call
|
50
56
|
end
|
51
57
|
end
|
52
|
-
|
53
|
-
def add_event_listener(event_type, listener, use_capture = false)
|
54
|
-
@listeners ||= {}
|
55
|
-
list = @listeners[event_type] ||= []
|
56
|
-
list << listener
|
57
|
-
end
|
58
|
-
|
59
|
-
def remove_event_listener(event_type, listener, use_capture = false)
|
60
|
-
return unless @listeners and @listeners[event_type]
|
61
|
-
return @listeners.delete(event_type) unless listener
|
62
|
-
|
63
|
-
@listeners[event_type].delete_if(&listener.method(:==))
|
64
|
-
end
|
65
|
-
|
66
|
-
def dispatch_event(event)
|
67
|
-
event.target = event.current_target = self
|
68
|
-
event.event_phase = Event::AT_TARGET
|
69
|
-
|
70
|
-
callback = __send__("on#{ event.type }")
|
71
|
-
callback.call(event) if callback
|
72
|
-
|
73
|
-
return unless @listeners and @listeners[event.type]
|
74
|
-
@listeners[event.type].each do |listener|
|
75
|
-
listener.call(event)
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
class Event
|
81
|
-
attr_reader :type, :bubbles, :cancelable
|
82
|
-
attr_accessor :target, :current_target, :event_phase, :data
|
83
|
-
|
84
|
-
CAPTURING_PHASE = 1
|
85
|
-
AT_TARGET = 2
|
86
|
-
BUBBLING_PHASE = 3
|
87
|
-
|
88
|
-
def initialize(event_type, options = {})
|
89
|
-
@type = event_type
|
90
|
-
metaclass = (class << self ; self ; end)
|
91
|
-
options.each do |key, value|
|
92
|
-
metaclass.__send__(:define_method, key) { value }
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
def init_event(event_type, can_bubble, cancelable)
|
97
|
-
@type = event_type
|
98
|
-
@bubbles = can_bubble
|
99
|
-
@cancelable = cancelable
|
100
|
-
end
|
101
|
-
|
102
|
-
def stop_propagation
|
103
|
-
end
|
104
|
-
|
105
|
-
def prevent_default
|
106
|
-
end
|
107
58
|
end
|
108
59
|
|
109
60
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Faye::WebSocket::API
|
2
|
+
class Event
|
3
|
+
|
4
|
+
attr_reader :type, :bubbles, :cancelable
|
5
|
+
attr_accessor :target, :current_target, :event_phase, :data
|
6
|
+
|
7
|
+
CAPTURING_PHASE = 1
|
8
|
+
AT_TARGET = 2
|
9
|
+
BUBBLING_PHASE = 3
|
10
|
+
|
11
|
+
def initialize(event_type, options = {})
|
12
|
+
@type = event_type
|
13
|
+
metaclass = (class << self ; self ; end)
|
14
|
+
options.each do |key, value|
|
15
|
+
metaclass.__send__(:define_method, key) { value }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def init_event(event_type, can_bubble, cancelable)
|
20
|
+
@type = event_type
|
21
|
+
@bubbles = can_bubble
|
22
|
+
@cancelable = cancelable
|
23
|
+
end
|
24
|
+
|
25
|
+
def stop_propagation
|
26
|
+
end
|
27
|
+
|
28
|
+
def prevent_default
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Faye::WebSocket::API
|
2
|
+
module EventTarget
|
3
|
+
|
4
|
+
attr_accessor :onopen, :onmessage, :onerror, :onclose
|
5
|
+
|
6
|
+
def add_event_listener(event_type, listener, use_capture = false)
|
7
|
+
@listeners ||= {}
|
8
|
+
list = @listeners[event_type] ||= []
|
9
|
+
list << listener
|
10
|
+
end
|
11
|
+
|
12
|
+
def remove_event_listener(event_type, listener, use_capture = false)
|
13
|
+
return unless @listeners and @listeners[event_type]
|
14
|
+
return @listeners.delete(event_type) unless listener
|
15
|
+
|
16
|
+
@listeners[event_type].delete_if(&listener.method(:==))
|
17
|
+
end
|
18
|
+
|
19
|
+
def dispatch_event(event)
|
20
|
+
event.target = event.current_target = self
|
21
|
+
event.event_phase = Event::AT_TARGET
|
22
|
+
|
23
|
+
callback = __send__("on#{ event.type }")
|
24
|
+
callback.call(event) if callback
|
25
|
+
|
26
|
+
return unless @listeners and @listeners[event.type]
|
27
|
+
@listeners[event.type].each do |listener|
|
28
|
+
listener.call(event)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
@@ -3,9 +3,9 @@
|
|
3
3
|
require "spec_helper"
|
4
4
|
|
5
5
|
WebSocketSteps = EM::RSpec.async_steps do
|
6
|
-
def server(port, secure, &callback)
|
6
|
+
def server(port, backend, secure, &callback)
|
7
7
|
@server = EchoServer.new
|
8
|
-
@server.listen(port, secure)
|
8
|
+
@server.listen(port, backend, secure)
|
9
9
|
@port = port
|
10
10
|
EM.add_timer(0.1, &callback)
|
11
11
|
end
|
@@ -135,21 +135,31 @@ describe Faye::WebSocket::Client do
|
|
135
135
|
end
|
136
136
|
end
|
137
137
|
|
138
|
-
describe "with a plain-text server" do
|
138
|
+
describe "with a plain-text Thin server" do
|
139
139
|
let(:socket_url) { plain_text_url }
|
140
140
|
let(:blocked_url) { secure_url }
|
141
141
|
|
142
|
-
before { server 8000, false }
|
142
|
+
before { server 8000, :thin, false }
|
143
143
|
after { sync ; stop }
|
144
144
|
|
145
145
|
it_should_behave_like "socket client"
|
146
146
|
end
|
147
147
|
|
148
|
-
describe "with a
|
148
|
+
describe "with a plain-text Rainbows server" do
|
149
|
+
let(:socket_url) { plain_text_url }
|
150
|
+
let(:blocked_url) { secure_url }
|
151
|
+
|
152
|
+
before { server 8000, :rainbows, false }
|
153
|
+
after { sync ; stop }
|
154
|
+
|
155
|
+
it_should_behave_like "socket client"
|
156
|
+
end
|
157
|
+
|
158
|
+
describe "with a secure Thin server" do
|
149
159
|
let(:socket_url) { secure_url }
|
150
160
|
let(:blocked_url) { plain_text_url }
|
151
161
|
|
152
|
-
before { server 8000, true }
|
162
|
+
before { server 8000, :thin, true }
|
153
163
|
after { sync ; stop }
|
154
164
|
|
155
165
|
it_should_behave_like "socket client"
|
data/spec/rainbows.conf
ADDED
data/spec/spec_helper.rb
CHANGED
@@ -1,9 +1,12 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'bundler/setup'
|
3
|
+
require 'thin'
|
4
|
+
require 'rainbows'
|
3
5
|
require File.expand_path('../../lib/faye/websocket', __FILE__)
|
4
6
|
require File.expand_path('../../vendor/em-rspec/lib/em-rspec', __FILE__)
|
5
7
|
|
6
8
|
Thin::Logging.silent = true
|
9
|
+
Unicorn::Configurator::DEFAULTS[:logger] = Logger.new(StringIO.new)
|
7
10
|
|
8
11
|
module EncodingHelper
|
9
12
|
def encode(message)
|
@@ -30,16 +33,27 @@ class EchoServer
|
|
30
33
|
[-1, {}, []]
|
31
34
|
end
|
32
35
|
|
33
|
-
def listen(port, ssl = false)
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
36
|
+
def listen(port, backend, ssl = false)
|
37
|
+
case backend
|
38
|
+
when :rainbows
|
39
|
+
rackup = Unicorn::Configurator::RACKUP
|
40
|
+
rackup[:port] = port
|
41
|
+
rackup[:set_listener] = true
|
42
|
+
options = rackup[:options]
|
43
|
+
options[:config_file] = File.expand_path('../rainbows.conf', __FILE__)
|
44
|
+
@server = Rainbows::HttpServer.new(self, options)
|
45
|
+
@server.start
|
46
|
+
when :thin
|
47
|
+
Rack::Handler.get('thin').run(self, :Port => port) do |s|
|
48
|
+
if ssl
|
49
|
+
s.ssl = true
|
50
|
+
s.ssl_options = {
|
51
|
+
:private_key_file => File.expand_path('../server.key', __FILE__),
|
52
|
+
:cert_chain_file => File.expand_path('../server.crt', __FILE__)
|
53
|
+
}
|
54
|
+
end
|
55
|
+
@server = s
|
41
56
|
end
|
42
|
-
@server = s
|
43
57
|
end
|
44
58
|
end
|
45
59
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faye-websocket
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-01-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: eventmachine
|
16
|
-
requirement: &
|
16
|
+
requirement: &18778460 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,32 +21,43 @@ dependencies:
|
|
21
21
|
version: 0.12.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *18778460
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
|
-
name:
|
27
|
-
requirement: &
|
26
|
+
name: rack
|
27
|
+
requirement: &18778060 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
|
-
- -
|
30
|
+
- - ! '>='
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '
|
33
|
-
type: :
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *18778060
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rainbows
|
38
|
+
requirement: &18777520 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.0.0
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *18777520
|
36
47
|
- !ruby/object:Gem::Dependency
|
37
48
|
name: rspec
|
38
|
-
requirement: &
|
49
|
+
requirement: &18777020 !ruby/object:Gem::Requirement
|
39
50
|
none: false
|
40
51
|
requirements:
|
41
52
|
- - ~>
|
42
53
|
- !ruby/object:Gem::Version
|
43
|
-
version: 2.
|
54
|
+
version: 2.8.0
|
44
55
|
type: :development
|
45
56
|
prerelease: false
|
46
|
-
version_requirements: *
|
57
|
+
version_requirements: *18777020
|
47
58
|
- !ruby/object:Gem::Dependency
|
48
|
-
name:
|
49
|
-
requirement: &
|
59
|
+
name: rake-compiler
|
60
|
+
requirement: &18776640 !ruby/object:Gem::Requirement
|
50
61
|
none: false
|
51
62
|
requirements:
|
52
63
|
- - ! '>='
|
@@ -54,18 +65,18 @@ dependencies:
|
|
54
65
|
version: '0'
|
55
66
|
type: :development
|
56
67
|
prerelease: false
|
57
|
-
version_requirements: *
|
68
|
+
version_requirements: *18776640
|
58
69
|
- !ruby/object:Gem::Dependency
|
59
|
-
name:
|
60
|
-
requirement: &
|
70
|
+
name: thin
|
71
|
+
requirement: &18776100 !ruby/object:Gem::Requirement
|
61
72
|
none: false
|
62
73
|
requirements:
|
63
74
|
- - ! '>='
|
64
75
|
- !ruby/object:Gem::Version
|
65
|
-
version:
|
76
|
+
version: 1.2.0
|
66
77
|
type: :development
|
67
78
|
prerelease: false
|
68
|
-
version_requirements: *
|
79
|
+
version_requirements: *18776100
|
69
80
|
description:
|
70
81
|
email: jcoglan@gmail.com
|
71
82
|
executables: []
|
@@ -85,12 +96,22 @@ files:
|
|
85
96
|
- lib/faye/websocket/api.rb
|
86
97
|
- lib/faye/websocket/hybi_parser/handshake.rb
|
87
98
|
- lib/faye/websocket/hybi_parser/stream_reader.rb
|
99
|
+
- lib/faye/websocket/api/event_target.rb
|
100
|
+
- lib/faye/websocket/api/event.rb
|
101
|
+
- lib/faye/websocket/adapter.rb
|
88
102
|
- lib/faye/websocket.rb
|
89
|
-
- lib/faye/
|
103
|
+
- lib/faye/eventsource.rb
|
104
|
+
- lib/faye/adapters/thin.rb
|
105
|
+
- lib/faye/adapters/rainbows.rb
|
106
|
+
- lib/faye/adapters/goliath.rb
|
107
|
+
- lib/faye/adapters/rainbows_client.rb
|
108
|
+
- examples/app.rb
|
90
109
|
- examples/client.rb
|
91
110
|
- examples/haproxy.conf
|
111
|
+
- examples/sse.html
|
112
|
+
- examples/config.ru
|
92
113
|
- examples/server.rb
|
93
|
-
- examples/
|
114
|
+
- examples/ws.html
|
94
115
|
- examples/autobahn_client.rb
|
95
116
|
- spec/faye/websocket/draft75_parser_spec.rb
|
96
117
|
- spec/faye/websocket/client_spec.rb
|
@@ -98,8 +119,9 @@ files:
|
|
98
119
|
- spec/faye/websocket/hybi_parser_spec.rb
|
99
120
|
- spec/server.key
|
100
121
|
- spec/server.crt
|
122
|
+
- spec/rainbows.conf
|
101
123
|
- spec/spec_helper.rb
|
102
|
-
homepage: http://github.com/
|
124
|
+
homepage: http://github.com/faye/faye-websocket-ruby
|
103
125
|
licenses: []
|
104
126
|
post_install_message:
|
105
127
|
rdoc_options:
|