sonixlabs-em-websocket 0.3.8 → 0.5.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.rdoc +69 -0
  3. data/Gemfile +6 -0
  4. data/LICENCE +7 -0
  5. data/README.md +100 -56
  6. data/README.md.BACKUP.14928.md +195 -0
  7. data/README.md.BASE.14928.md +77 -0
  8. data/README.md.LOCAL.14928.md +98 -0
  9. data/README.md.REMOTE.14928.md +142 -0
  10. data/examples/echo.rb +23 -7
  11. data/examples/ping.rb +24 -0
  12. data/examples/test.html +5 -6
  13. data/lib/em-websocket.rb +4 -2
  14. data/lib/em-websocket/close03.rb +3 -0
  15. data/lib/em-websocket/close05.rb +3 -0
  16. data/lib/em-websocket/close06.rb +3 -0
  17. data/lib/em-websocket/close75.rb +2 -1
  18. data/lib/em-websocket/connection.rb +219 -73
  19. data/lib/em-websocket/framing03.rb +6 -11
  20. data/lib/em-websocket/framing05.rb +6 -11
  21. data/lib/em-websocket/framing07.rb +25 -20
  22. data/lib/em-websocket/framing76.rb +6 -15
  23. data/lib/em-websocket/handler.rb +69 -28
  24. data/lib/em-websocket/handler03.rb +0 -1
  25. data/lib/em-websocket/handler05.rb +0 -1
  26. data/lib/em-websocket/handler06.rb +0 -1
  27. data/lib/em-websocket/handler07.rb +0 -1
  28. data/lib/em-websocket/handler08.rb +0 -1
  29. data/lib/em-websocket/handler13.rb +0 -1
  30. data/lib/em-websocket/handler76.rb +2 -0
  31. data/lib/em-websocket/handshake.rb +156 -0
  32. data/lib/em-websocket/handshake04.rb +18 -56
  33. data/lib/em-websocket/handshake75.rb +15 -8
  34. data/lib/em-websocket/handshake76.rb +15 -14
  35. data/lib/em-websocket/masking04.rb +4 -30
  36. data/lib/em-websocket/message_processor_03.rb +13 -4
  37. data/lib/em-websocket/message_processor_06.rb +25 -13
  38. data/lib/em-websocket/version.rb +1 -1
  39. data/lib/em-websocket/websocket.rb +35 -24
  40. data/spec/helper.rb +82 -55
  41. data/spec/integration/common_spec.rb +90 -70
  42. data/spec/integration/draft03_spec.rb +84 -56
  43. data/spec/integration/draft05_spec.rb +14 -12
  44. data/spec/integration/draft06_spec.rb +66 -9
  45. data/spec/integration/draft13_spec.rb +59 -29
  46. data/spec/integration/draft75_spec.rb +46 -40
  47. data/spec/integration/draft76_spec.rb +113 -109
  48. data/spec/integration/gte_03_examples.rb +42 -0
  49. data/spec/integration/shared_examples.rb +174 -0
  50. data/spec/unit/framing_spec.rb +83 -110
  51. data/spec/unit/handshake_spec.rb +216 -0
  52. data/spec/unit/masking_spec.rb +2 -0
  53. metadata +31 -71
  54. data/examples/flash_policy_file_server.rb +0 -21
  55. data/examples/js/FABridge.js +0 -604
  56. data/examples/js/WebSocketMain.swf +0 -0
  57. data/examples/js/swfobject.js +0 -4
  58. data/examples/js/web_socket.js +0 -312
  59. data/lib/em-websocket/handler_factory.rb +0 -107
  60. data/spec/unit/handler_spec.rb +0 -147
@@ -0,0 +1,77 @@
1
+ # EM-WebSocket
2
+
3
+ EventMachine based, async, Ruby WebSocket server. Take a look at examples directory, or check out the blog post below:
4
+
5
+ * [Ruby & Websockets: TCP for the Web](http://www.igvita.com/2009/12/22/ruby-websockets-tcp-for-the-browser/)
6
+
7
+ ## Simple server example
8
+
9
+ ```ruby
10
+ EventMachine.run {
11
+
12
+ EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080) do |ws|
13
+ ws.onopen {
14
+ puts "WebSocket connection open"
15
+
16
+ # publish message to the client
17
+ ws.send "Hello Client"
18
+ }
19
+
20
+ ws.onclose { puts "Connection closed" }
21
+ ws.onmessage { |msg|
22
+ puts "Recieved message: #{msg}"
23
+ ws.send "Pong: #{msg}"
24
+ }
25
+ end
26
+ }
27
+ ```
28
+
29
+ ## Secure server
30
+
31
+ It is possible to accept secure wss:// connections by passing :secure => true when opening the connection. Safari 5 does not currently support prompting on untrusted SSL certificates therefore using signed certificates is highly recommended. Pass a :tls_options hash containing keys as described in http://eventmachine.rubyforge.org/EventMachine/Connection.html#M000296
32
+
33
+ For example,
34
+
35
+ ```ruby
36
+ EventMachine::WebSocket.start({
37
+ :host => "0.0.0.0",
38
+ :port => 443
39
+ :secure => true,
40
+ :tls_options => {
41
+ :private_key_file => "/private/key",
42
+ :cert_chain_file => "/ssl/certificate"
43
+ }
44
+ }) do |ws|
45
+ ...
46
+ end
47
+ ```
48
+
49
+ ## Handling errors
50
+
51
+ There are two kinds of errors that need to be handled - errors caused by incompatible WebSocket clients sending invalid data and errors in application code. They are handled as follows:
52
+
53
+ Errors caused by invalid WebSocket data (for example invalid errors in the WebSocket handshake or invalid message frames) raise errors which descend from `EventMachine::WebSocket::WebSocketError`. Such errors are rescued internally and the WebSocket connection will be closed immediately or an error code sent to the browser in accordance to the WebSocket specification. However it is possible to be notified in application code on such errors by including an `onerror` callback.
54
+
55
+ ```ruby
56
+ ws.onerror { |error|
57
+ if e.kind_of?(EM::WebSocket::WebSocketError)
58
+ ...
59
+ end
60
+ }
61
+ ```
62
+
63
+ Application errors are treated differently. If no `onerror` callback has been defined these errors will propagate to the EventMachine reactor, typically causing your program to terminate. If you wish to handle exceptions, simply supply an `onerror callback` and check for exceptions which are not decendant from `EventMachine::WebSocket::WebSocketError`.
64
+
65
+ It is also possible to log all errors when developing by including the `:debug => true` option when initialising the WebSocket connection.
66
+
67
+ ## Examples & Projects using em-websocket
68
+
69
+ * [Pusher](http://pusherapp.com) - Realtime client push
70
+ * [Livereload](https://github.com/mockko/livereload) - LiveReload applies CSS/JS changes to Safari or Chrome w/o reloading
71
+ * [Twitter AMQP WebSocket Example](http://github.com/rubenfonseca/twitter-amqp-websocket-example)
72
+ * examples/multicast.rb - broadcast all ruby tweets to all subscribers
73
+ * examples/echo.rb - server <> client exchange via a websocket
74
+
75
+ # License
76
+
77
+ The MIT License - Copyright (c) 2009 Ilya Grigorik
@@ -0,0 +1,98 @@
1
+ # EM-WebSocket
2
+
3
+ EventMachine based, async, Ruby WebSocket server and client. Take a look at examples directory, or check out the blog post below:
4
+
5
+ * [Ruby & Websockets: TCP for the Web](http://www.igvita.com/2009/12/22/ruby-websockets-tcp-for-the-browser/)
6
+
7
+ ## Simple server example
8
+
9
+ ```ruby
10
+ EventMachine.run {
11
+
12
+ EventMachine::WebSocket.start_ws_server(:host => "0.0.0.0", :port => 8080) do |ws|
13
+ ws.onopen {
14
+ puts "WebSocket connection open"
15
+
16
+ # publish message to the client
17
+ ws.send "Hello Client"
18
+ }
19
+
20
+ ws.onclose { puts "Connection closed" }
21
+ ws.onmessage { |msg|
22
+ puts "Recieved message: #{msg}"
23
+ ws.send "Pong: #{msg}"
24
+ }
25
+ end
26
+ }
27
+ ```
28
+
29
+ ## Simple client example
30
+
31
+ ```ruby
32
+ EventMachine.run {
33
+
34
+ EventMachine::WebSocket.start_ws_client(:host => "echo.websocket.org", :port => 80) do |ws|
35
+ ws.onopen {
36
+ puts "WebSocket client connection open"
37
+
38
+ # publish message to the server
39
+ ws.send "Hello server", :text
40
+ }
41
+
42
+ ws.onclose { puts "Connection closed" }
43
+ ws.onmessage{ |msg, type|
44
+ puts "Received message: #{msg}"
45
+ }
46
+ end
47
+ }
48
+ ```
49
+
50
+ ## Secure server
51
+
52
+ It is possible to accept secure wss:// connections by passing :secure => true when opening the connection. Safari 5 does not currently support prompting on untrusted SSL certificates therefore using signed certificates is highly recommended. Pass a :tls_options hash containing keys as described in http://eventmachine.rubyforge.org/EventMachine/Connection.html#M000296
53
+
54
+ For example,
55
+
56
+ ```ruby
57
+ EventMachine::WebSocket.start({
58
+ :host => "0.0.0.0",
59
+ :port => 443
60
+ :secure => true,
61
+ :tls_options => {
62
+ :private_key_file => "/private/key",
63
+ :cert_chain_file => "/ssl/certificate"
64
+ }
65
+ }) do |ws|
66
+ ...
67
+ end
68
+ ```
69
+
70
+ ## Handling errors
71
+
72
+ There are two kinds of errors that need to be handled - errors caused by incompatible WebSocket clients sending invalid data and errors in application code. They are handled as follows:
73
+
74
+ Errors caused by invalid WebSocket data (for example invalid errors in the WebSocket handshake or invalid message frames) raise errors which descend from `EventMachine::WebSocket::WebSocketError`. Such errors are rescued internally and the WebSocket connection will be closed immediately or an error code sent to the browser in accordance to the WebSocket specification. However it is possible to be notified in application code on such errors by including an `onerror` callback.
75
+
76
+ ```ruby
77
+ ws.onerror { |error|
78
+ if e.kind_of?(EM::WebSocket::WebSocketError)
79
+ ...
80
+ end
81
+ }
82
+ ```
83
+
84
+ Application errors are treated differently. If no `onerror` callback has been defined these errors will propagate to the EventMachine reactor, typically causing your program to terminate. If you wish to handle exceptions, simply supply an `onerror callback` and check for exceptions which are not decendant from `EventMachine::WebSocket::WebSocketError`.
85
+
86
+ It is also possible to log all errors when developing by including the `:debug => true` option when initialising the WebSocket connection.
87
+
88
+ ## Examples & Projects using em-websocket
89
+
90
+ * [Pusher](http://pusherapp.com) - Realtime client push
91
+ * [Livereload](https://github.com/mockko/livereload) - LiveReload applies CSS/JS changes to Safari or Chrome w/o reloading
92
+ * [Twitter AMQP WebSocket Example](http://github.com/rubenfonseca/twitter-amqp-websocket-example)
93
+ * examples/multicast.rb - broadcast all ruby tweets to all subscribers
94
+ * examples/echo.rb - server <> client exchange via a websocket
95
+
96
+ # License
97
+
98
+ The MIT License - Copyright (c) 2009 Ilya Grigorik
@@ -0,0 +1,142 @@
1
+ # EM-WebSocket
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/em-websocket.png)](http://rubygems.org/gems/em-websocket)
4
+ [![Analytics](https://ga-beacon.appspot.com/UA-71196-10/em-websocket/readme)](https://github.com/igrigorik/ga-beacon)
5
+
6
+ EventMachine based, async, Ruby WebSocket server. Take a look at examples directory, or check out the blog post: [Ruby & Websockets: TCP for the Web](http://www.igvita.com/2009/12/22/ruby-websockets-tcp-for-the-browser/).
7
+
8
+ ## Simple server example
9
+
10
+ ```ruby
11
+ require 'em-websocket'
12
+
13
+ EM.run {
14
+ EM::WebSocket.run(:host => "0.0.0.0", :port => 8080) do |ws|
15
+ ws.onopen { |handshake|
16
+ puts "WebSocket connection open"
17
+
18
+ # Access properties on the EM::WebSocket::Handshake object, e.g.
19
+ # path, query_string, origin, headers
20
+
21
+ # Publish message to the client
22
+ ws.send "Hello Client, you connected to #{handshake.path}"
23
+ }
24
+
25
+ ws.onclose { puts "Connection closed" }
26
+
27
+ ws.onmessage { |msg|
28
+ puts "Recieved message: #{msg}"
29
+ ws.send "Pong: #{msg}"
30
+ }
31
+ end
32
+ }
33
+ ```
34
+
35
+ ## Protocols supported, and protocol specific functionality
36
+
37
+ Supports all WebSocket protocols in use in the wild (and a few that are not): drafts 75, 76, 1-17, rfc.
38
+
39
+ While some of the changes between protocols are unimportant from the point of view of application developers, a few drafts did introduce new functionality. It's possible to easily test for this functionality by using
40
+
41
+ ### Ping & pong supported
42
+
43
+ Call `ws.pingable?` to check whether ping & pong is supported by the protocol in use.
44
+
45
+ It's possible to send a ping frame (`ws.ping(body = '')`), which the client must respond to with a pong, or the server can send an unsolicited pong frame (`ws.pong(body = '')`) which the client should not respond to. These methods can be used regardless of protocol version; they return true if the protocol supports ping&pong or false otherwise.
46
+
47
+ When receiving a ping, the server will automatically respond with a pong as the spec requires (so you should _not_ write an onping handler that replies with a pong), however it is possible to bind to ping & pong events if desired by using the `onping` and `onpong` methods.
48
+
49
+ ### Close codes and reasons
50
+
51
+ A WebSocket connection can be closed cleanly, regardless of protocol, by calling `ws.close(code = nil, body = nil)`.
52
+
53
+ Early protocols just close the TCP connection, draft 3 introduced a close handshake, and draft 6 added close codes and reasons to the close handshake. Call `ws.supports_close_codes?` to check whether close codes are supported (i.e. the protocol version is 6 or above).
54
+
55
+ The `onclose` callback is passed a hash which may contain following keys (depending on the protocol version):
56
+
57
+ * `was_clean`: boolean indicating whether the connection was closed via the close handshake.
58
+ * `code`: the close code. There are two special close codes which the server may set (as defined in the WebSocket spec):
59
+ * 1005: no code was supplied
60
+ * 1006: abnormal closure (the same as `was_clean: false`)
61
+ * `reason`: the close reason
62
+
63
+ Acceptable close codes are defined in the WebSocket rfc (<http://tools.ietf.org/html/rfc6455#section-7.4>). The following codes can be supplies when calling `ws.close(code)`:
64
+
65
+ * 1000: a generic normal close
66
+ * range 3xxx: reserved for libraries, frameworks, and applications (and can be registered with IANA)
67
+ * range 4xxx: for private use
68
+
69
+ If unsure use a code in the 4xxx range. em-websocket may also close a connection with one of the following close codes:
70
+
71
+ * 1002: WebSocket protocol error.
72
+ * 1009: Message too big to process. By default em-websocket will accept frames up to 10MB in size. If a frame is larger than this the connection will be closed without reading the frame data. The limit can be overriden globally (`EM::WebSocket.max_frame_size = bytes`) or on a specific connection (`ws.max_frame_size = bytes`).
73
+
74
+ ## Secure server
75
+
76
+ It is possible to accept secure `wss://` connections by passing `:secure => true` when opening the connection. Pass a `:tls_options` hash containing keys as described in http://eventmachine.rubyforge.org/EventMachine/Connection.html#start_tls-instance_method
77
+
78
+ **Warning**: Safari 5 does not currently support prompting on untrusted SSL certificates therefore using a self signed certificate may leave you scratching your head.
79
+
80
+ ```ruby
81
+ EM::WebSocket.start({
82
+ :host => "0.0.0.0",
83
+ :port => 443,
84
+ :secure => true,
85
+ :tls_options => {
86
+ :private_key_file => "/private/key",
87
+ :cert_chain_file => "/ssl/certificate"
88
+ }
89
+ }) do |ws|
90
+ # ...
91
+ end
92
+ ```
93
+
94
+ It's possible to check whether an incoming connection is secure by reading `handshake.secure?` in the onopen callback.
95
+
96
+ ## Running behind an SSL Proxy/Terminator, like Stunnel
97
+
98
+ The `:secure_proxy => true` option makes it possible to use em-websocket behind a secure SSL proxy/terminator like [Stunnel](http://www.stunnel.org/) which does the actual encryption & decryption.
99
+
100
+ Note that this option is only required to support drafts 75 & 76 correctly (e.g. Safari 5.1.x & earlier, and Safari on iOS 5.x & earlier).
101
+
102
+ ```ruby
103
+ EM::WebSocket.start({
104
+ :host => "0.0.0.0",
105
+ :port => 8080,
106
+ :secure_proxy => true
107
+ }) do |ws|
108
+ # ...
109
+ end
110
+ ```
111
+
112
+ ## Handling errors
113
+
114
+ There are two kinds of errors that need to be handled -- WebSocket protocol errors and errors in application code.
115
+
116
+ WebSocket protocol errors (for example invalid data in the handshake or invalid message frames) raise errors which descend from `EM::WebSocket::WebSocketError`. Such errors are rescued internally and the WebSocket connection will be closed immediately or an error code sent to the browser in accordance to the WebSocket specification. It is possible to be notified in application code of such errors by including an `onerror` callback.
117
+
118
+ ```ruby
119
+ ws.onerror { |error|
120
+ if error.kind_of?(EM::WebSocket::WebSocketError)
121
+ # ...
122
+ end
123
+ }
124
+ ```
125
+
126
+ Application errors are treated differently. If no `onerror` callback has been defined these errors will propagate to the EventMachine reactor, typically causing your program to terminate. If you wish to handle exceptions, simply supply an `onerror callback` and check for exceptions which are not descendant from `EM::WebSocket::WebSocketError`.
127
+
128
+ It is also possible to log all errors when developing by including the `:debug => true` option when initialising the WebSocket server.
129
+
130
+ ## Emulating WebSockets in older browsers
131
+
132
+ It is possible to emulate WebSockets in older browsers using flash emulation. For example take a look at the [web-socket-js](https://github.com/gimite/web-socket-js) project.
133
+
134
+ Using flash emulation does require some minimal support from em-websocket which is enabled by default. If flash connects to the WebSocket port and requests a policy file (which it will do if it fails to receive a policy file on port 843 after a timeout), em-websocket will return one. Also see <https://github.com/igrigorik/em-websocket/issues/61> for an example policy file server which you can run on port 843.
135
+
136
+ ## Examples & Projects using em-websocket
137
+
138
+ * [Pusher](http://pusher.com) - Realtime Messaging Service
139
+ * [Livereload](https://github.com/mockko/livereload) - LiveReload applies CSS/JS changes to Safari or Chrome w/o reloading
140
+ * [Twitter AMQP WebSocket Example](http://github.com/rubenfonseca/twitter-amqp-websocket-example)
141
+ * examples/multicast.rb - broadcast all ruby tweets to all subscribers
142
+ * examples/echo.rb - server <> client exchange via a websocket
data/examples/echo.rb CHANGED
@@ -1,8 +1,24 @@
1
- require 'lib/em-websocket'
1
+ require File.expand_path('../../lib/em-websocket', __FILE__)
2
2
 
3
- EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080, :debug => true) do |ws|
4
- ws.onopen { ws.send "Hello Client!"}
5
- ws.onmessage { |msg| ws.send "Pong: #{msg}" }
6
- ws.onclose { puts "WebSocket closed" }
7
- ws.onerror { |e| puts "Error: #{e.message}" }
8
- end
3
+ EM.run {
4
+ EM::WebSocket.run(:host => "0.0.0.0", :port => 8080, :debug => false) do |ws|
5
+ ws.onopen { |handshake|
6
+ puts "WebSocket opened #{{
7
+ :path => handshake.path,
8
+ :query => handshake.query,
9
+ :origin => handshake.origin,
10
+ }}"
11
+
12
+ ws.send "Hello Client!"
13
+ }
14
+ ws.onmessage { |msg|
15
+ ws.send "Pong: #{msg}"
16
+ }
17
+ ws.onclose {
18
+ puts "WebSocket closed"
19
+ }
20
+ ws.onerror { |e|
21
+ puts "Error: #{e.message}"
22
+ }
23
+ end
24
+ }
data/examples/ping.rb ADDED
@@ -0,0 +1,24 @@
1
+ require File.expand_path('../../lib/em-websocket', __FILE__)
2
+
3
+ EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080, :debug => false) do |ws|
4
+ timer = nil
5
+ ws.onopen {
6
+ puts "Ping supported: #{ws.pingable?}"
7
+ timer = EM.add_periodic_timer(1) {
8
+ p ["Sent ping", ws.ping('hello')]
9
+ }
10
+ }
11
+ ws.onpong { |value|
12
+ puts "Received pong: #{value}"
13
+ }
14
+ ws.onping { |value|
15
+ puts "Received ping: #{value}"
16
+ }
17
+ ws.onclose {
18
+ EM.cancel_timer(timer)
19
+ puts "WebSocket closed"
20
+ }
21
+ ws.onerror { |e|
22
+ puts "Error: #{e.message}"
23
+ }
24
+ end
data/examples/test.html CHANGED
@@ -1,8 +1,5 @@
1
1
  <html>
2
2
  <head>
3
- <script src='js/swfobject.js'></script>
4
- <script src='js/FABridge.js'></script>
5
- <script src='js/web_socket.js'></script>
6
3
  <script>
7
4
  function init() {
8
5
  function debug(string) {
@@ -13,9 +10,11 @@
13
10
  }
14
11
 
15
12
  var Socket = "MozWebSocket" in window ? MozWebSocket : WebSocket;
16
- var ws = new Socket("ws://localhost:8080/");
17
- ws.onmessage = function(evt) { debug("Message: " + evt.data); };
18
- ws.onclose = function() { debug("socket closed"); };
13
+ var ws = new Socket("ws://localhost:8080/foo/bar?hello=world");
14
+ ws.onmessage = function(evt) { debug("Received: " + evt.data); };
15
+ ws.onclose = function(event) {
16
+ debug("Closed - code: " + event.code + ", reason: " + event.reason + ", wasClean: " + event.wasClean);
17
+ };
19
18
  ws.onopen = function() {
20
19
  debug("connected...");
21
20
  ws.send("hello server");
data/lib/em-websocket.rb CHANGED
@@ -1,15 +1,17 @@
1
1
  $:.unshift(File.dirname(__FILE__) + '/../lib')
2
2
 
3
+ #require "eventmachine"
3
4
  require "sonixlabs-eventmachine-java"
4
5
 
5
6
  %w[
6
- debugger websocket connection client_connection
7
+ debugger websocket connection
8
+ handshake
7
9
  handshake75 handshake76 handshake04
8
10
  framing76 framing03 framing04 framing05 framing07
9
11
  close75 close03 close05 close06
10
12
  masking04
11
13
  message_processor_03 message_processor_06
12
- handler_factory handler handler75 handler76 handler03 handler05 handler06 handler07 handler08 handler13
14
+ handler handler75 handler76 handler03 handler05 handler06 handler07 handler08 handler13
13
15
  ].each do |file|
14
16
  require "em-websocket/#{file}"
15
17
  end
@@ -5,7 +5,10 @@ module EventMachine
5
5
  # TODO: Ideally send body data and check that it matches in ack
6
6
  send_frame(:close, '')
7
7
  @state = :closing
8
+ start_close_timeout
8
9
  end
10
+
11
+ def supports_close_codes?; false; end
9
12
  end
10
13
  end
11
14
  end
@@ -5,7 +5,10 @@ module EventMachine
5
5
  # TODO: Ideally send body data and check that it matches in ack
6
6
  send_frame(:close, "\x53")
7
7
  @state = :closing
8
+ start_close_timeout
8
9
  end
10
+
11
+ def supports_close_codes?; false; end
9
12
  end
10
13
  end
11
14
  end