sonixlabs-em-websocket 0.3.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. data/.gitignore +4 -0
  2. data/CHANGELOG.rdoc +80 -0
  3. data/Gemfile +3 -0
  4. data/README.md +98 -0
  5. data/Rakefile +11 -0
  6. data/em-websocket.gemspec +27 -0
  7. data/examples/echo.rb +8 -0
  8. data/examples/flash_policy_file_server.rb +21 -0
  9. data/examples/js/FABridge.js +604 -0
  10. data/examples/js/WebSocketMain.swf +0 -0
  11. data/examples/js/swfobject.js +4 -0
  12. data/examples/js/web_socket.js +312 -0
  13. data/examples/multicast.rb +47 -0
  14. data/examples/test.html +30 -0
  15. data/lib/em-websocket/client_connection.rb +19 -0
  16. data/lib/em-websocket/close03.rb +11 -0
  17. data/lib/em-websocket/close05.rb +11 -0
  18. data/lib/em-websocket/close06.rb +16 -0
  19. data/lib/em-websocket/close75.rb +10 -0
  20. data/lib/em-websocket/connection.rb +184 -0
  21. data/lib/em-websocket/debugger.rb +17 -0
  22. data/lib/em-websocket/framing03.rb +167 -0
  23. data/lib/em-websocket/framing04.rb +15 -0
  24. data/lib/em-websocket/framing05.rb +168 -0
  25. data/lib/em-websocket/framing07.rb +180 -0
  26. data/lib/em-websocket/framing76.rb +114 -0
  27. data/lib/em-websocket/handler.rb +56 -0
  28. data/lib/em-websocket/handler03.rb +10 -0
  29. data/lib/em-websocket/handler05.rb +10 -0
  30. data/lib/em-websocket/handler06.rb +10 -0
  31. data/lib/em-websocket/handler07.rb +10 -0
  32. data/lib/em-websocket/handler08.rb +10 -0
  33. data/lib/em-websocket/handler13.rb +10 -0
  34. data/lib/em-websocket/handler75.rb +9 -0
  35. data/lib/em-websocket/handler76.rb +12 -0
  36. data/lib/em-websocket/handler_factory.rb +107 -0
  37. data/lib/em-websocket/handshake04.rb +75 -0
  38. data/lib/em-websocket/handshake75.rb +21 -0
  39. data/lib/em-websocket/handshake76.rb +71 -0
  40. data/lib/em-websocket/masking04.rb +63 -0
  41. data/lib/em-websocket/message_processor_03.rb +38 -0
  42. data/lib/em-websocket/message_processor_06.rb +52 -0
  43. data/lib/em-websocket/version.rb +5 -0
  44. data/lib/em-websocket/websocket.rb +45 -0
  45. data/lib/em-websocket.rb +23 -0
  46. data/lib/sonixlabs-em-websocket.rb +1 -0
  47. data/spec/helper.rb +146 -0
  48. data/spec/integration/client_examples.rb +48 -0
  49. data/spec/integration/common_spec.rb +118 -0
  50. data/spec/integration/draft03_spec.rb +270 -0
  51. data/spec/integration/draft05_spec.rb +48 -0
  52. data/spec/integration/draft06_spec.rb +88 -0
  53. data/spec/integration/draft13_spec.rb +75 -0
  54. data/spec/integration/draft75_spec.rb +117 -0
  55. data/spec/integration/draft76_spec.rb +230 -0
  56. data/spec/integration/shared_examples.rb +91 -0
  57. data/spec/unit/framing_spec.rb +325 -0
  58. data/spec/unit/handler_spec.rb +147 -0
  59. data/spec/unit/masking_spec.rb +27 -0
  60. data/spec/unit/message_processor_spec.rb +36 -0
  61. metadata +198 -0
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gemspec
2
+ pkg
3
+ Gemfile.lock
4
+ tags
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,80 @@
1
+ = Changelog
2
+
3
+ == 0.3.5 / 2011-10-24
4
+
5
+ - new features:
6
+ - Support WebSocket draft 13
7
+
8
+ == 0.3.2 / 2011-10-09
9
+
10
+ - bugfixes:
11
+ - Handling of messages with > 2 frames
12
+ - Encode string passed to onmessage handler as UTF-8 on Ruby 1.9
13
+ - Add 10MB frame length limit to all draft versions
14
+
15
+ == 0.3.1 / 2011-07-28
16
+
17
+ - new features:
18
+ - Support WebSocket drafts 07 & 08
19
+
20
+ == 0.3.0 / 2011-05-06
21
+
22
+ - new features:
23
+ - Support WebSocket drafts 05 & 06
24
+ - changes:
25
+ - Accept request headers in a case insensitive manner
26
+ - Change handling of errors. Previously some application errors were caught
27
+ internally and were invisible unless an onerror callback was supplied. See
28
+ readme for details
29
+
30
+ == 0.2.1 / 2011-03-01
31
+
32
+ - bugfixes:
33
+ - Performance improvements to draft 76 framing
34
+ - Limit frame lengths for draft 76
35
+ - Better error handling for draft 76 handshake
36
+ - Ruby 1.9 support
37
+
38
+ == 0.2.0 / 2010-11-23
39
+
40
+ - new features:
41
+ - Support for WebSocket draft 03
42
+ - bugfixes:
43
+ - Handle case when handshake split into two receive_data calls
44
+ - Stricter regexp matching of frames
45
+
46
+ == 0.1.4 / 2010-08-23
47
+
48
+ - new features:
49
+ - Allow custom ssl certificate to be used by passing :tls_options
50
+ - Protect against errors caused by non limited frame lengths
51
+ - Use custom exceptions rather than RuntimeError
52
+ - bugfixes:
53
+ - Handle invalid HTTP request with HandshakeError
54
+
55
+ == 0.1.3 / 2010-07-18
56
+
57
+ - new features:
58
+ - onerror callback
59
+ - bugfixes:
60
+ - proper handling of zero spaces in key1 or key2(prevent ZeroDivisionError)
61
+ - convert received data to utf-8 to prevent ruby 1.9 errors
62
+ - fix handling of null bytes within a frame
63
+
64
+ == 0.1.2 / 2010-06-16
65
+
66
+ - new features:
67
+ - none
68
+ - bugfixes:
69
+ - allow $ character inside header key
70
+
71
+ == 0.1.1 / 2010-06-13
72
+
73
+ - new features:
74
+ - wss/ssl support
75
+ - bugfixes:
76
+ - can't & strings
77
+
78
+ == 0.1.0 / 2010-06-12
79
+
80
+ - initial release
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/README.md ADDED
@@ -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
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new do |t|
7
+ t.rspec_opts = ["-c", "-f progress", "-r ./spec/helper.rb"]
8
+ t.pattern = 'spec/**/*_spec.rb'
9
+ end
10
+
11
+ task :default => :spec
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "em-websocket/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "sonixlabs-em-websocket"
7
+ s.version = EventMachine::Websocket::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Kazuhiro Yamada"]
10
+ s.email = ["sonixlabs@sonix.asia", "kyamada@sonix.asia"]
11
+ s.homepage = "https://github.com/sonixlabs/em-websocket"
12
+ s.summary = %q{EventMachine based WebSocket server}
13
+ s.description = %q{EventMachine based WebSocket server}
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_dependency("eventmachine", ">= 0.12.9")
21
+ s.add_dependency("addressable", '>= 2.1.1')
22
+ s.add_development_dependency('em-spec', '~> 0.2.5')
23
+ s.add_development_dependency("eventmachine", "~> 0.12.10")
24
+ s.add_development_dependency('em-http-request', '~> 0.2.6')
25
+ s.add_development_dependency('rspec', "~> 2.6.0")
26
+ s.add_development_dependency('rake')
27
+ end
data/examples/echo.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'lib/em-websocket'
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
@@ -0,0 +1,21 @@
1
+ # Super simple flash policy file server
2
+ # See https://github.com/igrigorik/em-websocket/issues/61
3
+
4
+ require 'eventmachine'
5
+
6
+ module FlashPolicy
7
+ def post_init
8
+ cross_domain_xml =<<-EOF
9
+ <cross-domain-policy>
10
+ <allow-access-from domain="*" to-ports="*" />
11
+ </cross-domain-policy>
12
+ EOF
13
+
14
+ send_data cross_domain_xml
15
+ close_connection_after_writing
16
+ end
17
+ end
18
+
19
+ EM.run {
20
+ EventMachine::start_server '0.0.0.0', 843, FlashPolicy
21
+ }