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
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f123929d5bba3aae574ce13838da5a2da5618d68
4
+ data.tar.gz: f1c22e421235bd30754318281f984b10ac3fb615
5
+ SHA512:
6
+ metadata.gz: 40a883e6e61d44f768780be9e5348b8d26c249adb4fcf039bed85a2d03eb81f0fbb41aeb8c8e97389f336418610c7f46ff6535219308c3cc92aa428d42d2f508
7
+ data.tar.gz: 3df7b288f64f260c3cf8c03c20cf19747a9950686d6c7da08ddef124fde6c742461ec992e27f9d0bb6949573b852eada0cc932979c3fd0432dc65c2756cc4de1
data/CHANGELOG.rdoc CHANGED
@@ -1,5 +1,74 @@
1
1
  = Changelog
2
2
 
3
+ == 0.5.1 / 2014-04-23
4
+
5
+ - new features:
6
+ - Support for receiving binary messages
7
+
8
+ - changed:
9
+ - Allow additional close codes to be sent by apps
10
+ - Raise better errors on missing Sec-WebSocket-Key2
11
+ - Updated http_parser.rb dependency to 0.6.0
12
+
13
+ - bug fixes:
14
+ - Abort if HTTP request URI is invalid
15
+ - Force close connections that have been sent a close handshake after a timeout
16
+
17
+ - improved spec compliance on:
18
+ - Missing continuation frames
19
+ - Fragmented control frames
20
+ - Close behaviour after protocol errors
21
+
22
+ == 0.5.0 / 2013-03-05
23
+
24
+ - new features:
25
+ - onclose handler is now passed a hash containing was_clean (set to true in drafts 03 and above when a connection is closed with a closing handshake, either by the server or the client), the close code, and reason (drafts 06 and above). Close code 1005 indicates that no code was supplied, and 1006 that the connection was closed abnormally.
26
+ - use Connection#support_close_codes? to easily check whether close codes are supported by the WebSocket protocol (drafts 06 and above)
27
+ - closes connection with 1007 close code if text frame contains invalid UTF8
28
+ - added Handshake#secure? for checking whether the connection is secure (either ssl or behind an ssl proxy)
29
+
30
+ - changed:
31
+ - Defaults to sending no close code rather than 1000 (consistent with browsers)
32
+ - Allows sending a 3xxx close code
33
+ - Renamed Connection#close_websocket to Connection#close (again for consistency with browsers). Old method is available till 0.6.
34
+ - Sends reasons with internally generated closure (previously only sent code)
35
+ - Echos close code when replying to close handshake
36
+
37
+ == 0.4.0 / 2013-01-22
38
+
39
+ - new features:
40
+ - on_open handler is now passed a handshake object which exposes the request headers, path, and query parameters
41
+ - Easily access the protocol version via Handshake#protocol_version
42
+ - Easily access the origin via Handshake#origin
43
+
44
+ - changed:
45
+ - Removed Connection#request - change to using handshake passed to on_open
46
+
47
+ - internals:
48
+ - Uses the http_parser.rb gem
49
+
50
+ == 0.3.8 / 2012-07-12
51
+
52
+ - bug fixes:
53
+ - Fixed support for Ruby 1.8.7 which was broken in 0.3.7
54
+
55
+ == 0.3.7 / 2012-07-11
56
+
57
+ - new features:
58
+ - Supports sending 1009 error code when incoming frame is too large to handle, and added associated exception class WSMessageTooBigError [Martyn Loughran]
59
+ - Supports overriding the maximum frame size by setting the max_frame_size accessor on the connection object (in bytes). Default unchanged at 10MB. [Martyn Loughran]
60
+
61
+ - bug fixes:
62
+ - Fixes some encoding issues on Ruby 1.9 [Dingding Ye]
63
+ - Raises a HandshakeError if WS header is empty [Markus Fenske]
64
+ - Connection#send would mutate passed string to BINARY encoding. The fix still mutates the string by forcing the encoding back to UTF-8 before returning, but if the passed string was encoded as UTF-8 this is equivalent [Martyn Loughran]
65
+
66
+ == 0.3.6 / 2011-12-23
67
+
68
+ - new features:
69
+ - Supports sending ping & pong messages
70
+ - Supports binding to received ping & pong messages
71
+
3
72
  == 0.3.5 / 2011-10-24
4
73
 
5
74
  - new features:
data/Gemfile CHANGED
@@ -1,3 +1,9 @@
1
1
  source "http://rubygems.org"
2
2
 
3
3
  gemspec
4
+
5
+ gem "em-websocket-client", git: "git@github.com:movitto/em-websocket-client.git", branch: "expose-websocket-api"
6
+ gem "em-spec", "~> 0.2.6"
7
+ gem "em-http-request", "~> 1.1.1"
8
+ gem "rspec", "~> 2.12.0"
9
+ gem "rake"
data/LICENCE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2009-2014 Ilya Grigorik, Martyn Loughran
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,98 +1,142 @@
1
1
  # EM-WebSocket
2
2
 
3
- EventMachine based, async, Ruby WebSocket server and client. Take a look at examples directory, or check out the blog post below:
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)
4
5
 
5
- * [Ruby & Websockets: TCP for the Web](http://www.igvita.com/2009/12/22/ruby-websockets-tcp-for-the-browser/)
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/).
6
7
 
7
8
  ## Simple server example
8
9
 
9
10
  ```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
- ```
11
+ require 'em-websocket'
28
12
 
29
- ## Simple client example
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"
30
17
 
31
- ```ruby
32
- EventMachine.run {
18
+ # Access properties on the EM::WebSocket::Handshake object, e.g.
19
+ # path, query_string, origin, headers
33
20
 
34
- EventMachine::WebSocket.start_ws_client(:host => "echo.websocket.org", :port => 80) do |ws|
35
- ws.onopen {
36
- puts "WebSocket client connection open"
21
+ # Publish message to the client
22
+ ws.send "Hello Client, you connected to #{handshake.path}"
23
+ }
37
24
 
38
- # publish message to the server
39
- ws.send "Hello server", :text
40
- }
25
+ ws.onclose { puts "Connection closed" }
41
26
 
42
- ws.onclose { puts "Connection closed" }
43
- ws.onmessage{ |msg, type|
44
- puts "Received message: #{msg}"
45
- }
46
- end
27
+ ws.onmessage { |msg|
28
+ puts "Recieved message: #{msg}"
29
+ ws.send "Pong: #{msg}"
30
+ }
31
+ end
47
32
  }
48
33
  ```
49
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
+
50
74
  ## Secure server
51
75
 
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
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
53
77
 
54
- For example,
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.
55
79
 
56
80
  ```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
- }
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
65
107
  }) do |ws|
66
- ...
108
+ # ...
67
109
  end
68
110
  ```
69
111
 
70
112
  ## Handling errors
71
113
 
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:
114
+ There are two kinds of errors that need to be handled -- WebSocket protocol errors and errors in application code.
73
115
 
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.
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.
75
117
 
76
118
  ```ruby
77
119
  ws.onerror { |error|
78
- if e.kind_of?(EM::WebSocket::WebSocketError)
79
- ...
120
+ if error.kind_of?(EM::WebSocket::WebSocketError)
121
+ # ...
80
122
  end
81
123
  }
82
124
  ```
83
125
 
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`.
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.
85
129
 
86
- It is also possible to log all errors when developing by including the `:debug => true` option when initialising the WebSocket connection.
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.
87
135
 
88
136
  ## Examples & Projects using em-websocket
89
137
 
90
- * [Pusher](http://pusherapp.com) - Realtime client push
138
+ * [Pusher](http://pusher.com) - Realtime Messaging Service
91
139
  * [Livereload](https://github.com/mockko/livereload) - LiveReload applies CSS/JS changes to Safari or Chrome w/o reloading
92
140
  * [Twitter AMQP WebSocket Example](http://github.com/rubenfonseca/twitter-amqp-websocket-example)
93
141
  * examples/multicast.rb - broadcast all ruby tweets to all subscribers
94
142
  * 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,195 @@
1
+ # EM-WebSocket
2
+
3
+ <<<<<<< HEAD
4
+ EventMachine based, async, Ruby WebSocket server and client. Take a look at examples directory, or check out the blog post below:
5
+ =======
6
+ [![Gem Version](https://badge.fury.io/rb/em-websocket.png)](http://rubygems.org/gems/em-websocket)
7
+ [![Analytics](https://ga-beacon.appspot.com/UA-71196-10/em-websocket/readme)](https://github.com/igrigorik/ga-beacon)
8
+ >>>>>>> upstream/master
9
+
10
+ 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/).
11
+
12
+ ## Simple server example
13
+
14
+ ```ruby
15
+ <<<<<<< HEAD
16
+ EventMachine.run {
17
+
18
+ EventMachine::WebSocket.start_ws_server(:host => "0.0.0.0", :port => 8080) do |ws|
19
+ ws.onopen {
20
+ puts "WebSocket connection open"
21
+
22
+ # publish message to the client
23
+ ws.send "Hello Client"
24
+ }
25
+
26
+ ws.onclose { puts "Connection closed" }
27
+ ws.onmessage { |msg|
28
+ puts "Recieved message: #{msg}"
29
+ ws.send "Pong: #{msg}"
30
+ }
31
+ end
32
+ }
33
+ ```
34
+
35
+ ## Simple client example
36
+
37
+ ```ruby
38
+ EventMachine.run {
39
+
40
+ EventMachine::WebSocket.start_ws_client(:host => "echo.websocket.org", :port => 80) do |ws|
41
+ ws.onopen {
42
+ puts "WebSocket client connection open"
43
+
44
+ # publish message to the server
45
+ ws.send "Hello server", :text
46
+ }
47
+
48
+ ws.onclose { puts "Connection closed" }
49
+ ws.onmessage{ |msg, type|
50
+ puts "Received message: #{msg}"
51
+ }
52
+ end
53
+ }
54
+ ```
55
+ =======
56
+ require 'em-websocket'
57
+
58
+ EM.run {
59
+ EM::WebSocket.run(:host => "0.0.0.0", :port => 8080) do |ws|
60
+ ws.onopen { |handshake|
61
+ puts "WebSocket connection open"
62
+
63
+ # Access properties on the EM::WebSocket::Handshake object, e.g.
64
+ # path, query_string, origin, headers
65
+
66
+ # Publish message to the client
67
+ ws.send "Hello Client, you connected to #{handshake.path}"
68
+ }
69
+
70
+ ws.onclose { puts "Connection closed" }
71
+
72
+ ws.onmessage { |msg|
73
+ puts "Recieved message: #{msg}"
74
+ ws.send "Pong: #{msg}"
75
+ }
76
+ end
77
+ }
78
+ ```
79
+
80
+ ## Protocols supported, and protocol specific functionality
81
+
82
+ Supports all WebSocket protocols in use in the wild (and a few that are not): drafts 75, 76, 1-17, rfc.
83
+
84
+ 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
85
+
86
+ ### Ping & pong supported
87
+
88
+ Call `ws.pingable?` to check whether ping & pong is supported by the protocol in use.
89
+
90
+ 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.
91
+
92
+ 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.
93
+
94
+ ### Close codes and reasons
95
+
96
+ A WebSocket connection can be closed cleanly, regardless of protocol, by calling `ws.close(code = nil, body = nil)`.
97
+
98
+ 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).
99
+
100
+ The `onclose` callback is passed a hash which may contain following keys (depending on the protocol version):
101
+
102
+ * `was_clean`: boolean indicating whether the connection was closed via the close handshake.
103
+ * `code`: the close code. There are two special close codes which the server may set (as defined in the WebSocket spec):
104
+ * 1005: no code was supplied
105
+ * 1006: abnormal closure (the same as `was_clean: false`)
106
+ * `reason`: the close reason
107
+
108
+ 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)`:
109
+
110
+ * 1000: a generic normal close
111
+ * range 3xxx: reserved for libraries, frameworks, and applications (and can be registered with IANA)
112
+ * range 4xxx: for private use
113
+
114
+ If unsure use a code in the 4xxx range. em-websocket may also close a connection with one of the following close codes:
115
+
116
+ * 1002: WebSocket protocol error.
117
+ * 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`).
118
+ >>>>>>> upstream/master
119
+
120
+ ## Secure server
121
+
122
+ 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
123
+
124
+ **Warning**: Safari 5 does not currently support prompting on untrusted SSL certificates therefore using a self signed certificate may leave you scratching your head.
125
+
126
+ ```ruby
127
+ EM::WebSocket.start({
128
+ :host => "0.0.0.0",
129
+ :port => 443,
130
+ :secure => true,
131
+ :tls_options => {
132
+ :private_key_file => "/private/key",
133
+ :cert_chain_file => "/ssl/certificate"
134
+ }
135
+ }) do |ws|
136
+ # ...
137
+ end
138
+ ```
139
+
140
+ It's possible to check whether an incoming connection is secure by reading `handshake.secure?` in the onopen callback.
141
+
142
+ ## Running behind an SSL Proxy/Terminator, like Stunnel
143
+
144
+ 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.
145
+
146
+ 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).
147
+
148
+ ```ruby
149
+ EM::WebSocket.start({
150
+ :host => "0.0.0.0",
151
+ :port => 8080,
152
+ :secure_proxy => true
153
+ }) do |ws|
154
+ # ...
155
+ end
156
+ ```
157
+
158
+ ## Handling errors
159
+
160
+ There are two kinds of errors that need to be handled -- WebSocket protocol errors and errors in application code.
161
+
162
+ 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.
163
+
164
+ ```ruby
165
+ ws.onerror { |error|
166
+ if error.kind_of?(EM::WebSocket::WebSocketError)
167
+ # ...
168
+ end
169
+ }
170
+ ```
171
+
172
+ 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`.
173
+
174
+ It is also possible to log all errors when developing by including the `:debug => true` option when initialising the WebSocket server.
175
+
176
+ ## Emulating WebSockets in older browsers
177
+
178
+ 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.
179
+
180
+ 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.
181
+
182
+ ## Examples & Projects using em-websocket
183
+
184
+ * [Pusher](http://pusher.com) - Realtime Messaging Service
185
+ * [Livereload](https://github.com/mockko/livereload) - LiveReload applies CSS/JS changes to Safari or Chrome w/o reloading
186
+ * [Twitter AMQP WebSocket Example](http://github.com/rubenfonseca/twitter-amqp-websocket-example)
187
+ * examples/multicast.rb - broadcast all ruby tweets to all subscribers
188
+ * examples/echo.rb - server <> client exchange via a websocket
189
+ <<<<<<< HEAD
190
+
191
+ # License
192
+
193
+ The MIT License - Copyright (c) 2009 Ilya Grigorik
194
+ =======
195
+ >>>>>>> upstream/master