Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Provide API for selecting which adapter to load.

  • Loading branch information...
commit 3a9d7a9d4362ab4b91ee2050a79a4e14e8670d4c 1 parent 609ea17
James Coglan jcoglan authored
Showing with 25 additions and 14 deletions.
  1. +7 −1 README.rdoc
  2. +3 −2 examples/server.rb
  3. +15 −11 lib/faye/websocket.rb
8 README.rdoc
View
@@ -55,10 +55,16 @@ is how you'd implement an echo server:
end
end
-This is a standard Rack app, so it can be run using a <tt>config.ru</tt> file:
+This is a standard Rack app, so it can be run using a <tt>config.ru</tt> file.
+However, so that incoming requests can be properly prepared to process WebSocket
+connections, you need to tell <tt>Faye::WebSocket</tt> which adapter to load;
+this can be either +thin+, +rainbows+ or +goliath+. If one of these servers is
+already loaded before <tt>faye/websocket</tt> is loaded, it will load
+appropriate adapters automatically.
# config.ru
require './app'
+ Faye::WebSocket.load_adapter('thin')
run App
5 examples/server.rb
View
@@ -5,10 +5,11 @@
port = ARGV[0] || 7000
secure = ARGV[1] == 'ssl'
engine = ARGV[2] || 'thin'
+spec = File.expand_path('../../spec', __FILE__)
-require engine
require File.expand_path('../app', __FILE__)
-spec = File.expand_path('../../spec', __FILE__)
+Faye::WebSocket.load_adapter(engine)
+
case engine
when 'goliath'
26 lib/faye/websocket.rb
View
@@ -31,6 +31,18 @@ class WebSocket
# http://www.w3.org/International/questions/qa-forms-utf-8.en.php
UTF8_MATCH = /^([\x00-\x7F]|[\xC2-\xDF][\x80-\xBF]|\xE0[\xA0-\xBF][\x80-\xBF]|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}|\xED[\x80-\x9F][\x80-\xBF]|\xF0[\x90-\xBF][\x80-\xBF]{2}|[\xF1-\xF3][\x80-\xBF]{3}|\xF4[\x80-\x8F][\x80-\xBF]{2})*$/
+ ADAPTERS = {
+ 'thin' => :Thin,
+ 'rainbows' => :Rainbows,
+ 'goliath' => :Goliath
+ }
+
+ def self.load_adapter(backend)
+ const = Kernel.const_get(ADAPTERS[backend]) rescue nil
+ require(backend) unless const
+ require File.expand_path("../adapters/#{backend}", __FILE__)
+ end
+
def self.encode(string, validate_encoding = false)
if Array === string
return nil if validate_encoding and !valid_utf8?(string)
@@ -141,15 +153,7 @@ def write(data)
end
end
-if defined? Thin
- require File.expand_path('../adapters/thin', __FILE__)
-end
-
-if defined? Rainbows
- require File.expand_path('../adapters/rainbows', __FILE__)
-end
-
-if defined? Goliath
- require File.expand_path('../adapters/goliath', __FILE__)
-end
+Faye::WebSocket.load_adapter('thin') if defined? Thin
+Faye::WebSocket.load_adapter('rainbows') if defined? Rainbows
+Faye::WebSocket.load_adapter('goliath') if defined? Goliath
Please sign in to comment.
Something went wrong with that request. Please try again.