websocket-rack-noodles 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/.travis.yml +8 -0
- data/CHANGELOG.md +61 -0
- data/Gemfile +7 -0
- data/README.md +196 -0
- data/Rakefile +11 -0
- data/example/example.ru +33 -0
- data/example/html/FABridge.js +604 -0
- data/example/html/WebSocketMain.swf +0 -0
- data/example/html/index.html +76 -0
- data/example/html/swfobject.js +4 -0
- data/example/html/web_socket.js +388 -0
- data/lib/rack/websocket/application.rb +56 -0
- data/lib/rack/websocket/extensions/common.rb +67 -0
- data/lib/rack/websocket/extensions/thin/connection.rb +24 -0
- data/lib/rack/websocket/extensions/thin.rb +16 -0
- data/lib/rack/websocket/extensions.rb +14 -0
- data/lib/rack/websocket/handler/base/connection.rb +83 -0
- data/lib/rack/websocket/handler/base.rb +68 -0
- data/lib/rack/websocket/handler/stub.rb +15 -0
- data/lib/rack/websocket/handler/thin.rb +58 -0
- data/lib/rack/websocket/handler.rb +21 -0
- data/lib/rack/websocket/version.rb +5 -0
- data/lib/rack/websocket.rb +14 -0
- data/spec/spec_helper.rb +18 -0
- data/spec/support/all_drafts.rb +44 -0
- data/spec/support/all_handlers.rb +80 -0
- data/spec/support/masked_messages.rb +9 -0
- data/spec/support/requests.rb +249 -0
- data/spec/thin_spec.rb +48 -0
- data/websocket-rack.gemspec +24 -0
- metadata +137 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 831b70dad0a08ddb271e39ddd90aef36cceca285
|
4
|
+
data.tar.gz: 91e6d4ca6818b570e15a8766540dd2bf967e57bc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7e887852026879e1d14a17983522acad6fc8c462e72e8afd729163fcde1b1e239dcbbbcabb409d74430fa80edb688c4afd440dc3cc1ff029252304f0ed46ac64
|
7
|
+
data.tar.gz: 1665ccd616fc44176d7253b115fb75710c5752a8b2e6b80a63f8d040ac6d2af14802a49b4c887d71b31c927749be3f51d2ebd170b12effce472477a010fb56ae
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
## 0.4.0 / 2012-04-16
|
4
|
+
|
5
|
+
- require EventMachine ~> 1.0.0.beta.4 (support for old version was to confusing for a lot of users)
|
6
|
+
- remove thin-websocket wrapper
|
7
|
+
|
8
|
+
## 0.3.3 / 2012-02-16
|
9
|
+
|
10
|
+
- postponse sending messages and closing connection to next tick(prevent locking rest of current request)
|
11
|
+
- support for WebSocket drafts 09-13 (currently proposed as standard)
|
12
|
+
- prevent blocking tests when invalid responsed is received
|
13
|
+
|
14
|
+
## 0.3.2 / 2011-12-13
|
15
|
+
|
16
|
+
- fix bug that resulted in 'location mismatch' error on Safari and iOS
|
17
|
+
|
18
|
+
## 0.3.1 / 2011-07-29
|
19
|
+
|
20
|
+
- support for WebSocket drafts 07 and 08
|
21
|
+
|
22
|
+
## 0.3.0 / 2011-05-10
|
23
|
+
|
24
|
+
- support for WebSocket drafts 05 and 06
|
25
|
+
- fix tests in Ruby 1.9.2
|
26
|
+
- better documentation
|
27
|
+
|
28
|
+
## 0.2.1 / 2011-04-01
|
29
|
+
|
30
|
+
- bugfix: env passed to callbacks should be valid now
|
31
|
+
|
32
|
+
## 0.2.0 / 2011-04-01
|
33
|
+
|
34
|
+
- prepare for supporting server other that thin
|
35
|
+
- small changes of API
|
36
|
+
- change handling backend options
|
37
|
+
- depend on em-websocket instead of copying source
|
38
|
+
|
39
|
+
## 0.1.4 / 2011-03-13
|
40
|
+
|
41
|
+
- performance improvements thanks to rbtrace
|
42
|
+
|
43
|
+
## 0.1.3 / 2011-03-12
|
44
|
+
|
45
|
+
- fixed critical bug that duplicated instance variables
|
46
|
+
|
47
|
+
## 0.1.2 / 2011-03-08
|
48
|
+
|
49
|
+
- change Rack::WebSocket::Application @connection variable name to @conn - first one is to often used to block it
|
50
|
+
- allow debugging of WebSocket connection
|
51
|
+
|
52
|
+
## 0.1.1 / 2011-03-07
|
53
|
+
|
54
|
+
- add missing gem dependencies
|
55
|
+
- clear connection inactivity timeout for WebSocket connections
|
56
|
+
- inform about bug in EM < 1.0.0
|
57
|
+
- add thin-websocket wrapper around thin binary
|
58
|
+
|
59
|
+
## 0.1.0 / 2011-03-05
|
60
|
+
|
61
|
+
- initial release
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,196 @@
|
|
1
|
+
# WebSocket Rack
|
2
|
+
|
3
|
+
- [![Build Status](https://travis-ci.org/imanel/websocket-rack.png)](http://travis-ci.org/imanel/websocket-rack)
|
4
|
+
- [![Dependency Status](https://gemnasium.com/imanel/websocket-rack.png)](http://gemnasium.com/imanel/websocket-rack)
|
5
|
+
- [![Code Climate](https://codeclimate.com/github/imanel/websocket-rack.png)](https://codeclimate.com/github/imanel/websocket-rack)
|
6
|
+
|
7
|
+
Rack-based WebSocket server
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
Create sample rack config file, and inside build app basing on Rack::WebSocket::Application.
|
12
|
+
|
13
|
+
``` ruby
|
14
|
+
require 'rack/websocket'
|
15
|
+
|
16
|
+
class MyApp < Rack::WebSocket::Application
|
17
|
+
end
|
18
|
+
|
19
|
+
map '/' do
|
20
|
+
run MyApp.new
|
21
|
+
end
|
22
|
+
```
|
23
|
+
|
24
|
+
After that just run Rack config from Rack server:
|
25
|
+
|
26
|
+
``` bash
|
27
|
+
thin -R config.ru start
|
28
|
+
```
|
29
|
+
|
30
|
+
Done.
|
31
|
+
|
32
|
+
## Configuration
|
33
|
+
|
34
|
+
Rack::WebSocket::Application make following methods available:
|
35
|
+
|
36
|
+
### initialize
|
37
|
+
|
38
|
+
Called once after server is started. This is place for application configuration so each instance variables from here will be available in whole application.
|
39
|
+
|
40
|
+
Example:
|
41
|
+
|
42
|
+
``` ruby
|
43
|
+
class MyApp < Rack::WebSocket::Application
|
44
|
+
def initialize(options = {})
|
45
|
+
super
|
46
|
+
@myvar = 4
|
47
|
+
end
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
It is important to include 'super' in initialize function, so application will be properly configured.
|
52
|
+
|
53
|
+
Please notice that in some servers, when 'initialize' is called, EventMachine reactor is not running yet. If you would like to configure EventMachine-based software, then you need to put it inside 'EM.next_tick' block, so this function will be called in first cycle of reactor.
|
54
|
+
|
55
|
+
Example:
|
56
|
+
|
57
|
+
``` ruby
|
58
|
+
class MyWebSocket < Rack::WebSocket::Application
|
59
|
+
def initialize
|
60
|
+
EM.next_tick { @redis = EM::Hiredis.connect }
|
61
|
+
end
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
### on_open(env)
|
66
|
+
|
67
|
+
Called after client is connected. Rack env of client is passed as attribute.
|
68
|
+
|
69
|
+
Example:
|
70
|
+
|
71
|
+
``` ruby
|
72
|
+
class MyApp < Rack::WebSocket::Application
|
73
|
+
def on_open(env)
|
74
|
+
puts "Client connected"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
```
|
78
|
+
|
79
|
+
### on_close(env)
|
80
|
+
|
81
|
+
Called after client is disconnected. Rack env of client is passed as attribute.
|
82
|
+
|
83
|
+
Example:
|
84
|
+
|
85
|
+
``` ruby
|
86
|
+
class MyApp < Rack::WebSocket::Application
|
87
|
+
def on_close(env)
|
88
|
+
puts "Client disconnected"
|
89
|
+
end
|
90
|
+
end
|
91
|
+
```
|
92
|
+
|
93
|
+
### on_message(env, msg)
|
94
|
+
|
95
|
+
Called after server receive message. Rack env of client is passed as attribute.
|
96
|
+
|
97
|
+
Example:
|
98
|
+
|
99
|
+
``` ruby
|
100
|
+
class MyApp < Rack::WebSocket::Application
|
101
|
+
def on_message(env, msg)
|
102
|
+
puts "Received message: " + msg
|
103
|
+
end
|
104
|
+
end
|
105
|
+
```
|
106
|
+
|
107
|
+
### on_error(env, error)
|
108
|
+
|
109
|
+
Called after server catch error. Variable passed is instance of Ruby Exception class.
|
110
|
+
|
111
|
+
Example:
|
112
|
+
|
113
|
+
``` ruby
|
114
|
+
class MyApp < Rack::WebSocket::Application
|
115
|
+
def on_error(env, error)
|
116
|
+
puts "Error occured: " + error.message
|
117
|
+
end
|
118
|
+
end
|
119
|
+
```
|
120
|
+
|
121
|
+
### send_data(data)
|
122
|
+
|
123
|
+
Sends data do client.
|
124
|
+
|
125
|
+
Example:
|
126
|
+
|
127
|
+
``` ruby
|
128
|
+
class MyApp < Rack::WebSocket::Application
|
129
|
+
def on_open(env)
|
130
|
+
send_data "Hello to you!"
|
131
|
+
end
|
132
|
+
end
|
133
|
+
```
|
134
|
+
|
135
|
+
### close_websocket
|
136
|
+
|
137
|
+
Closes connection.
|
138
|
+
|
139
|
+
Example:
|
140
|
+
|
141
|
+
``` ruby
|
142
|
+
class MyApp < Rack::WebSocket::Application
|
143
|
+
def on_open(env)
|
144
|
+
close_websocket if env['REQUEST_PATH'] != '/websocket'
|
145
|
+
end
|
146
|
+
end
|
147
|
+
```
|
148
|
+
|
149
|
+
## Available variables:
|
150
|
+
|
151
|
+
### @options
|
152
|
+
|
153
|
+
Options passed to app on initialize.
|
154
|
+
|
155
|
+
Example:
|
156
|
+
|
157
|
+
``` ruby
|
158
|
+
# In config.ru
|
159
|
+
map '/' do
|
160
|
+
run MyApp.new :some => :variable
|
161
|
+
end
|
162
|
+
|
163
|
+
# In application instance
|
164
|
+
@options # => { :some => :variable }
|
165
|
+
```
|
166
|
+
|
167
|
+
## FAQ
|
168
|
+
|
169
|
+
### Which WebSocket drafts are supported:
|
170
|
+
|
171
|
+
Currently we support drafts -75 and -76 form old(hixie) numeration and all drafts from -00 to -13 from current(ietf-hybi) numeration.
|
172
|
+
Please note that ietf-hybi-13 is currently proposed as final standard.
|
173
|
+
|
174
|
+
### Which Rack servers are supported?
|
175
|
+
|
176
|
+
Currently we are supporting following servers:
|
177
|
+
|
178
|
+
- Thin
|
179
|
+
|
180
|
+
### How to enable debugging?
|
181
|
+
|
182
|
+
Just use :backend => { :debug => true } option when initializing your app.
|
183
|
+
|
184
|
+
### How to enable wss/SSL support?
|
185
|
+
|
186
|
+
Thin v1.2.8 has an --ssl option - just use that! :)
|
187
|
+
|
188
|
+
### How to use function xxx?
|
189
|
+
|
190
|
+
Check [Thin](http://code.macournoyer.com/thin/) config - any option supported by Thin(like demonizing, SSL etc.) is supported by WebSocket-Rack.
|
191
|
+
|
192
|
+
## About
|
193
|
+
|
194
|
+
Author: Bernard Potocki <bernard.potocki@imanel.org>
|
195
|
+
|
196
|
+
Released under MIT license.
|
data/Rakefile
ADDED
data/example/example.ru
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require './lib/rack/websocket'
|
2
|
+
|
3
|
+
class MyApp < Rack::WebSocket::Application
|
4
|
+
def on_open(env)
|
5
|
+
puts "client connected"
|
6
|
+
EM.add_timer(5) do
|
7
|
+
send_data "This message should show-up 5 secs later"
|
8
|
+
end
|
9
|
+
|
10
|
+
EM.add_timer(15) do
|
11
|
+
send_data "This message should show-up 15 secs later"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def on_message(env, msg)
|
16
|
+
puts "message received: " + msg
|
17
|
+
send_data "Message: #{msg}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def on_close(env)
|
21
|
+
puts "client disconnected"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# use Rack::CommonLogger
|
26
|
+
|
27
|
+
map '/' do
|
28
|
+
run Rack::File.new(File.expand_path(File.dirname(__FILE__)) + '/html')
|
29
|
+
end
|
30
|
+
|
31
|
+
map '/websocket' do
|
32
|
+
run MyApp.new # :backend => { :debug => true }
|
33
|
+
end
|